Hi list, I want to write a general function so that it would take an lm object, extract its data element, then use the data at another R function (eg, glm). I searched R-help list, and found this would do the trick of the first part: a.lm$call$data this would return a name object but could not be recognized as a data.frameby glm. I also tried call(as.character(a.lm$call$data)) or eval(call(as.character(a.lm$call$data))) neither works. By eval(call(...)), it acts as evaluating of a function, but what I want is just a data frame object which could be inserted into glm function. Anyone could help? Thanks, Mike [[alternative HTML version deleted]]
Thilo Kellermann
2006-Sep-22 15:04 UTC
[R] extract data from lm object and then use again?
Hi, the data of the model fit is stored in lm$model and should work.... Good luck, Thilo On Friday 22 September 2006 16:45, Mike Wolfgang wrote:> Hi list, > > I want to write a general function so that it would take an lm object, > extract its data element, then use the data at another R function (eg, > glm). I searched R-help list, and found this would do the trick of the > first part: a.lm$call$data > this would return a name object but could not be recognized as a > data.frameby glm. I also tried > call(as.character(a.lm$call$data)) > or > eval(call(as.character(a.lm$call$data))) > neither works. > > By eval(call(...)), it acts as evaluating of a function, but what I want is > just a data frame object which could be inserted into glm function. Anyone > could help? Thanks, > > Mike > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html and provide commented, minimal, > self-contained, reproducible code.-- ________________________ Thilo Kellermann Department of Psychiatry und Psychotherapy RWTH Aachen University Pauwelstr. 30 52074 Aachen Tel.: +49 (0)241 / 8089977 Fax.: +49 (0)241 / 8082401 E-Mail: tkellermann at ukaachen.de
Marc Schwartz (via MN)
2006-Sep-22 15:21 UTC
[R] extract data from lm object and then use again?
On Fri, 2006-09-22 at 10:45 -0400, Mike Wolfgang wrote:> Hi list, > > I want to write a general function so that it would take an lm object, > extract its data element, then use the data at another R function (eg, glm). > I searched R-help list, and found this would do the trick of the first part: > a.lm$call$data > this would return a name object but could not be recognized as a > data.frameby glm. I also tried > call(as.character(a.lm$call$data)) > or > eval(call(as.character(a.lm$call$data))) > neither works. > > By eval(call(...)), it acts as evaluating of a function, but what I want is > just a data frame object which could be inserted into glm function. Anyone > could help? Thanks, > > MikeIf the 'data' argument in lm() is used, then this approach could work:> Iris2 <- eval(lm(Sepal.Length ~ Species, data = iris)$call$data)> str(Iris2)`data.frame': 150 obs. of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ... However, note that you do not get the actual data used within the lm() function (the model frame) but the entire source data frame. What you likely want instead is the model frame containing the columns actually used in the model formula:> Iris3 <- lm(Sepal.Length ~ Species, data = iris)$model> str(Iris3)`data.frame': 150 obs. of 2 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ... - attr(*, "terms")=Classes 'terms', 'formula' length 3 Sepal.Length ~ Species .. ..- attr(*, "variables")= language list(Sepal.Length, Species) .. ..- attr(*, "factors")= int [1:2, 1] 0 1 .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : chr [1:2] "Sepal.Length" "Species" .. .. .. ..$ : chr "Species" .. ..- attr(*, "term.labels")= chr "Species" .. ..- attr(*, "order")= int 1 .. ..- attr(*, "intercept")= int 1 .. ..- attr(*, "response")= int 1 .. ..- attr(*, ".Environment")=length 15 <environment> .. ..- attr(*, "predvars")= language list(Sepal.Length, Species) .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "factor" .. .. ..- attr(*, "names")= chr [1:2] "Sepal.Length" "Species" HTH, Marc Schwartz
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. Consider 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