How to Extract Regression Coefficients from lm() Function in R


You can use the following methods to extract regression coefficients from the lm() function in R:

Method 1: Extract Regression Coefficients Only

model$coefficients

Method 2: Extract Regression Coefficients with Standard Error, T-Statistic, & P-values

summary(model)$coefficients

The following example shows how to use these methods in practice.

Example: Extract Regression Coefficients from lm() in R

Suppose we fit the following multiple linear regression model in R:

#create data frame
df <- data.frame(rating=c(67, 75, 79, 85, 90, 96, 97),
                 points=c(8, 12, 16, 15, 22, 28, 24),
                 assists=c(4, 6, 6, 5, 3, 8, 7),
                 rebounds=c(1, 4, 3, 3, 2, 6, 7))

#fit multiple linear regression model
model <- lm(rating ~ points + assists + rebounds, data=df)

We can use the summary() function to view the entire summary of the regression model:

#view model summary
summary(model)

Call:
lm(formula = rating ~ points + assists + rebounds, data = df)

Residuals:
      1       2       3       4       5       6       7 
-1.5902 -1.7181  0.2413  4.8597 -1.0201 -0.6082 -0.1644 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)   
(Intercept)  66.4355     6.6932   9.926  0.00218 **
points        1.2152     0.2788   4.359  0.02232 * 
assists      -2.5968     1.6263  -1.597  0.20860   
rebounds      2.8202     1.6118   1.750  0.17847   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 3.193 on 3 degrees of freedom
Multiple R-squared:  0.9589,	Adjusted R-squared:  0.9179 
F-statistic: 23.35 on 3 and 3 DF,  p-value: 0.01396

To view the regression coefficients only, we can use model$coefficients as follows:

#view only regression coefficients of model
model$coefficients

(Intercept)      points     assists    rebounds 
  66.435519    1.215203   -2.596789    2.820224 

We can use these coefficients to write the following fitted regression equation:

Rating = 66.43551 + 1.21520(points) – 2.59678(assists) + 2.82022(rebounds)

To view the regression coefficients along with their standard errors, t-statistics, and p-values, we can use summary(model)$coefficients as follows:

#view regression coefficients with standard errors, t-statistics, and p-values
summary(model)$coefficients

             Estimate Std. Error   t value    Pr(>|t|)
(Intercept) 66.435519  6.6931808  9.925852 0.002175313
points       1.215203  0.2787838  4.358942 0.022315418
assists     -2.596789  1.6262899 -1.596757 0.208600183
rebounds     2.820224  1.6117911  1.749745 0.178471275

We can also access specific values in this output.

For example, we can use the following code to access the p-value for the points variable:

#view p-value for points variable
summary(model)$coefficients["points", "Pr(>|t|)"]

[1] 0.02231542

Or we could use the following code to access the p-value for  each of the regression coefficients:

#view p-value for all variables
summary(model)$coefficients[, "Pr(>|t|)"]

(Intercept)      points     assists    rebounds 
0.002175313 0.022315418 0.208600183 0.178471275 

The p-values are shown for each regression coefficient in the model.

You can use similar syntax to access any of the values in the regression output.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Perform Simple Linear Regression in R
How to Perform Multiple Linear Regression in R
How to Create a Residual Plot in R

