Book contents

REGRESSION MODELLING FOR HEALTH PRACTITIONERS · CHAPTER 9

Mixed Effects Models

Why random effects?

Data collected from health studies often exhibit hierarchical or longitudinal structure: repeated measurements on the same patient, patients nested within hospitals, or clusters of participants recruited from different clinics. Standard regression models assume independent observations; when this assumption is violated, inferences can be biased and standard errors understated. Mixed effects models incorporate both fixed effects (parameters associated with the entire population) and random effects (parameters that vary across clusters or subjects). They allow us to model correlation within clusters while estimating population‐level associations.

Linear mixed models

Suppose we have repeated measurements of systolic blood pressure for each patient over several clinic visits. Let YijY_{ij} be the blood pressure for patient ii at visit jj, and tijt_{ij} the time since baseline. A linear mixed model with a random intercept and slope may be written as Yij=β0+b0i+(β1+b1i)tij+β2Treatmenti+εij,b0i,b1i∼N(0,𝐃),εij∼N(0,σ2),\begin{aligned} Y_{ij} &= \beta_0 + b_{0i} + (\beta_1 + b_{1i})\,t_{ij} + \beta_2 \operatorname{Treatment}_i + \varepsilon_{ij},\\ b_{0i}, b_{1i} &\sim N(0, \mathbf{D}),\quad \varepsilon_{ij} \sim N(0, \sigma^2), \end{aligned} where b0ib_{0i} and b1ib_{1i} are patient‐specific random intercepts and slopes with covariance matrix 𝐃\mathbf{D}. The fixed effects β0\beta_0 and β1\beta_1 represent the average intercept and slope, while β2\beta_2 is the average treatment effect.

Generalized linear mixed models

When the outcome is binary or count, a generalized linear mixed model (GLMM) extends mixed modelling by including a link function. For example, to model repeated binary outcomes (e.g., daily adherence to medication), we can specify logit(Pr(Yij=1))=β0+b0i+β1xij,\operatorname{logit}\bigl(\Pr(Y_{ij}=1)\bigr) = \beta_0 + b_{0i} + \beta_1 x_{ij}, with a random intercept b0ib_{0i} capturing subject–specific propensity. Similarly, a Poisson GLMM can be used for clustered count data.

Worked example: random intercept model

Assume we have a dataset of 200 patients from 10 hospitals (clusters) and measure their cholesterol level. We are interested in the effect of diet (Diet, 1 = new diet, 0 = standard) on cholesterol while accounting for variation between hospitals.

Worked example

R (lme4)

library(lme4)

# df has: Cholesterol, Diet (0/1), Hospital (factor)
fit_mixed <- lmer(Cholesterol ~ Diet + (1 | Hospital), data = df)
summary(fit_mixed)
fixef(fit_mixed)     # fixed effects
ranef(fit_mixed)     # random effects
confint(fit_mixed)

Python (statsmodels)

import statsmodels.api as sm
import statsmodels.formula.api as smf

# df with Cholesterol, Diet, Hospital
model = smf.mixedlm('Cholesterol ~ Diet', df, groups=df['Hospital'])
result = model.fit()
print(result.summary())

Julia

using DataFrames, MixedModels

# df with Cholesterol, Diet, Hospital
fm = @formula(Cholesterol ~ Diet + (1 | Hospital))
fit_mixed = fit(MixedModel, fm, df)
coeftable(fit_mixed)
ranef(fit_mixed)

The fixed effect for Diet estimates the average difference in cholesterol between diet groups across all hospitals. The random intercepts model hospital‐specific baselines; their estimated variance indicates the magnitude of between‐hospital heterogeneity.

Interpretation

Exercises

Exercises

  1. Simulate a linear mixed model with a random intercept for clusters (e.g., schools) and a treatment effect. Fit the model in R, Python or Julia and recover the true parameters.

  2. Using a dataset of repeated measures (e.g., longitudinal blood pressure), fit a random intercept and random slope model. Assess whether adding a random slope significantly improves model fit (e.g., likelihood ratio test).

  3. Fit a logistic mixed model for a binary outcome with patient‐level random intercepts. Interpret the fixed effect estimates and random effects variance.

  4. Discuss the assumptions underlying mixed models. What happens if the random effects distribution is misspecified? Suggest strategies for model checking.

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