# Extract Regression Coefficients of Linear Model in R (Example)

This tutorial illustrates how to return the regression coefficients of a linear model estimation in R programming.

The content of the tutorial looks like this:

So without further ado, let’s get started:

## Constructing Example Data

We use the following data as basement for this tutorial:

```set.seed(87634) # Create random example data x1 <- rnorm(1000) x2 <- rnorm(1000) + 0.3 * x1 x3 <- rnorm(1000) + 0.1 * x1 + 0.2 * x2 x4 <- rnorm(1000) + 0.2 * x1 - 0.3 * x3 x5 <- rnorm(1000) - 0.1 * x2 + 0.1 * x4 y <- rnorm(1000) + 0.1 * x1 - 0.2 * x2 + 0.1 * x3 + 0.1 * x4 - 0.2 * x5 data <- data.frame(y, x1, x2, x3, x4, x5) head(data) # Head of data # y x1 x2 x3 x4 x5 # 1 -0.6441526 -0.42219074 -0.12603789 -0.6812755 0.9457604 -0.39240211 # 2 -0.9063134 -0.19953976 -0.35341624 1.0024131 1.3120547 0.05489608 # 3 -0.8873880 0.30450638 -0.58551780 -1.1073109 -0.2047048 0.44607502 # 4 0.4567184 1.33299913 -0.05512412 -0.5772521 0.3476488 1.65124595 # 5 0.6631039 -0.36705475 -0.26633088 1.0520141 -0.3281474 0.77052209 # 6 1.3952174 0.03528151 -2.43580550 -0.6727582 1.8374260 1.06429782```

The previously shown RStudio console output shows the structure of our example data – It’s a data frame consisting of six numeric columns. The first variable y is the outcome variable. The remaining variables x1-x5 are the predictors.

## Example: Extracting Coefficients of Linear Model

In this Example, I’ll illustrate how to estimate and save the regression coefficients of a linear model in R. First, we have to estimate our statistical model using the lm and summary functions:

```summary(lm(y ~ ., data)) # Estimate model # Call: # lm(formula = y ~ ., data = data) # # Residuals: # Min 1Q Median 3Q Max # -2.9106 -0.6819 -0.0274 0.7197 3.8374 # # Coefficients: # Estimate Std. Error t value Pr(>|t|) # (Intercept) -0.01158 0.03204 -0.362 0.717749 # x1 0.10656 0.03413 3.122 0.001847 ** # x2 -0.17723 0.03370 -5.259 1.77e-07 *** # x3 0.11174 0.03380 3.306 0.000982 *** # x4 0.09933 0.03295 3.015 0.002638 ** # x5 -0.24871 0.03323 -7.485 1.57e-13 *** # --- # Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 # # Residual standard error: 1.011 on 994 degrees of freedom # Multiple R-squared: 0.08674, Adjusted R-squared: 0.08214 # F-statistic: 18.88 on 5 and 994 DF, p-value: < 2.2e-16```

The previous output of the RStudio console shows all the estimates we need. However, the coefficient values are not stored in a handy format. Let’s therefore convert the summary output of our model into a data matrix:

```matrix_coef <- summary(lm(y ~ ., data))\$coefficients # Extract coefficients in matrix matrix_coef # Return matrix of coefficients # Estimate Std. Error t value Pr(>|t|) # (Intercept) -0.01158450 0.03203930 -0.3615716 7.177490e-01 # x1 0.10656343 0.03413045 3.1222395 1.846683e-03 # x2 -0.17723211 0.03369896 -5.2592753 1.770787e-07 # x3 0.11174223 0.03380415 3.3055772 9.817042e-04 # x4 0.09932518 0.03294739 3.0146597 2.637990e-03 # x5 -0.24870659 0.03322673 -7.4851370 1.572040e-13```

The previous R code saved the coefficient estimates, standard errors, t-values, and p-values in a typical matrix format.

Now, we can apply any matrix manipulation to our matrix of coefficients that we want. For instance, we may extract only the coefficient estimates by subsetting our matrix:

```my_estimates <- matrix_coef[ , 1] # Matrix manipulation to extract estimates my_estimates # Print estimates # (Intercept) x1 x2 x3 x4 x5 # -0.01158450 0.10656343 -0.17723211 0.11174223 0.09932518 -0.24870659```

That’s it. Now you can do whatever you want with your regression output!

## Video & Further Resources

I have recently released a video on my YouTube channel, which shows the R codes of this tutorial. Please find the video below:

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party. If you accept this notice, your choice will be saved and the page will refresh.

Besides the video, you might have a look at the related articles of this website.

This tutorial explained how to extract the coefficient estimates of a statistical model in R. Please let me know in the comments section, in case you have additional questions.

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

#### 8 Comments.Leave new

• Brent
September 5, 2021 8:41 pm

Hi,
How do I do the same with a simple linear regression output to compare both tables?

Thank you

• Hi Brent,

This depends on the values/estimates you want to compare. You may run the code shown in this tutorial to as many different linear regression models as you want.

Regards

Joachim

• Kunal
March 4, 2022 3:24 am

Hi Joachim,
I want to use those intercepts in an expression, how do I do that?

• Hey Kunal,

Do you still need help with this question? I just got back from vacation and couldn’t reply earlier.

Regards,
Joachim

• Eric S
March 31, 2022 11:49 pm

Hi Joachim,

How do you extract the information at the bottom of the summary() output:

Residual standard error: 1.011 on 994 degrees of freedom
Multiple R-squared: 0.08674
F-statistic: 18.88 on 5 and 994 DF, p-value: < 2.2e-16
?

Is there a way to view the structure of functions like summary() to ascertain how the numbers are stored in them, in general?

Thanks,
Eric

• Hey Eric,

Please have a look at the following three tutorials. They explain how to extract those values:

Regards,
Joachim

• Christian Koko
September 20, 2022 5:46 pm

Hey Eric,
Do you know how I can use the estimated regression coefficients to predict another dataset?