[R] extract data from lm object and then use again?
tlumley at u.washington.edu
Fri Sep 22 18:03:23 CEST 2006
On Fri, 22 Sep 2006, Thilo Kellermann wrote:
> the data of the model fit is stored in lm$model and should work....
Not reliably. In the first place, you should use the accessor function
model.frame(model) rather than model$model, which works even if the model
was fitted with model=FALSE.
But even then,
will not work reliably.
model <- lm(log(Volume)~log(Height)+log(Girth),data=trees)
The model frame has variables called eg "log(Volume)" rather than
When you need the source data frame you need to do something like
and even this might not work, eg if the model had no data
However, if the model had no data argument then the variables
must be available in environment(formula(model)), in which case any data
frame of the right size will do.
If there are no missing observations or the model was fitted with
na.action="na.exclude" then a fairly reliable approach is to use
if it is not NULL and to fall back to model.frame(model). This is what
More information about the R-help