2026-05-11
Understand the components of calculations for logistic regression diagnostics
Plot and determine observations where regression does not fit well or are influential using specific diagnostic values
Overall measurements of fit
How well does the fitted logistic regression model predict the outcome?
Different ways to measure the answer to this question
| Measure of fit | Hypothesis tested? | Equation | R code |
|---|---|---|---|
| Pearson residual | Yes | \(X^2=\sum_{j=1}^{J}{r\left(Y_j,{\widehat{\pi}}_j\right)^2}\) | Not given |
| Hosmer-Lemeshow test | Yes | \(\widehat{C}=\sum_{k=1}^{g}\frac{\left(o_k-n_k^\prime{\bar{\pi}}_k\right)^2}{n_k^\prime{\bar{\pi}}_k(1-{\bar{\pi}}_k)}\) | hoslem.test() |
| AUC-ROC | Kinda | Not given | auc(observed, predicted) |
| AIC | Only to compare models | \(AIC = -2 \cdot \text{log-likelihood} + 2q\) | AIC(model_name) |
| BIC | Only to compare models | \(BIC = -2 \cdot \text{log-likelihood} + q\text{log}(n)\) | BIC(model_name) |
Numerical problems
Different numerical problems to look out for
Linear Regression Assumptions
Logistic Regression Assumptions
Measures at the observation level for model diagnostics!
Model diagnostics of logistic regression can be assessed by checking for outliers:
Model diagnostics of logistic regression can be assessed by checking how influential an observation is:
\[\text{deviance residual}_i = \text{sign}(y_i - \widehat{\pi}_i) \cdot \sqrt{-2\left[y_i\log\left(\dfrac{y_i}{\widehat{\pi}_i}\right) + (1-y_i)\log\left(\dfrac{1-y_i}{1-\widehat{\pi}_i}\right)\right]}\]
\[d_i = \frac{\text{deviance residual}_i}{\sqrt{1-h_i}}\]
\[D_i = \dfrac{d_i^2 h_i}{(k-1)(1-h_i)^2}\]
\[\Delta \widehat{\beta}_j = \frac{\widehat{\beta}_j - \widehat{\beta}_{j(-i)}}{SE(\widehat{\beta}_{j(-i)})}\]
Measures the impact on the predicted probability (the “fit”)
It is the number of standard deviations the fitted value changes when an observation is removed
Rule of Thumb: Flag if \(|DFFITS| > 2\sqrt{k/n}\)
Outcome variable: any fracture in the first year of follow up (FRACTURE: 0 or 1)
Risk factor/variable of interest: history of prior fracture (PRIORFRAC: 0 or 1)
Potential confounder or effect modifier: age (AGE, a continuous variable)
Fitted model with interactions: \[\begin{aligned} \text{logit}\left(\widehat\pi(\mathbf{X})\right) & = \widehat\beta_0 &+ &\widehat\beta_1\cdot I(\text{PF}) & + &\widehat\beta_2\cdot Age& + &\widehat\beta_3 \cdot I(\text{PF}) \cdot Age \\ \text{logit}\left(\widehat\pi(\mathbf{X})\right) & = -1.376 &+ &1.002\cdot I(\text{PF})& + &0.063\cdot Age& -&0.057 \cdot I(\text{PF}) \cdot Age \end{aligned}\]
augment()?# Augment for residuals
library(broom)
aug_glow = augment(glow_m3, type.predict = "response", type.residuals = "deviance")
# Calculate influence measures (Cook's D, DFFITS, DFBETAs)
infl = influence.measures(glow_m3)
dx_glow = cbind(aug_glow, as.data.frame(infl$infmat)) %>%
dplyr::select(-c(cook.d, cov.r, hat)).fitted: Estimated probability of events (\(\widehat{\pi}\)).resid: Deviance residual.hat: Leverage.sigma: Standard error of the residuals.cooksd: Cook’s distance.std.resid: Standardized deviance residualdfb.1_: Change in coef. estimate for interceptdfb.prrY: Change in coef. estimate for prior fracturedfb.age_c: Change in coef. estimate for agedfb.pY:_: Change in coef. estimate for interaction between age and prior fracturedffit: Change in predicted probability of fractureRows: 500
Columns: 14
$ fracture <fct> No, No, No, No, No, No, No, N…
$ priorfrac <fct> No, No, Yes, No, No, Yes, No,…
$ age_c <dbl> -7, -4, 19, 13, -8, -2, 15, 1…
$ .fitted <dbl> 0.1402159, 0.1643855, 0.43133…
$ .resid <dbl> -0.5496799, -0.5993128, -1.06…
$ .hat <dbl> 0.003811004, 0.003143864, 0.0…
$ .sigma <dbl> 1.027863, 1.027807, 1.027018,…
$ .cooksd <dbl> 0.0001565683, 0.0001555953, 0…
$ .std.resid <dbl> -0.5507303, -0.6002571, -1.07…
$ dfb.1_ <dbl> -2.632841e-02, -2.994570e-02,…
$ dfb.prrY <dbl> 0.01472703, 0.01675039, -0.01…
$ dfb.ag_c <dbl> 2.185329e-02, 1.536230e-02, -…
$ `dfb.pY:_` <dbl> -0.013507094, -0.009495139, -…
$ dffit <dbl> -0.03314002, -0.03279756, -0.…
The plots allow us to identify those covariate patterns that are…
Poorly fit
Influential on estimated coefficients
We plot the diagnostic values against the predicted probabilities to see if there are any patterns in the poorly fit or influential observations
priorfrac age_c .fitted dfb.prrY
377 Yes 3 0.4113188 0.09307664
383 Yes -8 0.3977192 0.14436585
385 Yes 3 0.4113188 0.09307664
387 Yes 1 0.4088354 0.10203872
404 Yes -7 0.3989493 0.13946650
409 Yes -3 0.4038826 0.12039584
411 Yes -2 0.4051190 0.11574619
412 Yes -4 0.4026474 0.12509002
415 Yes -6 0.4001808 0.13462262
417 Yes -1 0.4063566 0.11113848
422 Yes -2 0.4051190 0.11574619
429 Yes -7 0.3989493 0.13946650
436 Yes -4 0.4026474 0.12509002
444 Yes -7 0.3989493 0.13946650
453 Yes 1 0.4088354 0.10203872