5 Replies to “How to Extract Regression Coefficients from lm() Function in R”

  1. Hi
    I am in the process of creating several models using a Shiny platform I got someone to create for me. I have no problem sharing the code with you. What I want to do is create a forecasting model once I have chosen the regression I want to add to the model. Your Example: Extract Regression Coefficients from lm() in R should help with this. I am not a coder so can you help me with other tips on how to create a multi-equation model with forecasting capability within R. There are other issues like re-estimating the model as new data becomes available and ordering the equations so that once Y1=a +bX1 is estimated Y1 Forecast can be appended to forecast Y2=c +dX2 + eY1. Thanks for any help you can offer.

  2. Hi I am in the process of creating several models using a Shiny platform I got someone to create for me. I have no problem sharing the code with you. What I want to do is create a forecasting model once I have chosen the regression I want to add to the model. Your Example: Extract Regression Coefficients from lm() in R should help with this. I am not a coder so can you help me with other tips on how to create a multi-equation model with forecasting capability within R. There are other issues like re-estimating the model as new data becomes available and ordering the equations so that once Y1=a +bX1 is estimated Y1 Forecast can be appended to forecast Y2=c +dX2 + eY1. Thanks for any help you can offer.

    1. Hi Gary…Creating a multi-equation model with forecasting capability in R using Shiny can be complex, especially if you’re not a coder. However, I can guide you through the process with some straightforward tips and steps.

      ### 1. **Basic Setup: Understanding the lm() Function**
      – **lm() Function**: This is R’s standard function for linear regression. Once you fit a model using `lm()`, you can extract coefficients, make predictions, and more.
      – **Example**:
      “`R
      model1 <- lm(Y1 ~ X1 + X2, data = your_data) coefficients <- coef(model1) summary(model1) ``` ### 2. **Creating the Multi-Equation Model** - **Sequential Forecasting**: - First, you need to estimate each equation individually. - Use the output of one equation as an input for the next. - Example: ```R model1 <- lm(Y1 ~ X1 + X2, data = your_data) your_data$Y1_forecast <- predict(model1, newdata = your_data) model2 <- lm(Y2 ~ X3 + X4 + Y1_forecast, data = your_data) your_data$Y2_forecast <- predict(model2, newdata = your_data) ``` - **Tip**: Ensure your data is correctly ordered so that forecasts from earlier models are available for subsequent models. ### 3. **Forecasting with New Data** - **Updating Models with New Data**: - Rerun the model whenever new data is added. - You can automate this within your Shiny app using `reactive()` or `observeEvent()` functions to trigger re-estimation. - Example: ```R observeEvent(input$update_button, { model1 <- lm(Y1 ~ X1 + X2, data = updated_data) updated_data$Y1_forecast <- predict(model1, newdata = updated_data) model2 <- lm(Y2 ~ X3 + X4 + Y1_forecast, data = updated_data) updated_data$Y2_forecast <- predict(model2, newdata = updated_data) }) ``` ### 4. **Dynamic Re-Estimation and Forecasting** - **Reactive Elements in Shiny**: - Use `reactive()` to define your models, ensuring they automatically update when new data is provided. - Example: ```R model1 <- reactive({ lm(Y1 ~ X1 + X2, data = input$data) }) forecast1 <- reactive({ predict(model1(), newdata = input$data) }) model2 <- reactive({ lm(Y2 ~ X3 + X4 + forecast1(), data = input$data) }) output$forecast <- renderTable({ data.frame(Y2_forecast = predict(model2(), newdata = input$data)) }) ``` ### 5. **Advanced Tips** - **Model Selection**: You can allow users to select different types of regression models within Shiny using `input` controls (e.g., radio buttons or dropdowns). - **Reproducibility**: Make sure your model re-estimation is consistent by using `set.seed()` if your models involve any randomness (e.g., in splitting data). - **Visualizing Results**: Use Shiny’s `plotOutput` and `renderPlot` to visualize the forecasts, which can help in understanding how well your model performs. ### 6. **Handling Multiple Models** - If you are dealing with many models and want to automate the process: - Store your models in a list or a similar structure. - Loop through them for re-estimation and forecasting. - Example: ```R models <- list() forecasts <- list() models[[1]] <- lm(Y1 ~ X1 + X2, data = your_data) forecasts[[1]] <- predict(models[[1]], newdata = your_data) models[[2]] <- lm(Y2 ~ X3 + X4 + forecasts[[1]], data = your_data) forecasts[[2]] <- predict(models[[2]], newdata = your_data) ``` ### **Summary** 1. Set up individual regression models using `lm()`. 2. Sequentially use the forecast from one model as an input to the next. 3. Re-estimate models automatically in Shiny when new data is available. 4. Use `reactive()` in Shiny to dynamically update and forecast. By following these steps, you'll be able to create a robust multi-equation forecasting model within your Shiny platform. If you need specific code examples or further guidance, feel free to share more details or code snippets!

  3. Hi Zach,
    I have this : model <-lm(EPSP ~ R + DV + RDV , data = data.frame) and 207 observations.
    I need to extract the coefficients of the variable (RDV) only for the 207 obs. since it is my dependent variable in another model. So, I need to create a new variable in my dataset using these coefficients.

    Thank you in advance,

    1. Hi Enas…What have you tried so far? Are you experiencing any errors in your code that we may review together?

Leave a Reply

Your email address will not be published. Required fields are marked *