how to Extract Regression Coefficients from glm() in R

The glm() function in R is used to create generalized linear models. The coefficients of the model can be extracted by using the command coef(), which returns the estimated coefficients of the model, along with the standard errors and the p-values associated with each coefficient. Additionally, the summary() command can be used to obtain an overall summary of the model, which includes the coefficients, standard errors and p-values.


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

Method 1: Extract All Regression Coefficients

model$coefficients

Method 2: Extract Regression Coefficient for Specific Variable

model$coefficients['my_variable']

Method 3: Extract All Regression Coefficients with Standard Error, Z Value & P-Value

summary(model)$coefficients

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

Example: Extract Regression Coefficients from glm() in R

Suppose we fit a using the Default dataset from the ISLR package:

#load dataset
data <- ISLR::Default

#view first six rows of data
head(data)

  default student   balance    income
1      No      No  729.5265 44361.625
2      No     Yes  817.1804 12106.135
3      No      No 1073.5492 31767.139
4      No      No  529.2506 35704.494
5      No      No  785.6559 38463.496
6      No     Yes  919.5885  7491.559

#fit logistic regression model
model <- glm(default~student+balance+income, family='binomial', data=data)

#view summary of logistic regression model
summary(model)

Call:
glm(formula = default ~ student + balance + income, family = "binomial", 
    data = data)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-2.4691  -0.1418  -0.0557  -0.0203   3.7383  

Coefficients:
              Estimate Std. Error z value Pr(>|z|)    
(Intercept) -1.087e+01  4.923e-01 -22.080  < 2e-16 ***
studentYes  -6.468e-01  2.363e-01  -2.738  0.00619 ** 
balance      5.737e-03  2.319e-04  24.738  < 2e-16 ***
income       3.033e-06  8.203e-06   0.370  0.71152    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 2920.6  on 9999  degrees of freedom
Residual deviance: 1571.5  on 9996  degrees of freedom
AIC: 1579.5

Number of Fisher Scoring iterations: 8

We can type model$coefficients to extract all of the regression coefficients in the model:

#extract all regression coefficients
model$coefficients

  (Intercept)    studentYes       balance        income 
-1.086905e+01 -6.467758e-01  5.736505e-03  3.033450e-06

We can also type model$coefficients[‘balance’] to extract the regression coefficient for the balance variable only:

#extract coefficient for 'balance'
model$coefficients['balance']

    balance 
0.005736505

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

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

                 Estimate   Std. Error    z value      Pr(>|z|)
(Intercept) -1.086905e+01 4.922555e-01 -22.080088 4.911280e-108
studentYes  -6.467758e-01 2.362525e-01  -2.737646  6.188063e-03
balance      5.736505e-03 2.318945e-04  24.737563 4.219578e-135
income       3.033450e-06 8.202615e-06   0.369815  7.115203e-01

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

#view p-value for balance variable
summary(model)$coefficients['balance', 'Pr(>|z|)']

[1] 4.219578e-135

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(>|z|)']

  (Intercept)    studentYes       balance        income 
4.911280e-108  6.188063e-03 4.219578e-135  7.115203e-01 

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 output.

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

x