Day 58. Mediation: Direct & Indirect Effects
How much of an effect runs through a particular mechanism? Controlled and natural direct effects, natural indirect effects, the mediation formula that identifies them, and the strong no-confounding assumptions they need, including the case where regression "controlling for the mediator" goes badly wrong.
- ●Define total, controlled direct, natural direct and natural indirect effects with potential outcomes.
- ●Compute them with the mediation formula for binary mediators and with closed-form expressions in linear models with interaction.
- ●State the identification assumptions and show why mediator–outcome confounding biases the "controlled regression" approach.
- ●Choose the right mediation quantity for a policy question.
Theory
Nested potential outcomes
Let be the treatment, a mediator and the outcome. Write for the mediator under treatment , and for the outcome when is set to and to . The observed outcome under treatment is .
Four effects (binary )
- Total effect: .
- Controlled direct effect at level : . Fix the mediator for everyone.
- Natural direct effect: . Change the treatment but keep each unit's mediator at the value it would have had without treatment.
- Natural indirect effect: . Keep the treatment on and shift only the mediator.
They add up: . The proportion mediated is .
Identification: the mediation formula
Assume, possibly within levels of pre-treatment covariates : no unmeasured confounding of (1) and , (2) and , (3) and , and (4) no confounder of and that is itself affected by . Then (Pearl's mediation formula)
needs only assumptions (1) and (3) (and can be identified even when (4) fails, by the g-formula).
Linear models with interaction
If and :
Without interaction (): and , the "product of coefficients" of Baron and Kenny.
The trap
"Regress on and and call the coefficient the direct effect" is right only under assumption (3). If something unobserved affects both and , is a collider on , and the coefficient is biased (Day 53, Exercise 9); it can even flip sign.
Part 1 · Warm-up
Exercise 1 · Read the potential outcomes
In words, what is for a jobseeker in a training programme where is the number of job applications sent?
Show solution
Step 1. First argument: the jobseeker receives training.
Step 2. Second argument: applications are held at the number they would have sent without training.
Answer: earnings with training but without training's effect on job search, so only the skill (direct) channel operates. It is a cross-world quantity: never observed for anyone.
Exercise 2 · Linear, no interaction
and , with no confounding. Find , and .
Show solution
Step 1. .
Step 2. .
Step 3. .
Exercise 3 · Linear with interaction
and . Compute , , , and .
Show solution
Step 1. ; .
Step 2. .
Step 3. .
Step 4. . Check directly: .
Exercise 4 · Proportion mediated
In Exercise 2, what share of the effect runs through ? In Exercise 3?
Show solution
Step 1. Exercise 2: .
Step 2. Exercise 3: .
Exercise 5 · Which effect answers which question?
Match: (a) "What would the effect of training be if we also guaranteed every trainee exactly 10 applications?" (b) "How much of training's effect is due to its effect on job search?" (c) "Does training work at all?"
Show solution
Step 1. (a) : the mediator is fixed by policy.
Step 2. (b) (or the proportion mediated): a mechanism question.
Step 3. (c) .
Part 2 · Proofs
Exercise 6 · The decomposition
Prove .
Show solution
Step 1. Add and subtract : .
Step 2. Take expectations: the first bracket is , the second .
Exercise 7 · Deriving the mediation formula for the NDE
Assume (a) , (b) , (c) , and (d) (the cross-world assumption). Prove the NDE formula.
Show solution
Step 1. .
Step 2. By (d), .
Step 3. By (a) and (c), (consistency).
Step 4. By (b), .
Step 5. So ; similarly . Subtract.
Exercise 8 · Linear formulas
With and , derive and from the mediation formula.
Show solution
Step 1. .
Step 2. .
Step 3. .
Step 4. Constants sum to zero against a difference of probabilities; the term gives .
Exercise 9 · No interaction, one direct effect
Show that if has no interaction, then for every .
Show solution
Step 1. No interaction: , a constant.
Step 2. for all .
Step 3. .
Exercise 10 · A treatment-induced mediator–outcome confounder
Let , , , (all observed). Explain why is identified but the is not, without further assumptions.
Show solution
Step 1. is a joint intervention; with unconfounded and blocking the back-door paths of , the g-formula gives (Day 55, Exercise 13).
Step 2. The NDE needs , which involves (through the direct path ) jointly with , which depends on .
Step 3. The joint distribution of and for the same unit is never observed and is not fixed by any observable distribution (a "recanting witness").
Answer: CDEs are identified; natural effects need extra assumptions or are reported with bounds.
Part 3 · Applied
Exercise 11 · Training, job finding and earnings
A training programme may raise monthly earnings ($100s) directly (skills) and by speeding up job finding (found a job within 3 months). With no confounding: , ; : : 10, : 20, : 12, : 24. Compute , , , , and the proportion mediated.
Show solution
Step 1. ; . .
Step 2. .
Step 3. .
Step 4. Check: . , .
Answer: about 46% of the effect () runs through faster job finding. The direct effect depends on the mediator level (2 or 4), so is a weighted average of the CDEs.
Exercise 12 · Cash transfers, food spending and child growth
A cash transfer raises monthly food spending ($): . Child height-for-age z-scores follow . Assume no unmeasured confounding. Compute the effects and the proportion mediated by food spending.
Show solution
Step 1. .
Step 2. .
Step 3. ; proportion mediated .
Answer: most of the effect runs through other channels (health care, schooling, reduced stress); the interaction means that food spending matters more for treated households.
Exercise 13 · Smoking, birth weight and infant mortality
Return to Day 48's paradox in mediation form. In a linear model, = birth weight (standardised), = birth defects (unobserved): , , with . What is the true direct effect of smoking, and what does the regression of mortality on smoking and birth weight report?
Show solution
Step 1. True direct effect: (harmful).
Step 2. Given and , is known, so with .
Step 3. .
Answer: the regression reports : smoking looks protective holding birth weight fixed, because birth weight is a collider of smoking and defects. The simulation confirms −0.3.
Exercise 14 · Occupation and the gender pay gap
A study regresses wages on gender, "controlling for occupation", and calls the remaining gap "discrimination" (a direct effect). Draw the DAG with gender , occupation , wage and unobserved ability or preferences that affect both occupation and wages. What is wrong, and which quantity could be defended?
Show solution
Step 1. , , , .
Step 2. Occupation is a mediator and a collider (). Conditioning on it opens , so the "within-occupation gap" mixes discrimination with ability differences induced by selection into occupations.
Step 3. The total gap is identified (gender is not caused by the other variables); the CDE given occupation needs measured.
Answer: report the total gap, and treat within-occupation gaps as descriptive unless the occupation–wage confounders are measured. Note also that occupational sorting can itself reflect discrimination, so the "indirect" part is not necessarily benign.
Exercise 15 · Obesity, hypertension and stroke
A health ministry can fund either a weight-loss programme or a hypertension-control programme. Researchers estimate the effect of obesity on stroke and the part mediated by hypertension . Which mediation quantity answers "how many strokes would remain among obese people if everyone's blood pressure were controlled?" What assumptions does it need?
Show solution
Step 1. Controlling blood pressure fixes the mediator at a normal level for everyone: is the effect of obesity that remains.
Step 2. Strokes prevented by hypertension control among the obese: , which needs the same assumptions.
Step 3. Assumptions: no unmeasured confounding of obesity and stroke, and of hypertension and stroke given obesity and covariates (e.g. diet, smoking, kidney function); no cross-world assumption is needed.
Answer: the CDE answers the policy question; natural effects answer the mechanism question and need stronger assumptions.
Verify in Python
import numpy as np
# Exercise 11: mediation formula with a binary mediator
pM = {0: .4, 1: .6}; EY = {(0, 0): 10, (0, 1): 20, (1, 0): 12, (1, 1): 24}
P = lambda m, x: pM[x] if m else 1 - pM[x]
TE = sum(EY[1, m] * P(m, 1) for m in (0, 1)) - sum(EY[0, m] * P(m, 0) for m in (0, 1))
NDE = sum((EY[1, m] - EY[0, m]) * P(m, 0) for m in (0, 1))
NIE = sum(EY[1, m] * (P(m, 1) - P(m, 0)) for m in (0, 1))
print(f"TE {TE:.2f} NDE {NDE:.2f} NIE {NIE:.2f} proportion mediated {NIE / TE:.2f}")
# Exercises 3 and 12: linear formulas, checked by simulating nested potential outcomes
def effects(b0, b1, t1, t2, t3, n=1_000_000, seed=58):
rng = np.random.default_rng(seed)
eM = rng.normal(size=n); eY = rng.normal(size=n)
M = lambda x: b0 + b1 * x + eM
Y = lambda x, m: t1 * x + t2 * m + t3 * x * m + eY
return (Y(1, M(0)) - Y(0, M(0))).mean(), (Y(1, M(1)) - Y(1, M(0))).mean()
for args in [(1, .5, 1, 2, .5), (50, 20, .10, .004, .002)]:
b0, b1, t1, t2, t3 = args
nde, nie = effects(*args)
print(f"simulated NDE {nde:.3f} (formula {t1 + t3 * b0:.3f}), NIE {nie:.3f} (formula {(t2 + t3) * b1:.3f})")
# Exercise 13: regression 'direct effect' with a confounded mediator
rng = np.random.default_rng(13); n = 1_000_000
X = rng.normal(size=n); U = rng.normal(size=n)
M = -X - U + rng.normal(size=n); Y = .2 * X - .5 * M + 1.0 * U + rng.normal(size=n)
coef = np.linalg.lstsq(np.column_stack([np.ones(n), X, M]), Y, rcond=None)[0]
print(f"coefficient on smoking given birth weight: {coef[1]:.3f} (true direct effect 0.2)")
Key takeaways
- ; controlled direct effects fix the mediator by policy, natural effects describe mechanisms.
- The mediation formula identifies natural effects under no confounding of –, – and –, plus a cross-world assumption that fails when treatment affects a mediator–outcome confounder.
- "Controlling for the mediator" gives a direct effect only if the mediator is unconfounded with the outcome; otherwise it is collider bias.
Simulation Lab: Direct & Indirect Effects
Open full screen →Controlled and natural direct effects, the indirect effect, and the bias from an unmeasured mediator–outcome confounder.
M = β0 + β1·X + g·U + e_M and Y = θ1·X + θ2·M + θ3·X·M + c·U + e_Y. The controlled direct effect CDE(m) = θ1 + θ3·m varies with the mediator level (line); NDE and NIE come from the mediation formula. With an unmeasured mediator–outcome confounder U (g, c ≠ 0), estimates from the regression of Y on X, M and X·M (dashed) are biased.