Skip to content
Prev 306849 / 398506 Next

predict.lm if regression vector is longer than predicton vector

This can happen if your newdata data.frame does not include
all the predictors required by the formula in the model.  In that
case predict will look in the current evaluation environment to
find the missing predictors, and those will generally not match
what is in your newdata.   E.g.,
1            2            3            4            5            6 
-0.008176128  0.725397589  1.089747865  1.361792281  1.596914353  1.813575253
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : 
  variable lengths differ (found for 'x2')
In addition: Warning message:
'newdata' had 5 rows but variable(s) found have 6 rows

Put all the required variables into newdata and things are fine
1          2          3          4          5 
-0.0366699 -1.1321492 -2.3778906 -3.6469522 -4.7909516

You can also get this problem if newdata is an environment or list
instead of a data.frame, because only data.frame forces all of
its components to have the same length.


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com