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

Joshua Wiley jwiley.psych at gmail.com
Mon Jan 31 03:22:29 CET 2011


On Sun, Jan 30, 2011 at 5:59 PM, Russell Pierce <rpier001 at ucr.edu> wrote:
> R-help list and interested parties,
>
> On Cross Validated mpiktas correctly noted that both the I() and rms
> Glm/Predict solution produce incorrect results

You probably meant it this way anyways, but I would say it produces
undesired results ("incorrect" seems a bit unfair to the
developers---predict() scales the data in a model built from a scaled
object)

> (http://stats.stackexchange.com/questions/6684/how-can-one-use-the-predict-function-on-a-lm-object-where-the-ivs-have-been-dynam/6718#6718).
>  As far as I can tell, the short version is that both I and rms leave
> scale() in the formula for the lm object, so predict and Predict() run
> scale on the provided newdata prior to generating the actual
> prediction.  So, for now, there appears no easy way to do this the way
> I hoped.  Time for me to get down to writing functions.

or just scale outside of the formula, which can be done in a couple
lines of code and equally computationally efficient (though possibly
at a minor memory loss).

Josh

>
> Best,
>
> Russell S. Pierce, M.A.
> Visual Cognition Lab
> Department of Psychology
> University of California, Riverside
> 900 University Avenue
> Riverside, CA 92521
> Lab Phone: (951) 827-7399
>
> On Sat, Jan 29, 2011 at 9:12 AM, Russell Pierce <rpier001 at ucr.edu> 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])
>>
>> -----------------------------------
>> Russell S. Pierce, M.A.
>> Visual Cognition Lab
>> Department of Psychology
>> University of California, Riverside
>> 900 University Avenue
>> Riverside, CA 92521
>> Lab Phone: (951) 827-7399



More information about the R-help mailing list