R Extract Matrix Containing Regression Coefficients of lm (Example Code)

This page explains how to return the regression coefficients of a linear model estimation in the R programming language.

Introducing Example Data

data(iris)                                                             # Load iris data
head(iris)
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa
# 2          4.9         3.0          1.4         0.2  setosa
# 3          4.7         3.2          1.3         0.2  setosa
# 4          4.6         3.1          1.5         0.2  setosa
# 5          5.0         3.6          1.4         0.2  setosa
# 6          5.4         3.9          1.7         0.4  setosa

Example: Creating Data Matrix Containing Coefficients of Linear Model

coefficients_data <- summary(lm(Sepal.Length ~ ., iris))$coefficients  # Create data containing coefficients
coefficients_data                                                      # Print coefficients data
#                     Estimate Std. Error   t value     Pr(>|t|)
# (Intercept)        2.1712663 0.27979415  7.760227 1.429502e-12
# Sepal.Width        0.4958889 0.08606992  5.761466 4.867516e-08
# Petal.Length       0.8292439 0.06852765 12.100867 1.073592e-23
# Petal.Width       -0.3151552 0.15119575 -2.084418 3.888826e-02
# Speciesversicolor -0.7235620 0.24016894 -3.012721 3.059634e-03
# Speciesvirginica  -1.0234978 0.33372630 -3.066878 2.584344e-03

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top