Mixed-Effects (Random-Effects) Models for Longitudinal RWE
A regression framework for repeated/clustered outcomes that adds subject- or cluster-specific random effects to fixed-effect covariates, modeling within-subject correlation and yielding subject-specific (conditional) effects under a missing-at-random likelihood.
In plain language
A mixed-effects model is a statistical tool for studying how patients change over time when you measure the same person more than once. It splits the math into two layers: one layer captures the average pattern across all patients (for example, how much a drug lowers blood sugar on average), and a second layer gives each patient their own personal baseline and their own personal rate of change. This separation lets you ask the right question -- how much did a typical patient improve? -- without a high-baseline patient's numbers drowning out a low-baseline patient's numbers.
Mixed-effects (random-effects / multilevel) models
extend ordinary regression to data where outcomes are measured repeatedly within the same patient over time, or are clustered within sites/providers/regions, so that observations are not independent. The model partitions variation into fixed effects (population-level covariate coefficients — the treatment contrast, time trend, baseline covariates) and random effects (subject- or cluster-specific deviations, e.g., a random intercept for each `person_id`, optionally a random slope on time). For a continuous outcome the canonical specification is the linear mixed model (LMM) of Laird & Ware: y_ij = X_ijbeta + Z_ijb_i + e_ij, with b_i ~ N(0, G) and e_ij ~ N(0, R). For binary/count repeated outcomes (e.g., monthly hospitalization, exacerbation counts) the analogue is the generalized linear mixed model (GLMM) with a logit/log link. Estimation is by (restricted) maximum likelihood — REML for the LMM, Laplace/adaptive Gauss-Hermite quadrature or pseudo-likelihood for the GLMM.
Core conceptual distinction
(the estimand). The estimand a mixed model targets is subject-specific (conditional): the fixed-effect coefficient is the change in the same patient's (or same cluster's) outcome, holding that patient's random effect fixed. This is the sharpest single point of confusion in longitudinal RWE. (1) Mixed model vs GEE (population-average). A generalized estimating equation models the marginal/population-average mean and treats within-subject correlation as a nuisance working structure. For the identity and log links the conditional and marginal coefficients coincide, but for the logit link they do not — a GLMM odds ratio is larger in magnitude than the corresponding GEE odds ratio (attenuation by the random-effect variance), and the two answer different questions ("effect for a typical patient" vs "effect on the population prevalence"). Pick the estimand in the protocol, not after seeing both. (2) Random effects vs cluster-robust / fixed-effects regression. Random effects assume the cluster effect is uncorrelated with the covariates (an exchangeability assumption a Hausman-type comparison probes); if site-level confounding correlates with exposure, a within-cluster (fixed-effects) estimator or design-based control is safer. (3) Mixed model vs MMRM. The mixed model for repeated measures (MMRM) is the same likelihood machinery with time treated as a categorical fixed effect and an unstructured residual covariance instead of random slopes — the FDA/EMA-favored primary analysis for continuous endpoints in registrational longitudinal trials; the random-coefficient growth-curve form is more parsimonious but imposes a functional shape on the trajectory.
Interpreting the output
Consider the worked example: five patients measured at months 0, 3, and 6. Drug arm patients (101, 102) drop an average of 1.10 HbA1c points over 6 months; comparator patients (103, 104) drop an average of 0.20 points. The linear mixed model with random intercepts and random slopes yields an estimated fixed-effect treatment difference of approximately −0.90 HbA1c points (95% CI −1.6 to −0.2) favoring drug.
Formal interpretation: The fixed-effect coefficient of −0.90 is a subject-specific (conditional) estimate — the expected additional HbA1c reduction for a given patient on the drug arm compared with what that same patient's trajectory would have been on the comparator, holding that patient's random intercept and slope fixed. It is not a comparison between different patients. The random-intercept variance quantifies how much patients differ at baseline (patient 101 starts at 9.2, patient 102 at 7.4); the random-slope variance quantifies how much their rates of change differ.
Practical interpretation: The drug is estimated to lower HbA1c by about 0.9 points more than the comparator in a typical patient over 6 months. This subject-specific framing is appropriate when the clinical question is "what will happen to this patient?" If the question is instead "what is the average effect across the population?" — the HTA or payer's usual target — a GEE (marginal model) answers it directly, and for a Gaussian identity-link outcome like HbA1c the two coefficient values coincide; for non-linear links (logit, log) the conditional mixed-model coefficient is larger in magnitude than the corresponding marginal GEE coefficient.
Pros, cons, and trade-offs
(specific and comparative). - vs GEE (population-average models): Mixed models use all available person-time under a likelihood that is valid under missing-at-random (MAR) dropout, whereas standard GEE with an independence or exchangeable working correlation is only consistent under the stronger missing-completely-at-random (MCAR) assumption (unless weighted). Mixed models also estimate between- and within-subject variance components and give individual predictions (BLUPs). Cost: results are sensitive to the random-effects distribution and link, the conditional OR is easily misread as marginal, and convergence/quadrature failures are common with sparse binary clusters. Prefer the mixed model when the question is subject-specific, when dropout is plausibly MAR, or when you need variance decomposition or shrinkage prediction. - vs MMRM (unstructured-covariance repeated measures): The random-coefficient mixed model borrows strength across the trajectory and handles unbalanced/irregular visit times naturally — an advantage in RWE where visits are not on a protocol grid. Cost: it assumes the modeled growth shape (linear/spline) is correct; MMRM makes no such assumption but needs roughly balanced nominal visits and can be over-parameterized in small samples. Prefer MMRM for registrational continuous endpoints with scheduled visits; prefer the growth-curve mixed model for irregular real-world follow-up. - vs cluster-robust / GEE-with-robust-SE on a marginal model: Robust ("sandwich") variances fix only the standard errors of a misspecified-correlation model; the mixed model instead models the correlation, which is more efficient when the variance structure is approximately right and is what enables MAR-valid likelihood inference. Cost: a wrong random-effects structure biases both point estimates and SEs, so sandwich correction of a well-specified marginal model is sometimes the more robust choice. Prefer mixed models when you trust the hierarchical structure; pair the GLMM with profile/Kenward-Roger or bootstrap inference for small numbers of clusters.
When to use
(decision rules). Repeated continuous, binary, or count outcomes per patient (PRO/HRQoL scores over visits, serial labs/HbA1c, monthly utilization or cost-adjacent counts); outcomes clustered within hospitals, physicians, payers, or geographies; analyses needing individual-level prediction or explicit variance decomposition (e.g., how much outcome variation is between vs within site); irregular real-world visit timing where complete-case or last-observation-carried-forward would discard information or bias under MAR.
When NOT to use — and when it is actively misleading or dangerous
(decision rules). - You need a population-average / policy estimand but report the conditional coefficient. Presenting a GLMM logit coefficient as if it were the population odds ratio overstates effect magnitude; for a marginal target use GEE or marginalize the fitted GLMM (predicted-margins / g-computation over the random-effect distribution). - Informative (non-ignorable, MNAR) dropout. The MAR likelihood is not a free lunch — if sicker patients drop out for reasons not captured by observed covariates and prior outcomes, mixed-model estimates are biased in the same direction as the dropout, and the model gives no warning. Add a sensitivity analysis (pattern-mixture, selection model, or reference-based/jump-to-reference multiple imputation). - Few clusters with many covariates. With <~10-15 clusters, the random-effect variance is poorly estimated and Wald/normal-theory cluster-level inference is anticonservative; use Kenward-Roger/Satterthwaite degrees of freedom or a cluster bootstrap, or switch to a design-based analysis. - Random-intercept-only models when slopes vary, or wrong exchangeability. Omitting a needed random slope understates SEs of time-by-treatment effects; assuming random effects uncorrelated with exposure when site-level channeling exists smuggles confounding back in. - Using a mixed model to "adjust away" confounding by indication. It models correlation, not treatment selection. It is a companion to a sound design (active-comparator new-user, propensity weighting), not a substitute.
Data-source operational depth
(claims vs EHR vs registry vs linked). - Claims (FFS vs MA vs commercial): Repeated outcomes are usually counts per interval (ED visits, hospitalizations, fills) requiring a Poisson/negative-binomial GLMM with a log-offset for observed person-time. The dominant failure mode is denominator/observability artifacts masquerading as outcome variation: Medicare Advantage (Part C) encounter capture is incomplete and inconsistent versus fee-for-service Parts A/B, so MA-only person-time produces spuriously low counts — restrict to FFS A/B (plus D for drug exposure) or model plan type and use a true at-risk offset. Plan switching, partial-year enrollment, and claims-adjudication lag create unbalanced and right-truncated panels; build an explicit per-interval `observed_days` offset and drop intervals with no observable enrollment rather than coding them as zero events. Differential competing risks by exposure in elderly claims (death ends the panel non-randomly) means a naive repeated-count GLMM can be biased; consider a joint longitudinal-survival model or censor consistently. Immortal time in procedure studies (follow-up clock starting before the exposure-defining procedure) corrupts the time variable for every visit — align the time origin at the index/exposure date for all subjects. - EHR: Visits are encounter-driven, not protocol-scheduled, so the visit times are themselves informative (sicker patients are measured more often) — measurement frequency can be an outcome-correlated process that violates the MAR-at-fixed-times convenience and may require a joint model of the measurement process. Lab/PRO values are missing not-at-random when ordered only on clinical suspicion; external-care leakage truncates trajectories. Use the actual continuous measurement time, not a nominal visit number, in the random slope. - Registry: Scheduled visits give cleaner balanced panels and adjudicated outcomes (an advantage for LMM/MMRM), but completeness varies by site and enrollment can be selective; site should usually enter as a random effect, and missingness still needs MAR scrutiny. - Linked claims–EHR–registry: The ideal substrate (EHR/registry severity to make MAR plausible + claims for complete utilization counts + a death index to handle the competing risk), but linkage selection and date-discrepancy reconciliation (encounter vs claim vs registry visit date) must be resolved before defining the repeated-measures time grid.
Worked claims example
Question: does a new biologic vs an active comparator reduce the rate of asthma-related ED visits over 24 months of follow-up in a commercial + Medicare FFS database? (1) Cohort: incident users of either drug (active-comparator new-user design) with 365 days of continuous medical + pharmacy enrollment before the index `fill_date`; exclude MA-only person-time because Part C ED encounters are under-captured. (2) Panel: split each patient's post-index follow-up into 12 consecutive 60-day intervals indexed by `interval_num`; in each interval compute `n_ed_visits` (count of claims with an asthma ED revenue/CPT code, de-duplicated to one event per calendar day) and `observed_days` = days of continuous FFS enrollment in that interval (the at-risk offset). Drop intervals with `observed_days` = 0 — they are unobservable, not event-free. (3) Model: a Poisson (or negative-binomial, if overdispersed) GLMM — log(E[n_ed_visits]) = beta0 + beta1arm + beta2interval_num + beta3(arm x interval_num) + Xgamma + log(observed_days) + b_person, with a random intercept b_person ~ N(0, sigma^2) absorbing each patient's baseline visit propensity; add a random slope on `interval_num` if trajectories diverge. (4) Estimand: report the conditional incidence-rate ratio (exp(beta1)) explicitly as subject-specific; if a population-average IRR is the policy quantity, refit as a log-link GEE or marginalize the GLMM. (5) Missingness/competing risk: dropout from disenrollment is plausibly MAR given observed history (the GLMM likelihood handles it); deaths end the panel non-randomly, so run a sensitivity analysis censoring at death and a pattern-mixture check. (6) Inference: with many patients the random-intercept SE is fine; cluster on `person_id`, and if also clustering by a small number of health plans, use Kenward-Roger / bootstrap rather than naive Wald.
Worked example
Scenario
A research team wants to know whether a new diabetes drug lowers HbA1c (a measure of blood sugar control) over 6 months. They enroll five patients, measure HbA1c at baseline (month 0), month 3, and month 6, and record every reading in a long-format table where each row is one patient at one visit. Patients start at different HbA1c levels, so they need a model that accounts for each person's own baseline rather than lumping everyone together.
Dataset
Long-format HbA1c measurements (one row per patient per visit).
| person_id | month | hba1c | arm |
|---|---|---|---|
| 101 | 9.2 | drug | |
| 101 | 3 | 8.5 | drug |
| 101 | 6 | 7.8 | drug |
| 102 | 7.4 | drug | |
| 102 | 3 | 7.0 | drug |
| 102 | 6 | 6.6 | drug |
| 103 | 8.8 | comparator | |
| 103 | 3 | 8.6 | comparator |
| 103 | 6 | 8.5 | comparator |
| 104 | 7.1 | comparator | |
| 104 | 3 | 7.0 | comparator |
| 104 | 6 | 7.0 | comparator |
Steps
Each patient has three rows -- one per visit. This is the long format a mixed-effects model expects.
Patients start at very different baselines: patient 101 begins at 9.2 and patient 102 at 7.4. A random intercept gives each patient their own starting point so the model does not force them all to start at the same average.
Patient 101 drops 1.4 points over 6 months; patient 102 drops 0.8 points. A random slope lets each patient have their own trajectory instead of assuming everyone changes at exactly the same rate.
The fixed effect for the drug arm captures the average additional drop across all drug patients compared to comparator patients, after accounting for each person's individual baseline and slope.
Drug patients (101, 102) average a drop of -1.1 points over 6 months [(9.2 to 7.8 = -1.4) + (7.4 to 6.6 = -0.8)] / 2 = -1.10. Comparator patients (103, 104) average -0.15 points [(8.8 to 8.5 = -0.3) + (7.1 to 7.0 = -0.1)] / 2 = -0.20.
The estimated fixed-effect treatment difference is approximately -1.10 minus (-0.20) = -0.90 HbA1c points in favor of the drug.
Result
Estimated average treatment difference: drug arm drops HbA1c by approximately 0.90 points more than the comparator arm over 6 months. The random intercept accounts for patients' different starting values; the random slope accounts for their different rates of change. The fixed-effect estimate of -0.90 describes the typical within-patient benefit, not a comparison of different people.
Runnable example
python implementation
Linear mixed growth-curve model in Python with statsmodels MixedLM (REML) for a serial continuous outcome measured at irregular real-world times. MixedLM is the production-grade mixed-effects fitter in statsmodels; for an exact log person-time offset on...
import statsmodels.formula.api as smf
# panel: one row per (person_id, visit); see header for required columns.
# Linear mixed growth curve: random intercept + random slope on continuous time per patient.
# re_formula gives each person_id its own intercept and slope, absorbing within-patient correlation;
# the arm*meas_time fixed effect is the SUBJECT-SPECIFIC (conditional) divergence in trajectories.
lmm = smf.mixedlm(
"outcome_value ~ arm * meas_time + age + female + baseline_severity",
data=panel,
groups=panel["person_id"],
re_formula="~ meas_time", # correlated random intercept & slope
).fit(reml=True, method="lbfgs")
print(lmm.summary())
# Variance components: between-patient (random-effects cov) vs within-patient (residual scale).
print("Random-effects covariance (G):\n", lmm.cov_re)
print("Residual variance (within-subject):", lmm.scale)
# The estimand is conditional. For a population-average mean trajectory, average model predictions
# over the random-effect distribution (predicted margins) or refit a marginal model (GEE).r implementation
Repeated-measures mixed models in R. Two fits from the same patient-interval `panel`: (A) Poisson GLMM with a true log person-time offset and a random intercept (claims count outcome). (B) Linear mixed growth-curve model for a continuous serial outcome...
library(lme4)
library(glmmTMB)
library(emmeans)
panel <- subset(panel, observed_days > 0) # keep only observable person-time
## (A) Poisson GLMM for repeated ED-visit counts; offset(log(observed_days)) -> a RATE model.
## (1 | person_id) is the patient random intercept; estimand is SUBJECT-SPECIFIC (conditional).
fit_count <- glmer(
n_ed_visits ~ arm * interval_num + age + female + baseline_severity +
offset(log(observed_days)) + (1 | person_id),
data = panel, family = poisson(link = "log"),
control = glmerControl(optimizer = "bobyqa")
)
# Overdispersion check; if present, refit negative-binomial with glmmTMB:
# fit_count <- glmmTMB(n_ed_visits ~ arm * interval_num + age + female + baseline_severity +
# offset(log(observed_days)) + (1 | person_id), family = nbinom2, data = panel)
summary(fit_count)
exp(fixef(fit_count)["arm"]) # conditional incidence-rate ratio for arm
## (B) Linear mixed growth curve: random intercept + random slope on continuous measurement time.
## Use actual meas_time (irregular real-world visits), not nominal visit number.
fit_lmm <- lmer(
outcome_value ~ arm * meas_time + age + female + baseline_severity +
(1 + meas_time | person_id),
data = panel, REML = TRUE
)
summary(fit_lmm)
# Marginal (population-average) treatment means over time, if a marginal estimand is required:
emmeans(fit_lmm, ~ arm | meas_time, at = list(meas_time = c(0.5, 1, 1.5, 2)))