[R] How to use lm.predict to obtain fitted values?

Peter Ehlers ehlers at math.ucalgary.ca
Fri May 19 23:35:41 CEST 2006



Larry Howe wrote:
> I have had a similar issue recently, and looking at the archives of this list, 
> I see other cases of it as well. It took me a while to figure out that the 
> variable name in the data frame must be identical to the variable name in the 
> model. I don't see this mentioned in the documentation of predict.lm, and R 
> issues no warning in this case.
> 
> How would I go about officially requesting that this is mentioned, either in 
> the documentation, or as a warning?
> 
> Sincerely,
> Larry Howe
> 
Here's what I would do before officially requesting anything: read
'An Introduction to R', especially section 11.3:

  predict(object, newdata=data.frame)
  The data frame supplied must have variables specified with the same
  labels as the original.

Seems pretty explicit.

As well, the predict.lm help page has, under Arguments:

  newdata   An optional data frame in which to look for variables with
            which to predict.

That, too, seems unambiguous; i.e. you can't predict with values of z
when z is not in your formula.

Peter Ehlers


> On Friday May 19 2006 13:26, Prof Brian Ripley wrote:
> 
>>data.frame(x=newData)  will not have any entries called x:  You supplied
>>newdata, so assuming you means newdata,
>>
>>
>>>data.frame(x=newdata)
>>
>>   x.1 x.2 x.3 x.4 x.5 x.6
>>1   1   1   1   1   1   1
>>
>>has 6 columns none of which is labelled x.
>>
>>If you read the help for lm, it does not mention having a matrix on the
>>rhs of a formula, and the help for data.frame does explain how it works.
>>
>>predict(model, data.frame(x=I(newData)))
>>
>>might work.
>>
>>On Fri, 19 May 2006, Richard Lawn wrote:
>>
>>>I am writing a function to assess the out of sample predictive
>>>capabilities of a time series regression model.  However lm.predict isn't
>>>behaving as I expect it to.  What I am trying to do is give it a set of
>>>explanatory variables and have it give me a single predicted value using
>>>the lm fitted model.
>>>
>>>
>>>>    model = lm(y~x)
>>>>    newdata=matrix(1,1,6)
>>>>    pred = predict.lm(model,data.frame(x=newData));
>>>
>>>Warning message:
>>>'newdata' had 6 rows but variable(s) found have 51 rows.
>>>
>>>
>>>>    pred = predict.lm(model,data.frame(newData));
>>>
>>>Warning message:
>>>'newdata' had 6 rows but variable(s) found have 51 rows.
>>>
>>>y is a vector of length 51.
>>>x is a 6x51 matrix
>>>newdata is a matrix of the explanatory variables I'd like a prediction
>>>for.
>>>
>>>The predict.lm function is giving me 51 (=number of observations I had)
>>>numbers, rather than the one number I do want - the predicted value of y,
>>>given the values of x I have supplied it.
>>>
>>>Many thanks,
>>>R
>>>




More information about the R-help mailing list