[R] User error in calling predict/model.frame

David Winsemius dwinsemius at comcast.net
Sat Jan 29 21:31:40 CET 2011


On Jan 29, 2011, at 2:36 PM, Joshua Wiley wrote:

> On Sat, Jan 29, 2011 at 11:10 AM, David Winsemius
> <dwinsemius at comcast.net> wrote:
>>
>> On Jan 29, 2011, at 12:12 PM, Russell Pierce wrote:
>>
>>> Just in case someone else stumbles onto this thread and is facing a
>>> similar issue:  The quick solution for me turned out to be using Glm
>>> and Predict in the rms package.  Thanks go to Joshua and Ista for
>>> helping me out with this issue.  Double thanks go to Joshua for
>>> suggesting I take a closer look at the rms package.
>>>
>>> library(rms)
>>> dat <- data.frame(xxA = rnorm(20,10), xxB = rnorm(20,20))
>>> dat$out <- with(dat,xxA+xxB+xxA*xxB+rnorm(20,20))
>>> rms.res <- Glm(out ~ scale(xxA)*scale(xxB),data=dat)
>>> newdata <-
>>> as.data.frame(Predict(rms.res,xxA=c(-1,0,1),xxB=c(-1,0,1))[,1:3])
>>
>> Puzzled that you would see a particular need for the rms functions  
>> (not that
>> I have anything against the rmes package, but it doesn't seem to be  
>> needed
>> here.)
>>
>> This seems to work without error:
>>
>> rms.res <- glm(out ~ scale(xxA)*scale(xxB), data=dat)
>> newdata <- predict(rms.res,  
>> newdata=data.frame(xxA=c(-1,0,1),xxB=c(-1,0,1)))
>
> The issue is the predicted values are different.

Huh?. With the same model and data, they should be the same:
 > lm.mod2 <- lm(out ~ scale(xxA)*scale(xxB), data=dat)
 > newdata2 <- expand.grid(xxA=c(-1,0,1),xxB=c(-1,0,1))
 > newdata2$preds <- predict(lm.mod, newdata2)
 > newdata2
   xxA xxB    preds
1  -1  -1 218.6366
2   0  -1 232.4330
3   1  -1 246.2295
4  -1   0 230.9129
5   0   0 244.8696
6   1   0 258.8263
7  -1   1 243.1892
8   0   1 257.3062
9   1   1 271.4232

 > Predict(rms.res,xxA=c(-1,0,1),xxB=c(-1,0,1), conf.int=FALSE)
   xxA xxB     yhat
1  -1  -1 218.6366
2   0  -1 232.4330
3   1  -1 246.2295
4  -1   0 230.9129
5   0   0 244.8696
6   1   0 258.8263
7  -1   1 243.1892
8   0   1 257.3062
9   1   1 271.4232


>
> #################################
> require(rms)
> set.seed(10)
> dat <- data.frame(xxA = rnorm(20, 10), xxB = rnorm(20, 20))
> dat$out <- with(dat, xxA+xxB+xxA*xxB+rnorm(20,20))
>
> rms.res1 <- Glm(out ~ scale(xxA) * scale(xxB), data=dat)
> rms.res2 <- glm(out ~ scale(xxA) * scale(xxB), data=dat)
>
> as.data.frame(Predict(rms.res1, xxA = -1:1, xxB = -1:1))[,1:3]
> predict(rms.res2, expand.grid(xxA= -1:1, xxB= -1:1))
> ##################################
>
>> --
>> David Winsemius, MD
>> West Hartford, CT
>
> -- 
> Joshua Wiley
> Ph.D. Student, Health Psychology
> University of California, Los Angeles
> http://www.joshuawiley.com/

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list