Daniel Yudkin
Daniel Yudkin

Reputation: 524

Extract dataframe from lm model object

Simple question for which I haven't been able to find the answer.

I'm looking for a function that extracts the name of the data frame used as input from an lm model object.

So for example, if I run

model <- lm(mpg ~ cyl, data = mtcars)

I want a function like

data.name(model)

that produces

mtcars

I've looked here and here but they don't seem to be giving me what I'm looking for. For example, unless I'm using model.frame() wrong, it just gives me the data frame with the terms used in the model, not the original input data frame.

Upvotes: 4

Views: 1087

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226182

model$call$data gives you mtcars (an unevaluated symbol); deparse(model$call$data) gives you "mtcars" (a string). eval(model$call$data) gives you back the original data object, if it is available in the current environment.

Upvotes: 7

Related Questions