How to extract the regression coefficients, standard error of coefficients, t scores, and p-values from a regression model in R?

R ProgrammingServer Side ProgrammingProgramming

Regression analysis output in R gives us so many values but if we believe that our model is good enough, we might want to extract only coefficients, standard errors, and t-scores or p-values because these are the values that ultimately matters, specifically the coefficients as they help us to interpret the model. We can extract these values from the regression model summary with delta $ operator.

Example

Consider the below data −

> set.seed(99)
> x1<-rpois(50,2)
> x2<-rpois(50,10)
> x3<-rpois(50,25)
> x4<-rnorm(50,1)
> x5<-rnorm(50,2.5)
> x6<-rnorm(50,1.5)
> x7<-runif(50,2,20)
> y<-sample(1:1000,50,replace=TRUE)

Creating the regression model −

> Regression_Model<-lm(y~x1+x2+x3+x4+x5+x6+x7)

Getting the output of the model &minus

> summary(Regression_Model)
Call:
lm(formula = y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7)
Residuals:
Min 1Q Median 3Q Max
-580.06 -268.03 71.54 248.45 450.20
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 885.966696 336.412681 2.634 0.0118 *
x1 -33.463082 34.748162 -0.963 0.3411
x2  -8.056429 13.866217 -0.581 0.5643
x3  -0.003585  9.641347  0.000 0.9997
x4 -62.751405 47.195104 -1.330 0.1908
x5 -53.421667 40.706602 -1.312 0.1965
x6 -46.645285 41.017385 -1.137 0.2619
x7   7.705532  8.543121  0.902 0.3722
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 309.4 on 42 degrees of freedom
Multiple R-squared: 0.1242, Adjusted R-squared: -0.02181
F-statistic: 0.8506 on 7 and 42 DF, p-value: 0.5526

Extracting all regression coefficients, standard error of coefficients, t scores, and p-values from the model −

> summary(Regression_Model)$coefficients
Estimate Std. Error t value Pr(>|t|)
(Intercept) 885.966696369 336.412681 2.6335710454 0.01177664
x1 -33.463081817 34.748162 -0.9630173179 0.34105093
x2  -8.056428960 13.866217 -0.5810113022 0.56433788
x3  -0.003584907  9.641347 -0.0003718264 0.99970509
x4 -62.751404764 47.195104 -1.3296168453 0.19082124
x5 -53.421667389 40.706602 -1.3123588063 0.19652614
x6 -46.645285482 41.017385 -1.1372076842 0.26189795
x7   7.705532157  8.543121  0.9019575482 0.37222303

Extracting individual regression coefficients, standard error of coefficients, t scores, and p-values from the model −

> summary(Regression_Model)$coefficients[1,2]
[1] 336.4127
> summary(Regression_Model)$coefficients[1,1]
[1] 885.9667
> summary(Regression_Model)$coefficients[1,4]
[1] 0.01177664
> summary(Regression_Model)$coefficients[3,1]
[1] -8.056429
> summary(Regression_Model)$coefficients[7,1]
[1] -46.64529
> summary(Regression_Model)$coefficients[7,4]
[1] 0.261898
> summary(Regression_Model)$coefficients[8,4]
[1] 0.372223
> summary(Regression_Model)$coefficients[1,3]
[1] 2.633571
> summary(Regression_Model)$coefficients[2,1]
[1] -33.46308
> summary(Regression_Model)$coefficients[2,2]
[1] 34.74816
> summary(Regression_Model)$coefficients[2,4]
[1] 0.3410509
> summary(Regression_Model)$coefficients[4,4]
[1] 0.9997051
> summary(Regression_Model)$coefficients[4,3]
[1] -0.0003718264
> summary(Regression_Model)$coefficients[5,4]
[1] 0.1908212
> summary(Regression_Model)$coefficients[5,1]
[1] -62.7514
> summary(Regression_Model)$coefficients[5,2]
[1] 47.1951
> summary(Regression_Model)$coefficients[6,1]
[1] -53.42167
> summary(Regression_Model)$coefficients[6,4]
[1] 0.1965261
raja
Published on 11-Aug-2020 09:16:54
Advertisements