ML Systems Lab Open interactive version →
Advanced 30 min read mediationindirect effectsNDENIEcausal path analysis

Mediation Analysis

Direct vs indirect effects, NDE/NIE decomposition, sequential ignorability

Job training increases earnings. How much of the effect is direct — training improves skills and directly raises wages — versus indirect — training leads to employment, which raises earnings? This is a mediation question: decomposing the total causal effect into the portion that flows through a mediator (employment) versus the direct path. The distinction matters for policy. If employment mediates the effect, job placement services might be as effective as skills training at lower cost. If the effect is direct, the content of the training matters and placement alone is insufficient.

Definitions. Total Effect (TE) = Direct Effect (DE) + Indirect Effect (IE). Natural Direct Effect (NDE) = Y(t, M(t′)) − Y(t′, M(t′)): the effect of treatment holding the mediator at the value it would take under control. Natural Indirect Effect (NIE) = Y(t, M(t)) − Y(t, M(t′)): the effect of the mediator shifting from its control-level to its treatment-level value while holding treatment fixed.

Baron-Kenny (classical approach):

- Regress outcome on treatment. - Regress mediator on treatment. - Regress outcome on both treatment and mediator.

The indirect effect equals the coefficient of treatment on mediator multiplied by the coefficient of mediator on outcome. Widely used and intuitive — but requires no unmeasured confounding of the mediator-outcome relationship, a strong assumption that an RCT does not guarantee. The RCT randomizes treatment, not the mediator.

Counterfactual approach (Imai, Keele, Tingley): estimates the Average Causal Mediation Effect (ACME) under sequential ignorability — treatment is as-good-as-randomized given pre-treatment confounders (as in Baron-Kenny), and the mediator is additionally as-good-as-randomized given treatment and those same confounders (the assumption Baron-Kenny simply asserts without naming). Because that second piece is rarely guaranteed even in a clean RCT, the approach pairs its ACME estimate with a sensitivity analysis to assess robustness to unmeasured mediator-outcome confounding. More rigorous than Baron-Kenny because it names and stress-tests the assumption Baron-Kenny leaves implicit.

What mediation analysis is not: controlling for the mediator in a regression. Controlling for M in a regression estimates the controlled direct effect, not the natural direct effect — and introduces collider bias if the mediator and outcome share an unmeasured common cause. M is a post-treatment variable. Any unmeasured variable that affects both M and Y creates a backdoor path through M that conditioning on M opens rather than closes. Mediation requires a structural model, not just adding an extra covariate to the regression.

Key points

Takeaway

Mediation requires a stronger assumption than total effect estimation — an RCT guarantees T-ignorability but not M-ignorability, and every mediation result needs sensitivity analysis for unmeasured mediator-outcome confounding.

Recap

Check your understanding

Q1. A new recommendation algorithm (T) increases 7-day retention (Y) by 5 percentage points. You suspect mechanism is session length (M). Select the two true statements about a valid mediation analysis here.

Q2. You control for a variable M in a regression of Y on T, and the coefficient on T drops from 0.4 to 0.1. Your colleague concludes '80% of the effect of T on Y is mediated by M.' What questions would you ask?

Q3. Your treatment T was assigned by a clean randomized experiment. Why can the mediation decomposition (NDE/NIE) through mediator M still be biased?

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →