[R] predict.lm "variables found" question

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Nov 9 20:48:45 CET 2006

"Larry White" <ljw1001 at gmail.com> writes:

> hello,
> I'm trying to predict some values based on a linear regression model.
> I've created the model using one dataframe, and have the prediction
> values in a second data frame (call it newdata). There are 56 rows in
> the dataframe used to create the model and 15 in newdata.
> I ran predict(model1, newdata) and get the warning: 'newdata' had 15
> rows but variable(s) found have 56 rows
> When i checked help(predict.lm) I found this:
> "Variables are first looked for in newdata and then searched for in
> the usual way (which will include the environment of the formula used
> in the fit). A warning will be given if the variables found are not of
> the same length as those in newdata if it was supplied. "
> My questions are - how can I just get predicted values for the 15 rows
> in the newdata data frame, and if that's not possible, how can I tell
> which of the 56 predicted values are derived from newdata only, if
> any.

You need to have all your predictors represented in newdata. You seem
to have at least one of them missing (a typo in a variable name could
do that). 

   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907

More information about the R-help mailing list