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 be the blood pressure for patient at visit , and the time since baseline. A linear mixed model with a random intercept and slope may be written as where and are patient‐specific random intercepts and slopes with covariance matrix . The fixed effects and represent the average intercept and slope, while 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 with a random intercept 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
Variance components. Examine the variance of random effects to understand the amount of clustering. A large cluster variance suggests that ignoring clustering could bias results.
Prediction. Mixed models allow prediction of subject‐specific effects (BLUPs), which can be useful for personalised medicine.
Model complexity. Adding random slopes increases flexibility but requires sufficient data per cluster. Avoid overfitting with too many random parameters.
Interpretation. Random effects represent deviations from the population average; they are assumed to arise from a normal distribution. Inferences about fixed effects remain population‐level.
Exercises
Exercises
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.
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).
Fit a logistic mixed model for a binary outcome with patient‐level random intercepts. Interpret the fixed effect estimates and random effects variance.
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.