Causal Mediation and Effect Modification
A pair of distinct causal-inference tasks in RWE — mediation, which decomposes a total effect into pathway-specific direct and indirect components through a post-treatment mediator, and effect modification/interaction, which describes how the effect varies across baseline subgroups or scales — each with its own estimands, identification assumptions, and characteristic biases (notably post-treatment adjustment bias).
In plain language
Causal mediation analysis asks: of the total effect a drug has on an outcome, how much travels through a specific in-between biological or behavioral step (the mediator), and how much acts by other routes? Effect modification asks a different question: does the drug work better or worse for certain patient groups defined before treatment starts? These two questions sound similar but require entirely different analyses, and confusing them, or carelessly adjusting for a step that happens after treatment begins, is one of the most common errors in real-world evidence studies.
RWE manuscripts routinely call a variable a "mediator," a "moderator," a "confounder," or an "effect modifier" without specifying its causal timing or role, yet these are different objects requiring different analyses. A confounder causes both exposure and outcome and is adjusted for. A mediator lies on the causal path from exposure to outcome (exposure -> mediator -> outcome) and is measured after exposure. An effect modifier (moderator) is a baseline characteristic across whose levels the exposure effect differs in magnitude or sign. A single post-treatment variable can be simultaneously a mediator of the exposure and a confounder of later treatment — which is precisely why naive regression adjustment for it is dangerous. This entry yokes two tasks that share machinery but answer different questions: mediation analysis (how much of the effect runs through a pathway) and effect modification / interaction (for whom, and on what scale, the effect is larger).
Core estimand distinction — mediation
With exposure A, mediator M, outcome Y, the total effect (TE) includes all pathways. The controlled direct effect (CDE) is the A->Y effect with M fixed by intervention at a chosen value m. The natural direct effect (NDE) and natural indirect effect (NIE) decompose TE = NDE + NIE by setting M to the value it would naturally take under a reference exposure — identification of these requires a cross-world (counterfactual independence) assumption that no experiment can verify. VanderWeele's four-way decomposition splits TE into a pure direct effect, a reference interaction, a mediated interaction, and a pure indirect effect, unifying mediation and interaction in one framework. The estimand must be chosen and pre-specified: CDE answers "what if we blocked the pathway," NDE/NIE answer "how much of the effect is mediated," and they coincide only when there is no A-by-M interaction.
Core estimand distinction — effect modification / interaction
Effect modification is scale-dependent. A treatment can show no interaction on the multiplicative (ratio) scale yet strong interaction on the additive (risk-difference) scale, and vice versa; whenever two factors each have a main effect, they cannot be additive on both scales at once. Public-health and HTA decisions (who to treat, absolute benefit, number-needed-to-treat) hinge on the additive scale, summarized by the RERI (relative excess risk due to interaction), the attributable proportion (AP), and the synergy index (S) — Knol & VanderWeele's reporting standard asks for both scales plus stratum-specific estimates. Critically, effect modification is not the same as causal interaction: a modifier may merely be a marker correlated with the true causal interactor, and machine-learned heterogeneous treatment effects (HTE) describe conditional variation without licensing a causal-pathway interpretation.
Pros, cons, and trade-offs — mediation
- vs reporting only the total effect (e.g., a single PS-adjusted hazard ratio): Mediation explains mechanism — how much of a drug's cardiovascular benefit operates through weight or HbA1c — which is valuable for label claims, surrogate validation, and pipeline decisions. Cost: it requires a correctly timed, well-measured mediator and strong, untestable confounding assumptions; an unmeasured mediator-outcome confounder biases NDE/NIE in unknown direction. Prefer the total effect alone unless the mechanistic question genuinely changes a decision. - vs g-estimation / marginal structural models for direct effects: When the mediator-outcome confounders are themselves affected by the exposure (treatment-induced confounding), standard regression-based mediation (Imai/VanderWeele product/difference methods) is biased and you must use g-methods (MSM for natural effects, g-formula, or g-estimation of structural nested models). Cost: g-methods are harder to specify and communicate. Prefer regression-based mediation only when no intermediate confounder is plausibly on the A->L->M,Y structure. - vs simply adjusting for the mediator in the outcome model: This is the cardinal error. Conditioning on a post-treatment mediator does not yield the total effect and generally does not yield a clean direct effect either — it can open a collider path (M's unmeasured causes) and induce post-treatment/overadjustment bias. Adjust for a post-treatment variable only when the estimand is explicitly a controlled/natural direct effect and the cross-world or no-exposure-induced-confounding assumptions are documented and defended.
Pros, cons, and trade-offs — effect modification
- vs a single pooled (marginal) effect: Effect modification targets the policy question of who benefits, supporting subgroup labeling and HTA reimbursement restrictions. Cost: multiplicity and data-driven subgroup fishing inflate false positives; report pre-specified subgroups, the interaction test, and both additive and multiplicative measures. Prefer the pooled effect when subgroups are not pre-specified or the trial/cohort is underpowered for interaction (interaction tests need ~4x the sample of main-effect tests). - vs causal-ML heterogeneous treatment effects (causal forests, meta-learners): ML estimates a flexible CATE surface useful for hypothesis generation and individualized prediction. Cost: it does not distinguish a true causal modifier from a correlated marker, and standard implementations target the conditional ATE, not a pre-specified contrast. Prefer parametric interaction terms when the modifiers are few, pre-specified, and clinically motivated.
When to use
Use mediation when a stakeholder question is genuinely about mechanism or surrogacy (does the cardiovascular benefit of a GLP-1 agonist run through weight loss?), the mediator is measurable with correct timing (post-exposure, pre-outcome), and mediator-outcome confounders are measured. Use effect modification/interaction when the decision is about targeting (which subgroup gets the largest absolute benefit), the modifiers are baseline and pre-specified, and the cohort is powered for interaction.
When NOT to use — and when it is actively misleading or dangerous
- Never adjust for a post-treatment mediator in a primary total-effect model. This is the single most common and most damaging error: it changes the estimand from the total effect to an ill-defined quantity, can introduce collider/overadjustment bias, and is routinely mislabeled as a "fully adjusted" total effect. Adjust for a post-treatment variable only when the estimand is explicitly direct-effect-like (CDE/NDE) and the assumptions are stated. - Do not run regression-based mediation when a mediator-outcome confounder is affected by treatment. The product-of-coefficients and difference methods break under treatment-induced confounding; results are biased even with infinite data. Switch to g-methods or do not decompose. - Do not interpret NDE/NIE causally without confronting the cross-world assumption. It is untestable; report sensitivity analysis (e.g., the proportion of the effect that an unmeasured U would have to explain to nullify the indirect effect). - Do not declare "no effect modification" from a non-significant multiplicative interaction term. Absence of multiplicative interaction is compatible with strong additive interaction that matters for treatment decisions; always report the additive scale. - Do not mine subgroups. Data-driven subgroup discovery without pre-specification or multiplicity control manufactures spurious modifiers; the famous astrological-sign subgroup result is the cautionary tale.
Data-source operational depth
- Claims (FFS or commercial): Strong for utilization mediators — adherence (PDC/MPR), treatment switching, hospitalization, procedure completion — and for utilization-scale effect modifiers (line of therapy, prior insulin). Weak for biological mediators: BMI/weight is captured only sparsely and non-currently via ICD-10 Z68.x codes, and labs are absent, so a biological mediator must be proxied (e.g., bariatric-procedure codes, anti-obesity Rx initiation) or sourced from linked EHR. Failure modes: (1) Medicare Advantage-only person-time lacks fee-for-service claims, so a "mediator-not-observed" can be missingness rather than a true zero — restrict to enrollees with full A/B/D or commercial pharmacy benefit. (2) Differential competing risks by exposure in the elderly: in claims of older adults, death competes with the non-fatal outcome and may differ by arm, so a mediator measured at a later landmark is conditioned on differential survival — handle with competing-risk or landmark/clone-censor-weight approaches. (3) Immortal time at the mediator-assessment window: both arms must survive (and remain enrolled) to the mediator measurement, which can induce immortal-time bias if handled naively. - EHR: Best substrate for biological mediators (weight, HbA1c, eGFR, blood pressure, PROs), but measurement is visit-driven and irregular — a mediator value exists only when a test was ordered, and ordering is informative (sicker patients are measured more), so missingness is rarely at random. Define a fixed mediator-assessment window relative to index, use the closest in-window value, and model missingness (multiple imputation or IPW) rather than complete-case. - Registry: Strongest for adjudicated effect modifiers and mediators with clinical meaning (cancer stage, disease activity, genomic markers, progression) measured on protocol-driven schedules; weak for complete pharmacy exposure and full utilization pathways. Link to claims to capture utilization mediators and to a death index for competing risks. - Linked claims-EHR-vital records: The ideal substrate — EHR biological mediators + claims utilization completeness + reliable mortality for competing risks — but linkage selects the linkable subset and introduces date-discrepancy issues (order vs fill vs lab-result dates) that must be reconciled before timing a mediator relative to time zero.
Worked claims/linked example (mediation + immortal time)
Question: does the reduction in hospitalized heart-failure events under a GLP-1 receptor agonist vs a DPP-4 inhibitor operate through early weight loss, among adults with type 2 diabetes in a linked commercial-claims + EHR database? (1) Cohort (ACNU core): age >=18, >=2 diabetes diagnoses, 365 days continuous medical+pharmacy enrollment, and no fill of any GLP-1 or DPP-4 agent in the 365-day washout; index_date = first qualifying fill (`fill_date`), arm assigned from the dispensed NDC. (2) Mediator (M): percent change in body weight from the baseline EHR value to the value nearest a 6-month landmark (window 4-8 months post-index). Because weight is poorly captured in claims (Z68.x sparse), the EHR linkage supplies it; in claims-only data the mediator would be proxied by anti-obesity Rx adds or bariatric procedure codes, an inferior operationalization to flag. (3) Immortal-time control: restrict the analysis to patients alive and enrolled at the 6-month landmark and start follow-up for the outcome at the landmark (landmark analysis), so neither arm accrues immortal time waiting for the mediator; alternatively clone-censor-weight. (4) Outcome (Y): first hospitalized HF event (validated dx in primary position) from the landmark to disenrollment, death, or data end. (5) Confounding: PS or covariate adjustment on baseline covariates measured in [index_date-365, index_date]; for the mediation step, additionally adjust the mediator-outcome relationship for landmark-window confounders — and verify none of them is treatment-affected (if HbA1c at the landmark both responds to arm and confounds weight->HF, regression mediation is invalid and g-methods are required). (6) Estimands: report TE (landmark HR), CDE (HF effect with weight change fixed), and NDE/NIE with a cross-world sensitivity analysis. (7) Effect-modification companion: pre-specify baseline BMI category as a modifier and report stratum-specific HRs and the RERI on the risk scale, since absolute HF reduction may be largest in the highest-BMI stratum even if the hazard ratio is constant.
Interpreting the output
In the GLP-1 versus DPP-4 analysis (250 per arm), decomposition yields: total risk difference = −0.040; natural direct effect (NDE) = 0.000; natural indirect effect (NIE) through weight loss = −0.040; high-BMI subgroup RD = −0.06; normal-BMI subgroup RD = −0.02.
(1) Formal interpretation. The zero NDE indicates that, had weight loss been set to its counterfactual value under DPP-4 for every patient, GLP-1 and DPP-4 would produce identical MACE rates — the entire benefit is mediated through the weight-loss pathway. The NIE of −0.040 represents the share of the total effect attributable to the mediator when exposure is fixed at GLP-1. This decomposition relies on the sequential ignorability (cross-world) assumption: no unmeasured confounding of the mediator-outcome relationship, even within levels of exposure. The effect-modification finding (RD −0.06 in high-BMI vs −0.02 in normal-BMI) is a subgroup contrast, not a mediation quantity, and must be assessed for interaction on the chosen effect scale before being interpreted as a modifier rather than chance variation.
(2) Practical interpretation. Complete mediation through weight loss has a direct regulatory implication: a payer restricting GLP-1 coverage to patients who achieve a weight-loss threshold may inadvertently capture the entire cardiovascular mechanism — the benefit does not appear to bypass the weight-loss pathway. The effect-modification finding supports label language about BMI-stratified expected benefit, but requires replication given the observational basis and the multiple-comparison exposure inherent in subgroup reporting.
Worked example
Scenario
A 500-person cohort study compares a GLP-1 receptor agonist (drug A) to a DPP-4 inhibitor (drug B) on the 12-month risk of a heart-failure hospitalization. Researchers want to do two things: (1) decompose the drug's total risk-difference effect into the part that runs through early weight loss (the mediator) and the part that does not, and (2) check whether the drug works differently in patients with high versus normal baseline BMI (effect modification). Patients are assigned to A (n=250) or B (n=250) at the index date. Weight loss of at least 5% by the 6-month landmark is the mediator. The outcome is heart-failure hospitalization in months 7-12.
Dataset
Aggregated risk table (simplified from the cohort); each row is one patient subgroup defined by arm and mediator status.
| arm | achieved_5pct_weight_loss | n_patients | hf_events | risk |
|---|---|---|---|---|
| A (GLP-1) | yes | 150 | 9 | 0.06 |
| A (GLP-1) | no | 100 | 14 | 0.14 |
| B (DPP-4) | yes | 50 | 5 | 0.1 |
| B (DPP-4) | no | 200 | 28 | 0.14 |
Steps
Step 1 — Total effect. Overall risk in arm A = (9+14)/250 = 23/250 = 0.092. Overall risk in arm B = (5+28)/250 = 33/250 = 0.132. Total risk difference (RD) = 0.092 - 0.132 = -0.040, meaning drug A reduced the 12-month heart-failure risk by 4.0 percentage points.
Step 2 — Direct effect (path NOT through weight loss). Among patients who did NOT achieve 5% weight loss, risk in A = 0.14, risk in B = 0.14. Direct RD = 0.14 - 0.14 = 0.00. Drug A shows no advantage when the weight-loss pathway is blocked, giving a direct effect of 0.00.
Step 3 — Indirect effect (path THROUGH weight loss). The indirect effect equals the total effect minus the direct effect: -0.040 - 0.000 = -0.040. All of the drug's benefit in this illustration travels through the weight-loss mediator.
Step 4 — Arithmetic check. Direct (-0.00) + indirect (-0.040) = -0.040 = total RD. The decomposition is exact.
Step 5 — Effect modification (a separate question). Now split by baseline BMI, a characteristic measured before treatment. In the high-BMI subgroup, RD = -0.06. In the normal-BMI subgroup, RD = -0.02. The drug's absolute benefit is three times larger in the high-BMI group. This is effect modification: the effect varies across a pre-treatment subgroup. Note that this is a different analysis from the mediation above; the mediator (weight loss) happened after treatment, while the BMI modifier was baseline.
Result
Total RD = -0.040 (drug A reduces 12-month HF risk by 4.0 pp). Decomposition: direct effect = 0.00, indirect effect through weight loss = -0.040, sum = -0.040. Effect modification by baseline BMI: high-BMI subgroup RD = -0.06, normal-BMI subgroup RD = -0.02; the benefit is larger in the high-BMI group, but this reflects who benefits, not how the effect travels.
Runnable example
python implementation
Effect modification (additive RERI) and regression-based mediation on a binary outcome. Required input table `df` (one row per subject, cohort + baseline + landmark variables already built): A : exposure arm, 1 = study drug, 0 = active comparator (assigned...
import numpy as np
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
# ---- Additive interaction (RERI) on the risk scale -------------------------------
# Log-binomial gives risk ratios; RERI = RR11 - RR10 - RR01 + 1 (Knol & VanderWeele 2012).
em = smf.glm("Y ~ A * high_bmi + age + cci", data=df,
family=sm.families.Binomial(sm.families.links.Log())).fit()
b = em.params
RR10 = np.exp(b["A"]) # A only
RR01 = np.exp(b["high_bmi"]) # modifier only
RR11 = np.exp(b["A"] + b["high_bmi"] + b["A:high_bmi"]) # both
reri = RR11 - RR10 - RR01 + 1
print(f"RERI (additive interaction) = {reri:.3f} (>0 => positive additive interaction)")
# ---- Regression-based mediation: NDE / NIE via the difference method -------------
# Difference method: total effect minus mediator-adjusted (direct) effect on the RR scale.
# Requires: no exposure-induced mediator-outcome confounding (else g-methods).
def mediation_rr(data):
tot = smf.glm("Y ~ A + age + cci", data=data,
family=sm.families.Binomial(sm.families.links.Log())).fit()
dir_ = smf.glm("Y ~ A + M + age + cci", data=data,
family=sm.families.Binomial(sm.families.links.Log())).fit()
te = np.exp(tot.params["A"]) # total effect (RR)
nde = np.exp(dir_.params["A"]) # direct effect with M held in the model (RR)
nie = te / nde # indirect (mediated) effect on the RR scale
prop_med = np.log(nie) / np.log(te) # proportion mediated (log-RR scale)
return te, nde, nie, prop_med
te, nde, nie, prop = mediation_rr(df)
rng = np.random.default_rng(20240601)
boot = np.array([mediation_rr(df.sample(len(df), replace=True, random_state=int(rng.integers(1e9))))
for _ in range(1000)])
lo, hi = np.percentile(boot[:, 2], [2.5, 97.5]) # bootstrap CI for the indirect effect (NIE)
print(f"TE={te:.2f} NDE={nde:.2f} NIE={nie:.2f} (95% CI {lo:.2f}-{hi:.2f}) prop. mediated={prop:.2%}")r implementation
Causal mediation with simulation-based NDE/NIE and additive interaction (RERI) in R. Input data.frame `df` mirrors the Python version (A, M, Y, high_bmi, age, cci; cohort + landmark built). The mediation::mediate path estimates natural effects under...
library(mediation)
library(interactionR)
## ---- Causal mediation: NDE / NIE with an A*M interaction allowed -----------------
med.fit <- glm(M ~ A + age + cci, family = binomial, data = df) # mediator model
out.fit <- glm(Y ~ A * M + age + cci, family = binomial, data = df) # outcome model (interaction)
set.seed(20240601)
med <- mediate(med.fit, out.fit, treat = "A", mediator = "M",
robustSE = TRUE, sims = 1000)
summary(med) # ACME (=NIE), ADE (=NDE), total effect, proportion mediated, 95% CIs
## Sensitivity to an unmeasured mediator-outcome confounder (Imai/Keele/Yamamoto 2010):
summary(medsens(med, rho.by = 0.1)) # rho at which the indirect effect crosses 0
## ---- Additive interaction (RERI / AP / S) ---------------------------------------
em.fit <- glm(Y ~ A * high_bmi + age + cci, family = binomial, data = df)
interactionR(em.fit, exposure_names = c("A", "high_bmi"),
ci.type = "delta", ci.level = 0.95) # RERI, AP, synergy index with CIs