Skip to content
Prev 248748 / 398503 Next

User error in calling predict/model.frame

Dear Russell,
On Fri, Jan 28, 2011 at 5:59 PM, Russell Pierce <rpier001 at ucr.edu> wrote:
predict() is the right choice.  The problem lies with your model
object (or your new data if you prefer to think of it that way), not
predict().
Just pause for a moment here, and look at what you actually have:

class(my.data[, "scale(xxA)"])
str(my.data[, "scale(xxA)"])
Now let's look at the data you made:

class(newdata[, "scale(xxA)"])
str(newdata[, "scale(xxA)"])

Notice that this differs from the data in the model object.  Of course
it would be possible to work around this using clever techniques, but
this way leads to pain.  Fortunately, there are alteRnatives.
Consider instead:

##########################
set.seed(10)
dat <- data.frame(xxA = rnorm(20, 10), xxB = rnorm(20, 20))
d2 <- as.data.frame(lapply(dat, scale))
names(d2) <- paste("s", names(dat), sep = ".")
dat <- cbind(dat, d2, out = with(dat, xxA + xxB + xxA * xxB + rnorm(20, 20)))
lm.res.scale <- lm(out ~ s.xxA * s.xxB, data = dat)
newdata <- expand.grid(s.xxA = -1:1, s.xxB = -1:1)
(newdata$Y <- predict(lm.res.scale, newdata))
###########################
Not quite sure which formula you adjusted with "as.vector()".  But in
general, the ability to transform data directly in the model call is
for quick 'n dirty convenience.  You can quickly get into a situation
where your variable names are unwieldy (possibly containing special
characters like spaces, and ",").

Cheers,

Josh

P.S. I did my undergraduate in psychology at Riverside.