Thanks for your response Ista,

I'm looking at the results in newdata following your command.  I agree that
predict ran, but I don't think it did what I expect it to do. I may be
mistaken, but shouldn't the mean of dat$out be close to the mean of
newdata$Y?  Shouldn't the values in newdata$Y (assuming predict is working
as expected) be similar values to:

coef(lm.res.scale)[1]+coef(lm.res.scale)[2]*newdata[,1]+coef(lm.res.scale)[3]*newdata[,2]+coef(lm.res.scale)[4]*newdata[,1]*newdata[,2]

?

-----------------------------------
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 Fri, Jan 28, 2011 at 2:31 PM, Ista Zahn <izahn@psych.rochester.edu>wrote:

> Hi Russell,
> There may be some subtleties that I'm not picking up on, but the
> obvious problem is that the names of the predictors in newdata do not
> match the names of the predictors in dat.
>
> names(newdata) <- names(dat)[1:2]
>    newdata$Y <- predict(lm.obj,newdata)
>
> does work on my machine.
>
> Best,
> Ista
>
> On Fri, Jan 28, 2011 at 4:37 PM, Russell Pierce <rpier001@ucr.edu> wrote:
> > I want to predict values from an existing lm (linear model, e.g.
> > lm.obj) result in R using a new set of predictor variables (e.g.
> > newdata).  However, it seems that because my linear models was made by
> > calling scale() on the target predictor that predict exits with an
> > error, "Error in scale(xxA, center = 9.7846094491829, scale =
> > 0.959413568556403) : object 'xxA' not found".  By debugging predict, I
> > can see that the error occurs in a call to model.frame.  By debugging
> > model frame I can see the error occurs with this command:  variables
> > <- eval(predvars, data, env); it seems likely that the error is
> > because predvars looks like this:
> >
> >    list(scale(xxA, center = 10.2058714830 <tel:+12058714830>537, scale =
> 0.984627257169526),
> >    scale(xxB, center = 20.4491690881149, scale = 1.13765718273923))
> >
> > An example case:
> >
> >    dat <- data.frame(xxA = rnorm(20,10), xxB = rnorm(10,20))
> >    dat$out <- with(dat,xxA+xxB+xxA*xxB+rnorm(20,20))
> >    xVar <- "scale(xxA)"
> >    traceVar <- "scale(xxB)"
> >    DVname <- "out"
> >    lm.obj <- lm.res.scale <- lm(out ~ scale(xxA)*scale(xxB),data=dat)
> >    my.data <- lm.obj$model #load the data from the lm object
> >    X1 <- my.data[,xVar]
> >    X2 <- my.data[,traceVar]
> >    DV <- lm.obj$model[,DVname]
> >    newdata <- expand.grid(X1=c(-1,0,1),X2=c(-1,0,1))
> >    newdata$X1 <- newdata$X1 * sd(my.data[,xVar])
> >    newdata$X2 <- newdata$X2 * sd(my.data[,traceVar])
> >    names(newdata) <- c(xVar,traceVar) #have to rename to original
> > variable names for predict to work
> >    newdata$Y <- predict(lm.obj,newdata)
> >
> > Is there something I could do before passing newdata or lm.obj to
> > predict() that would prevent the error?  From the help file it looks
> > like I might be able to do something with the terms, argument but I
> > haven't quite figured out what I would need to do. Alternatively, is
> > there a fix for model.frame that would prevent the error?  Should
> > predict() behave this way?
> >
> > Thanks for your time,
> >
> > Russell S. Pierce
> >
> > ______________________________________________
> > R-help@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>
>
> --
> Ista Zahn
> Graduate student
> University of Rochester
> Department of Clinical and Social Psychology
> http://yourpsyche.org
>

	[[alternative HTML version deleted]]

