StatsOtter Causal inference workflows
11
Workflow·5 steps

Causal mediation analysis (mediation)

Summary by StatsOtter

Decomposes a treatment effect into the part transmitted through a mediator (ACME) and the rest (direct effect), with sensitivity analysis.

1

Input · what goes in

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
2

Pipeline · the recipe

↑ Click any step in the diagram to read its logic, code, assumptions & discussion.

1
Data prep

Load package and data

Data preparation — shapes the raw inputs into what the estimator expects.

What happens here

Load the mediation package and the JOBS II field-experiment data shipped with it.

Reads from the input data Feeds into #2
Key code
# Install:  install.packages("mediation")
library(mediation)
data(jobs)

Reference / docs ↗

Discussion on this step (0)
  • No comments on this step yet — be the first.
2
Estimation

Fit the mediator model

The core estimate — where the causal quantity itself is computed.

What happens here

Regress the mediator (job-search self-efficacy) on the randomized treatment plus pre-treatment covariates.

Formula
M = \alpha + \beta T + \gamma X + \varepsilon
Reads from #1 Feeds into #3
Key code
med.fit <- lm(job_seek ~ treat + econ_hard + sex + age,
              data = jobs)

Reference / docs ↗

Discussion on this step (0)
  • No comments on this step yet — be the first.
3
Estimation

Fit the outcome model

The core estimate — where the causal quantity itself is computed.

What happens here

Regress the depression outcome on the mediator, treatment, and the same covariates.

Formula
Y = \alpha + \tau T + \delta M + \gamma X + \varepsilon
Reads from #2 Feeds into #4
Key code
out.fit <- lm(depress2 ~ treat + job_seek + econ_hard + sex + age,
              data = jobs)

Reference / docs ↗

Discussion on this step (0)
  • No comments on this step yet — be the first.
4
Inference

Simulate mediation effects

Uncertainty quantification — standard errors, intervals, and aggregation.

What happens here

Pass both fitted models to mediate() to simulate the ACME, ADE, and total effect.

Formula
\bar{\delta}(t) = E[Y_i(t, M_i(1)) - Y_i(t, M_i(0))]
Reads from #3 Feeds into #5
Key code
med.out <- mediate(med.fit, out.fit, treat = "treat",
                   mediator = "job_seek", sims = 1000)
summary(med.out)

Reference / docs ↗

Discussion on this step (0)
  • No comments on this step yet — be the first.
5
Robustness check

Sensitivity analysis

A robustness check — does the headline result survive a different lens?

What happens here

Use medsens() to assess how a violation of sequential ignorability (correlated errors, rho) would alter the ACME.

Formula
\rho = \mathrm{Corr}(\varepsilon_M, \varepsilon_Y)
Reads from #4 Feeds into the final output
Key code
sens.out <- medsens(med.out, rho.by = 0.1,
                    effect.type = "indirect", sims = 100)
summary(sens.out)

Reference / docs ↗

Discussion on this step (0)
  • No comments on this step yet — be the first.
3

Output · what you get

ACME (mediated), ADE (direct) and total effect, each with a 95% confidence interval.
Fig 1ACME (mediated), ADE (direct) and total effect, each with a 95% confidence interval.

Result figure rendered by StatsOtter from the package's documented example — unofficial community showcase; all credit to the original authors.

Result · the numbers

\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 

Links: package · paper

Discussion (0)

  • No comments yet — start the conversation.