priorfrac age_c .fitted dffit
3 Yes 19 0.4313389 -0.1813034
169 Yes 21 0.4338587 -0.2014239
178 No 20 0.4686423 -0.1845408
192 Yes -12 0.3928116 -0.1793038
228 Yes -12 0.3928116 -0.1793038
280 No 21 0.4842350 -0.1977886
328 Yes 21 0.4338587 -0.2014239
383 Yes -8 0.3977192 0.1983474
398 No 20 0.4686423 0.2020885
404 Yes -7 0.3989493 0.1875104
408 Yes 20 0.4325984 0.2326503
421 Yes 15 0.4263101 0.1794865
424 Yes 20 0.4325984 0.2326503
429 Yes -7 0.3989493 0.1875104
444 Yes -7 0.3989493 0.1875104
Do a data quality check
If we have quite a lot of outliers that are highly influential, potential reasons can be considered:
The link used in logistic regression model is not appropriate for outcome
One or more important covariates missing in the model
Methods: Our final logistic regression model consisted of the outcome, fracture, and predictors including prior fracture, age, and their interaction. To assess the overall model fit, we calculated the AUC-ROC. We identified observations with high standardized deviance residual (absolute value greater than 3), Cook’s distance (greater than 1), and change in predicted probability (greater than 0.179). No identified observations were omitted.
Results: The AUC-ROC was 0.68. Out of 500 observations, we identified 26 with high change in predicted probability.
Discussion:
AUC-ROC low: Included covariates were pre-determined
Influential points were kept in because all observations were within feasible range of the predictors and outcome. (we could try age-squared and see if that helps AUC and/or diagnostics)
Lesson 13: Model Diagnostics