glm() is base R’s generalized linear model function. For logistic regression, it models a binary outcome by estimating probabilities between 0 and 1 with maximum likelihood estimation. section 14.6.2
Use glm() for a practical binary-classification workflow:
- Provide a formula, binary outcome, predictor, family = "binomial", and data frame. section 14.6.2
- Inspect coefficients, p-values, deviance, AIC, and Fisher scoring iterations with summary(). section 14.6.2
- Evaluate fit with McFadden’s pseudo-R2, then generate response probabilities with predict(..., type = "response"). section 14.6.2
- Convert probabilities at a chosen threshold into classes and assess them with a confusion matrix, sensitivity, specificity, accuracy, and an ROC curve. section 14.6.2
Predicting NBA playoff qualification
A model can predict the binary variable playoffs2 from standardized points allowed: fit3 <- glm(playoffs2 ~ z_o_pts, family = "binomial", data = train). The predictor is statistically significant, and the model’s McFadden pseudo-R2 is 0.31. Predictions from test data can be thresholded at 0.50, producing 0.80 sensitivity, 0.63 specificity, 0.7375 accuracy, and an ROC AUC of 0.73. section 14.6.2
fit3 <- glm(playoffs2 ~ z_o_pts, family = "binomial", data = train) summary(fit3) ## ## Call: ## glm(formula = playoffs2 ~ z_o_pts, family = "binomial", data = train) ## ## Deviance Residuals: ## Min 1Q Median 3Q Max ## -2.17428 -0.77250 -0.01473 0.76694 2.13669 ## ## Coefficients: ## Estimate Std. Error z value Pr(>|z|) ## (Intercept) 0.1998 0.1727 1.157 0.247 ## z_o_pts -1.7210 0.2317 -7.428 0.00000000000011 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## (Dispersion parameter for binomial family taken to be 1) ## ## Null deviance: 304.98 on 219 degrees of freedom ## Residual deviance: 210.91 on 218 degrees of freedom ## AIC: 214.91 ## ## Number of Fisher Scoring iterations: 5
View the complete fit3 call and summary output, including coefficients, deviance, AIC, and Fisher scoring iterations. section 14.6.2
