extract df from lm in r

To extract the dataframe from a linear regression model in R, you can follow these steps:

  1. Fit the linear regression model using the lm() function. For example, you can use the following code to fit a model:
model <- lm(dependent_variable ~ independent_variables, data = your_data)

Replace dependent_variable with the name of your dependent variable and independent_variables with the names of your independent variables. Also, replace your_data with the name of your dataframe.

  1. Once the model is fitted, you can extract the dataframe using the data.frame() function. Here's an example:
extracted_df <- data.frame(model$model)

The model$model part returns the dataframe used to fit the model, and data.frame() creates a new dataframe from it.

  1. You can then access and manipulate the extracted dataframe as needed. For example, you can print the first few rows using the head() function:
head(extracted_df)

This will display the first six rows of the extracted dataframe.

That's it! Following these steps will allow you to extract the dataframe from a linear regression model in R.