Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract dataframe from lm model object

Tags:

r

lm

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.

like image 920
Daniel Yudkin Avatar asked Nov 14 '25 21:11

Daniel Yudkin


1 Answers

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.

like image 146
Ben Bolker Avatar answered Nov 17 '25 20:11

Ben Bolker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!