[R] How does predict.lm work?
ggrothendieck at gmail.com
Tue Sep 9 17:03:47 CEST 2008
Just try it:
> BOD # built in data frame
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6
6 7 19.8
> BOD.lm <- lm(demand ~ Time, BOD)
> predict(BOD.lm, list(Time = 10))
> predict(BOD.lm, list(10))
Error in eval(expr, envir, enclos) : object "Time" not found
On Tue, Sep 9, 2008 at 10:59 AM, Williams, Robin
<robin.williams at metoffice.gov.uk> wrote:
> Please could someone explain how this element of predict.lm works?
> >From the help file
> An optional data frame in which to look for variables with which to
> predict. If omitted, the fitted values are used.
> Does this dataframe (newdata) need to have the same variable names as
> was used in the original data frame used to fit the model? Or will R
> just look across consecutive columns of newdata, and apply them to the
> call as appropriate?
> For example, if I have fitted a model with four variables
> (x1,x2,x3,x4) in my original dataframe, and then have a second dataframe
> which I want to supply to the newdata argument in predict.lm with
> variable names (x5, x6, x7, x8), do I need to change the variable names
> in my newdata dataframe to match those of the original dataframe? Or
> will R treat x5 as x1, x6 as x2, etc, when using predict.lm?
> I would like to know so that I can design the structure of some
> somewhat larger dataframes in a manner which will make using predict.lm
> straight forward and quick.
> Hope this makes sense.
> Many thanks for any help.
> Robin Williams
> Met Office summer intern - Health Forecasting
> robin.williams at metoffice.gov.uk
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help