Causal mediation analysis (mediation)
Follow the analysis.
Select a node to explore the method
Load package and data
Data preparation — shapes the raw inputs into what the estimator expects.
Load the mediation package and the JOBS II field-experiment data shipped with it.
# Install: install.packages("mediation")
library(mediation)
data(jobs)
Before using this step, make its assumptions and its output explicit. A useful peer note explains what you would check, what could go wrong, or what would change your interpretation.
Questions to explore- Which assumption matters most here, and what evidence could challenge it?
- What would you check before passing this output to the next step?
Choose a question to start your comment.
Peer discussion
- Bring your experience, a question, or a different perspective.
Log in to comment on this step.
Fit the mediator model
The core estimate — where the causal quantity itself is computed.
Regress the mediator (job-search self-efficacy) on the randomized treatment plus pre-treatment covariates.
med.fit <- lm(job_seek ~ treat + econ_hard + sex + age,
data = jobs)
Before using this step, make its assumptions and its output explicit. A useful peer note explains what you would check, what could go wrong, or what would change your interpretation.
Questions to explore- Which assumption matters most here, and what evidence could challenge it?
- What would you check before passing this output to the next step?
Choose a question to start your comment.
Peer discussion
- Bring your experience, a question, or a different perspective.
Log in to comment on this step.
Fit the outcome model
The core estimate — where the causal quantity itself is computed.
Regress the depression outcome on the mediator, treatment, and the same covariates.
out.fit <- lm(depress2 ~ treat + job_seek + econ_hard + sex + age,
data = jobs)
Before using this step, make its assumptions and its output explicit. A useful peer note explains what you would check, what could go wrong, or what would change your interpretation.
Questions to explore- Which assumption matters most here, and what evidence could challenge it?
- What would you check before passing this output to the next step?
Choose a question to start your comment.
Peer discussion
- Bring your experience, a question, or a different perspective.
Log in to comment on this step.
Simulate mediation effects
Uncertainty quantification — standard errors, intervals, and aggregation.
Pass both fitted models to mediate() to simulate the ACME, ADE, and total effect.
med.out <- mediate(med.fit, out.fit, treat = "treat",
mediator = "job_seek", sims = 1000)
summary(med.out)
Before using this step, make its assumptions and its output explicit. A useful peer note explains what you would check, what could go wrong, or what would change your interpretation.
Questions to explore- Which assumption matters most here, and what evidence could challenge it?
- What would you check before passing this output to the next step?
Choose a question to start your comment.
Peer discussion
- Bring your experience, a question, or a different perspective.
Log in to comment on this step.
Sensitivity analysis
A robustness check — does the headline result survive a different lens?
Use medsens() to assess how a violation of sequential ignorability (correlated errors, rho) would alter the ACME.
sens.out <- medsens(med.out, rho.by = 0.1,
effect.type = "indirect", sims = 100)
summary(sens.out)
Before using this step, make its assumptions and its output explicit. A useful peer note explains what you would check, what could go wrong, or what would change your interpretation.
Questions to explore- Which assumption matters most here, and what evidence could challenge it?
- What would you check before passing this output to the next step?
Choose a question to start your comment.
Peer discussion
- Bring your experience, a question, or a different perspective.
Log in to comment on this step.
Inputs & data
A fitted mediator model and a fitted outcome model sharing the same data with a treatment, a mediator, an outcome, and covariates.
Show data format & exampleHide example
| id | treat | mediator | outcome | age |
|---|---|---|---|---|
| 1 | 1 | 4.2 | 7 | 34 |
| 2 | 0 | 2.1 | 3 | 51 |
| 3 | 1 | 3.8 | 6 | 29 |
| 4 | 0 | 1.9 | 2 | 47 |
Result figure rendered by StatsOtter from the package's documented example — unofficial community showcase; all credit to the original authors.
The estimate
\delta(t)=\mathbb E\big[\,Y!\big(t,M(1)\big)-Y!\big(t,M(0)\big)\big]
⚠️ Unofficial community showcase of mediation (docs). Not affiliated with the authors — all credit to Kosuke Imai & coauthors; this summarizes public documentation.
What it does: mediation implements the Imai-Keele-Tingley-Yamamoto framework for causal mediation, estimating the Average Causal Mediation Effect (ACME), the Average Direct Effect (ADE), and the total effect. How it works: you fit two models—a mediator model (mediator ~ treatment + covariates) and an outcome model (outcome ~ treatment + mediator + covariates)—then pass both to mediate(), which uses a quasi-Bayesian Monte Carlo or nonparametric bootstrap to simulate counterfactuals and produce point estimates with confidence intervals. It supports linear, GLM, ordered, quantile, GAM, and survival models. Assumptions: identification rests on sequential ignorability—no unmeasured treatment-outcome, treatment-mediator, or mediator-outcome confounding given covariates. Because this is untestable, medsens() provides a sensitivity analysis quantifying how strong unobserved confounding would need to be to nullify the estimated mediation effect.
What you get — ACME (indirect effect), ADE (direct effect), total effect, and proportion mediated, each with confidence intervals; plus optional sensitivity bounds.
Example output
Causal Mediation Analysis
Quasi-Bayesian Confidence Intervals
Estimate 95% CI Lower 95% CI Upper p-value
ACME -0.01593 -0.03190 0.00 0.10
ADE -0.03762 -0.08940 0.01 0.14
Total Effect -0.05355 -0.10670 0.00 0.06
Prop. Mediated 0.29746 -0.06204 1.04 0.14
Sample Size Used: 899
Simulations: 1000
Discuss the workflow (0)
Log in to join the discussion.