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

Richard Lawn chestnut_smiley at hotmail.com
Fri May 19 18:32:27 CEST 2006


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