Re: [R] extract data from lm object and then use again?

From: Thomas Lumley <>
Date: Fri 22 Sep 2006 - 16:03:23 GMT

On Fri, 22 Sep 2006, Thilo Kellermann wrote:

> Hi,
> 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,

   glm(formula(model), data=model.frame(model)) 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 "Volume".

When you need the source data frame you need to do something like

    eval(model$call$data, environment(formula(model))) and even this might not work, eg if the model had no data argument.

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

   eval(model$call$data, environment(formula(model))) if it is not NULL and to fall back to model.frame(model). This is what termplot() does.

         -thomas mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Sat Sep 23 02:12:15 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 22 Sep 2006 - 17:30:19 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.