A Complete Tutorial on Logistic Regression, and Inference in R

A Complete Tutorial on Logistic Regression, and Inference in R

Simple Logistic Regression

A Working Example

d = read.csv('Heart.csv')
m = glm(d$AHD ~ d$Chol, family = binomial)
m
Call:  glm(formula = d$AHD ~ d$Chol, family = binomial)Coefficients:
(Intercept)       d$Chol  
  -0.989822     0.003339Degrees of Freedom: 302 Total (i.e. Null);  301 Residual
Null Deviance:     418 
Residual Deviance: 415.8  AIC: 419.8

Inference

summary(m)
Call:
glm(formula = d$AHD ~ d$Chol, family = binomial)Deviance Residuals: 
   Min      1Q  Median      3Q     Max  
-1.573  -1.098  -1.020   1.236   1.419Coefficients:
             Estimate Std. Error z value Pr(>|z|)  
(Intercept) -0.989822   0.572848  -1.728    0.084 .
d$Chol       0.003339   0.002271   1.470    0.142  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1(Dispersion parameter for binomial family taken to be 1)Null deviance: 417.98  on 302  degrees of freedom
Residual deviance: 415.78  on 301  degrees of freedom
AIC: 419.78Number of Fisher Scoring iterations: 4

Adding More Predictor and Checking for Significance

d$ChestPain = as.integer(as.factor(d$ChestPain))
m3 = glm(d$AHD ~ d$Chol + d$Sex + d$RestBP + d$ChestPain + d$MaxHR, family = binomial)
summary(m3)
Call:
glm(formula = d$AHD ~ d$Chol + d$Sex + d$RestBP + d$ChestPain + 
    d$MaxHR, family = binomial)Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-2.5095  -0.7739  -0.2296   0.7185   2.8139Coefficients:
             Estimate Std. Error z value Pr(>|z|)    
(Intercept)  0.811483   1.619947   0.501 0.616419    
d$Chol       0.007352   0.002903   2.532 0.011327 *  
d$Sex        2.022295   0.366336   5.520 3.38e-08 ***
d$RestBP     0.029476   0.008698   3.389 0.000702 ***
d$ChestPain -0.976384   0.174952  -5.581 2.39e-08 ***
d$MaxHR     -0.042467   0.007913  -5.366 8.03e-08 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1(Dispersion parameter for binomial family taken to be 1)Null deviance: 417.98  on 302  degrees of freedom
Residual deviance: 282.24  on 297  degrees of freedom
AIC: 294.24Number of Fisher Scoring iterations: 5
exp(cbind(OR = coef(m3), confint.default(m3)))
OR      2.5 %     97.5 %
(Intercept) 2.2512446 0.09408516 53.8671819
d$Chol      1.0073793 1.00166344  1.0131278
d$Sex       7.5556425 3.68507064 15.4916252
d$RestBP    1.0299151 1.01250545  1.0476241
d$ChestPain 0.3766706 0.26732673  0.5307392
d$MaxHR     0.9584225 0.94367191  0.9734037

Conclusion

Leave a Reply

Close Menu