Book contents

REGRESSION MODELLING FOR HEALTH PRACTITIONERS · CHAPTER 10

Regularisation and Variable Selection

Why regularise?

In regression modelling, including too many predictors can lead to overfitting, inflated variance and poor generalisation. Conversely, omitting important variables can bias estimates. Variable selection aims to find a parsimonious model that balances bias and variance. Classical approaches such as stepwise selection use p–values or information criteria (AIC/BIC) to add or remove variables. Information criteria penalise model complexity: AIC balances goodness of fit and complexity, while BIC imposes a stronger penalty and is more conservative. Cross–validation evaluates out–of–sample predictive performance and is widely used in machine learning. However, stepwise selection can produce unstable models. Regularisation methods such as ridge and lasso regression shrink coefficients towards zero to prevent overfitting and perform automatic variable selection.

Ridge regression

Ridge regression adds an ℓ2\ell_2 penalty to the least–squares criterion, minimising L(𝛃)=∑i=1n(yi−β0−𝐱i⊤𝛃)2+λ∑j=1pβj2,L(\boldsymbol{\beta}) = \sum_{i=1}^n (y_i - \beta_0 - \mathbf{x}_i^\top\boldsymbol{\beta})^2 + \lambda \sum_{j=1}^p \beta_j^2, where λ≥0\lambda\ge 0 controls the amount of shrinkage. Ridge regression does not set coefficients exactly to zero but can improve prediction when predictors are highly correlated.

Lasso and elastic net

The lasso adds an ℓ1\ell_1 penalty: L(𝛃)=∑i=1n(yi−β0−𝐱i⊤𝛃)2+λ∑j=1p|βj|.L(\boldsymbol{\beta}) = \sum_{i=1}^n (y_i - \beta_0 - \mathbf{x}_i^\top\boldsymbol{\beta})^2 + \lambda \sum_{j=1}^p |\beta_j|. The ℓ1\ell_1 penalty encourages sparsity: coefficients can be exactly zero, performing variable selection. The elastic net combines ℓ1\ell_1 and ℓ2\ell_2 penalties and is useful when predictors are highly correlated. Selecting the penalty parameter λ\lambda (and mixing parameter for elastic net) is typically done via cross–validation.

Information criteria and cross–validation

The Akaike information criterion (AIC) is defined as AIC=2k−2ln(L)\text{AIC} = 2k - 2\ln(L), where kk is the number of parameters and LL the maximised likelihood. Lower AIC indicates a better trade–off between fit and complexity. The Bayesian information criterion (BIC) uses BIC=kln(n)−2ln(L)\text{BIC} = k\ln(n) - 2\ln(L) and penalises large models more strongly. Cross–validation partitions the data into training and validation sets, fits the model on the training data and evaluates predictive error on the validation set. kk–fold cross–validation repeats this process across kk partitions and averages the error. Model selection should consider the study goal: prediction vs inference. Penalised methods typically prioritise prediction, whereas AIC/BIC may be preferred for inferential models.

Worked example: lasso for predicting body mass index

Assume a dataset with 20 lifestyle and demographic predictors of body mass index (BMI) among adults. We apply lasso regression to select a subset of predictors.

Worked example

R (glmnet)

library(glmnet)

# X: matrix of predictors, y: vector of BMI
set.seed(123)
cv_fit <- cv.glmnet(X, y, alpha = 1)  # alpha=1 for lasso
best_lambda <- cv_fit$lambda.min
lasso_fit <- glmnet(X, y, alpha = 1, lambda = best_lambda)
coef(lasso_fit)

Python (scikit-learn)

from sklearn.linear_model import LassoCV

lasso_cv = LassoCV(cv=10).fit(X, y)
print("Best alpha:", lasso_cv.alpha_)
print("Coefficients:", lasso_cv.coef_)

Julia

using GLMNet

cv = glmnetcv(X, y, alpha = 1)  # cross-validated lasso
best_lambda = cv.lambda_min
fit_lasso = glmnet(X, y, alpha = 1, lambda = best_lambda)
coef(fit_lasso)

Coefficients shrunk exactly to zero are predictors that contribute little to explaining BMI given the others. Compare the lasso model with a full multiple regression in terms of predictive accuracy (e.g., via cross–validation) and interpretability.

Interpretation

Exercises

Exercises

  1. Simulate a dataset with 10 predictors, only 3 of which truly affect the outcome. Fit a lasso model using cross–validation and identify the selected predictors. Compare with stepwise selection based on AIC.

  2. Implement ridge regression on the same dataset and compare coefficient shrinkage with lasso. Discuss scenarios where ridge is preferable.

  3. For a real dataset with many correlated predictors (e.g., gene expression data), apply elastic net regression and tune both the mixing parameter and penalty via cross–validation.

  4. Read about AIC and BIC. Using the Poisson example in Chapter 6, compare models with and without interaction terms via AIC and BIC. Which model is preferred?

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