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.
On this page
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_ij*beta + Z_ij*b_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.
- 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.
- 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.
- 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 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?
- 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.
- 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.
- Model: a Poisson (or negative-binomial, if overdispersed) GLMM — log(E[n_ed_visits]) = beta0 + beta1*arm + beta2*interval_num + beta3*(arm x interval_num) + X*gamma + 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.
- 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.
- 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.
- 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.
Decision diagram
flowchart TD
Q[Longitudinal RWE question] --> EST{Estimand:<br/>subject-specific or<br/>population-average?}
EST -->|Population-average / policy| GEE[GEE or marginalized GLMM<br/>see gee-population-average-models-rwe]
EST -->|Subject-specific / conditional| TYPE{Outcome type}
TYPE -->|Continuous serial| VISIT{Visit timing}
TYPE -->|Counts / binary per interval| GLMM[GLMM: Poisson/NB or logistic<br/>random intercept + log person-time offset]
VISIT -->|Scheduled / balanced grid| MMRM[MMRM: categorical time,<br/>unstructured covariance]
VISIT -->|Irregular real-world times| LMM[Linear mixed growth curve:<br/>random intercept + slope on time]
GLMM --> MISS{Dropout mechanism?}
LMM --> MISS
MMRM --> MISS
MISS -->|MAR| OK[Likelihood valid; report + variance components]
MISS -->|MNAR suspected| SENS[Add pattern-mixture / reference-based MI sensitivity]flowchart LR
subgraph Pop[Population-level: fixed effects]
B0[Intercept beta0] --- BX[Covariate effects X*beta<br/>arm, time, baseline covariates]
end
subgraph Sub[Subject-level: random effects]
BI["Random intercept b_i ~ N(0, G)<br/>each patient's baseline level"]
BS[Random slope on time<br/>each patient's trajectory]
end
Pop --> Mean[Linear predictor: X*beta + Z*b_i]
Sub --> Mean
Mean --> Link["Link g(.): identity LMM / log Poisson / logit GLMM"]
Link --> Y["Repeated outcome y_ij<br/>within-subject correlation absorbed by b_i"]
Sub --> VC["Variance components from G:<br/>separate between- vs within-subject variation"]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 | 0 | 9.2 | drug |
| 101 | 3 | 8.5 | drug |
| 101 | 6 | 7.8 | drug |
| 102 | 0 | 7.4 | drug |
| 102 | 3 | 7.0 | drug |
| 102 | 6 | 6.6 | drug |
| 103 | 0 | 8.8 | comparator |
| 103 | 3 | 8.6 | comparator |
| 103 | 6 | 8.5 | comparator |
| 104 | 0 | 7.1 | comparator |
| 104 | 3 | 7.0 | comparator |
| 104 | 6 | 7.0 | comparator |
Steps
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.
Trade-offs
Runnable example
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;
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).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 (e.g., HbA1c) measured at irregular times.
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)))Repeated-measures mixed models in SAS. Three procedures from the same patient-interval/visit data: WORK.PANEL : person_id, interval_num, n_ed_visits, observed_days (>0), arm (0/1), age, female, baseline_severity (one row per patient-interval;
/* Keep only observable person-time before modeling. */
data panel; set work.panel; where observed_days > 0; logpt = log(observed_days); run;
/* (A) Poisson GLMM for repeated ED-visit counts: random intercept + log person-time offset -> RATE model.
exp(arm estimate) is a SUBJECT-SPECIFIC (conditional) incidence-rate ratio. */
proc glimmix data=panel method=laplace;
class person_id female;
model n_ed_visits = arm interval_num arm*interval_num age female baseline_severity
/ dist=poisson link=log offset=logpt solution;
random intercept / subject=person_id; /* b_person ~ N(0, sigma^2) */
/* If overdispersed, switch dist=negbin (NB GLMM). */
estimate 'arm log-IRR' arm 1 / exp; /* exp gives the conditional IRR */
run;
/* (B) Linear mixed growth curve: random intercept + random slope on continuous measurement time. */
proc mixed data=serial method=reml;
class person_id arm visit;
model outcome_value = arm meas_time arm*meas_time age female baseline_severity / solution ddfm=kr;
random intercept meas_time / subject=person_id type=un; /* correlated random intercept & slope */
run;
/* (C) MMRM (regulatory primary analysis): categorical time, UNSTRUCTURED residual covariance,
Kenward-Roger denominator df. No random effects -- correlation is in the R-side covariance. */
proc mixed data=serial method=reml;
class person_id arm visit;
model outcome_value = arm visit arm*visit age female baseline_severity / solution ddfm=kr;
repeated visit / subject=person_id type=un;
lsmeans arm*visit / diff; /* marginal treatment-by-visit means */
run;Citations
- [1]Laird NM, Ware JH. Random-effects models for longitudinal data. Biometrics. 1982;38(4):963-974.
- [2]Liang KY, Zeger SL. Longitudinal data analysis using generalized linear models. Biometrika. 1986;73(1):13-22.