[R] Problem with predict.lm()

David Winsemius
Wed, 29 Apr 2015 14:56:43 -0000

On Apr 29, 2015, at 7:21 AM, Martin Spindler wrote:

> Dear all,
>
> the following example somehow uses the "old data" (X) to make the
predictions, but not the new data Xnew as intended.
>
> y <- rnorm(100)
> X <- matrix(rnorm(100*10), ncol=10)
> lm <- lm(y~X)
> Xnew <- matrix(rnorm(100*20), ncol=10)
> ynew <- predict(lm, newdata=as.data.frame(Xnew)) #prediction in not made
for Xnew
>
> How can I foce predict.lm to use use the new data?

If you look at what you are passing to `predict`, it should be apparent why it
does not find a proper 'newdata' argument:

str( as.data.frame(Xnew) )
'data.frame':	200 obs. of  10 variables:
 $ V1 : num  1.2 -0.319 -0.175 -1.009 0.197 ...
 $ V2 : num  1.529 -0.107 -1.013 -0.869 1.166 ...
 $ V3 : num  -0.417 -0.34 -0.101 -0.018 -2.237 ...
 $ V4 : num  -2.2274 -1.15 0.0252 1.014 1.9455 ...
 $ V5 : num  -0.207 1.628 -0.24 -0.194 -0.722 ...
 $ V6 : num  -1.176 0.935 -0.862 -1.152 0.815 ...
 $ V7 : num  0.967 -1.464 -1.554 0.065 0.205 ...
 $ V8 : num  -0.282 1.699 -0.267 -0.8 -0.643 ...
 $ V9 : num  -0.34833 -0.24907 -0.84185 -0.0518 -0.00216 ...
 $ V10: num  -0.37 -0.227 -2.949 0.899 -0.586 ...

You need the newdata argument to be named exactly as lm would have coerced into
being when given a single X predictor that was a matrix.

Try instead:

ynew <- predict(lm, newdata=list(X=Xnew) )

--
David Winsemius
Alameda, CA, USA