← Methods repository
CONCEPTADVANCEDPYTHON · R · SAS4 citations

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.

Inferential Statisticsinferential-statisticslongitudinal-datamixed-effects-modelsrandom-effectsrepeated-measuresglmmmultilevel-modelsconditional-vs-marginal
On this page
Methods reference only. Use primary source citations and local policy before applying this in a study protocol, regulatory submission, payer dossier, or clinical decision.
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.

When to use it
When the question is subject-specific, dropout is plausibly MAR, or variance decomposition / individual prediction is needed.
Irregular real-world visit timing; prefer MMRM for registrational continuous endpoints on a scheduled visit grid.
When the hierarchical/correlation structure is trusted; otherwise pair a marginal model with robust variance, and use Kenward-Roger/bootstrap when clusters are few.
Watch out for
Conditional (subject-specific) coefficients differ from marginal ones under nonlinear links and are easily misreported as population-average;
Imposes a functional shape on the time trend (linear/spline) that can be wrong; MMRM avoids that assumption for scheduled visits.
A misspecified random-effects structure biases point estimates and SEs, whereas sandwich correction of a well-specified marginal model is agnostic to the working correlation.

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.

  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 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 + 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.
  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.

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]
Decision logic for longitudinal RWE modeling. The first fork is the estimand (conditional vs marginal); a marginal/policy target points to GEE or a marginalized GLMM rather than reporting the raw conditional coefficient. Outcome type and visit regularity then select the mixed-model form, and the dropout mechanism determines whether the MAR likelihood suffices or a non-ignorable sensitivity analysis is required.
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"]
Anatomy of a mixed-effects model. Fixed effects carry the population-level covariate and treatment contrasts; subject-specific random effects (intercept, optionally slope) absorb within-patient correlation and let the variance-component decomposition separate between- from within-subject variation. The link function determines whether fixed-effect coefficients are conditional rate/odds ratios or mean differences.

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_idmonthhba1carm
10109.2drug
10138.5drug
10167.8drug
10207.4drug
10237.0drug
10266.6drug
10308.8comparator
10338.6comparator
10368.5comparator
10407.1comparator
10437.0comparator
10467.0comparator

Steps

1Each patient has three rows -- one per visit. This is the long format a mixed-effects model expects.
2Patients 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.
3Patient 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.
4The 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.
5Drug 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.
6The 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.

Trade-offs

vs. GEE / population average models
Pros of this
Likelihood is valid under missing-at-random dropout (vs MCAR for unweighted GEE); estimates variance components and subject-specific predictions (BLUPs); targets the conditional estimand directly.
vs. MMRM (unstructured covariance repeated measures)
Pros of this
Handles unbalanced/irregular visit times and borrows strength across the trajectory via random coefficients — well suited to real-world off-grid follow-up.
vs. Cluster robust SEs on a marginal model
Pros of this
Models the correlation structure rather than only correcting SEs, enabling MAR-valid likelihood inference and greater efficiency when the variance structure is approximately correct.

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;

requires: pandas · numpy · 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).

Citations

FOUNDATIONAL / METHODS
  1. [1]Laird NM, Ware JH. Random-effects models for longitudinal data. Biometrics. 1982;38(4):963-974.
  2. [2]Liang KY, Zeger SL. Longitudinal data analysis using generalized linear models. Biometrika. 1986;73(1):13-22.
APPLIED EXAMPLES
  1. [3]Detry MA, Ma Y. Analyzing repeated measurements using mixed models. JAMA. 2016;315(4):407-408.
REPORTING & GUIDANCE
  1. [4]Mallinckrodt CH, Lane PW, Schnell D, Peng Y, Mancuso JP. Recommendations for the primary analysis of continuous endpoints in longitudinal clinical trials. Drug Information Journal. 2008;42(4):303-319.