Skip to content
Prev 350507 / 398503 Next

Problem with predict.lm()

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

            
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) )