Book contents Open the calibration lab

REGRESSION MODELLING FOR HEALTH PRACTITIONERS · CHAPTER 13

Model Validation

Any predictive model should be evaluated on data not used for its development. Without validation, a model may appear to perform well simply because it overfits the training data. This chapter describes strategies for assessing model performance and avoiding overfitting.

Why Validate Models?

Model validation examines how well a fitted model generalises to new data. In health research this is critical, because a model that perfectly predicts outcomes in the training cohort may perform poorly in another population. Internal validation assesses predictive accuracy using resampling methods such as cross‑validation and bootstrapping, while external validation applies the model to an independent dataset. A validated model helps clinicians and researchers trust predictions and supports regulatory approval of risk scores.

Cross‑Validation

K‑fold cross‑validation is a widely used method for estimating prediction error. The dataset is randomly divided into KK roughly equal parts. A model is trained on K−1K-1 folds and evaluated on the held‑out fold. This process is repeated so that each fold serves as validation once, and the performance estimates are averaged over the KK iterations. Because each training set contains only (K−1)/K(K-1)/K of the data, cross‑validation error estimates may be biased upward; the bias decreases as KK increases but the variance may increase. Leave‑one‑out cross‑validation (K=nK=n) uses each observation as its own validation set but can be computationally intensive.

Bootstrap

Bootstrapping is a resampling technique that repeatedly draws samples with replacement from the original dataset. Each bootstrap sample is the same size as the original data but contains some observations multiple times and leaves others out. A model is fitted on each bootstrap sample, and its performance is assessed either on the bootstrap sample itself (optimistic) or on the out‑of‑bag observations. The distribution of the estimated performance can be used to estimate optimism‑corrected prediction error, construct confidence intervals or assess the stability of variable selection.

Worked Example

We illustrate internal validation using a logistic regression model to predict 30‑day readmission after hospital discharge. The dataset readmission.csv contains 500 observations with binary outcome Readmit and predictors Age, Length_stay and Comorbidity.

Worked example

Python example. Supply a dataset with both outcome classes represented in every validation fold.

from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold, cross_val_score
import pandas as pd

data = pd.read_csv("readmission.csv")
X = data[["Age", "Length_stay", "Comorbidity"]]
y = data["Readmit"]
folds = StratifiedKFold(10, shuffle=True, random_state=42)
auc = cross_val_score(LogisticRegression(max_iter=1000), X, y,
                      cv=folds, scoring="roc_auc")
print(auc.mean())

For a bootstrap optimism correction, fit the entire modelling procedure on the original data and calculate its apparent AUC. For each bootstrap sample, refit the procedure, score that fit on its bootstrap sample and on the original sample, and subtract the latter AUC from the former. Subtract the mean of these differences from the original apparent AUC. Repeat preprocessing and selection inside each resample.

Interpretation

Choose the number of folds KK by considering the trade‑off between bias and variance: smaller KK (e.g., 5 or 10) provides relatively unbiased error estimates with moderate variance, whereas leave‑one‑out cross‑validation may have high variance. Bootstrapping provides optimism‑corrected estimates and can be used to assess the stability of variable selection.

Exercises

  1. Using the readmission.csv dataset, implement 5‑fold and 10‑fold cross‑validation for a logistic regression model. Compare the estimated AUC values.

  2. Implement the bootstrap procedure to estimate the optimism‑corrected AUC. How does it compare to the cross‑validated AUC?

  3. Discuss advantages and disadvantages of cross‑validation versus bootstrap for internal validation.

  4. Split your dataset into training and test sets (70/30). Fit a model on the training set and report its performance on the test set. Compare this single split to cross‑validation and bootstrap results.

Code examples use illustrative datasets. Supply data with the named columns to run them.