Your A/B test shows that the new checkout flow increased conversions by 12%. That's great. But why did it work? Was it because customers felt more confident? Because the process was faster? Because they understood the pricing better?
You know X causes Y. But you don't know how. That's where mediation analysis comes in.
Mediation analysis tests whether an effect operates through an intermediate variable — the mediator. It quantifies how much of your treatment effect flows through a specific mechanism versus other pathways. This matters because understanding mechanisms lets you amplify what's working and fix what isn't.
Before we jump into the method, let's be clear about what we're testing: a causal chain. X affects M, and M affects Y. This isn't just correlation — it's a sequence of relationships that requires careful experimental design and strong assumptions.
What You'll Learn
- The step-by-step logic of mediation analysis and how to interpret indirect effects
- How to calculate and test the mediated pathway using Baron-Kenny, Sobel, and bootstrap methods
- When mediation analysis gives you reliable answers (and when it doesn't)
- A worked example with real numbers showing partial vs. complete mediation
- Common mistakes that invalidate mediation claims
The Mediation Model: Three Relationships to Estimate
Mediation analysis decomposes a total effect into direct and indirect components. Here's the causal structure:
- Total Effect (c): X → Y (ignoring M)
- Direct Effect (c'): X → Y (controlling for M)
- Indirect Effect (a×b): X → M → Y
The indirect effect is the product of two paths:
- Path a: How much does X affect M?
- Path b: How much does M affect Y (controlling for X)?
The logic: if X increases M by 0.5 units (path a = 0.5), and each unit of M increases Y by 2 units (path b = 2), then the indirect effect through M is 0.5 × 2 = 1.0.
The total effect equals the direct effect plus the indirect effect: c = c' + a×b. If the indirect effect accounts for most of c, you have strong mediation. If c' is near zero, you have complete mediation — the effect runs entirely through M.
Mediation vs. Confounding
Don't confuse mediators with confounders. A confounder causes both X and Y, creating spurious association. A mediator is caused by X and causes Y. You control for confounders to remove bias. You measure mediators to understand mechanisms. Controlling for a true mediator removes the very mechanism you're trying to study.
Step-by-Step: Running the Baron-Kenny Procedure
The Baron-Kenny approach (1986) established the classic framework for testing mediation. It's been updated with better statistical methods, but the logic remains foundational. Here are the steps:
Step 1: Estimate the Total Effect
Run a regression of Y on X:
Y = β₀ + c·X + ε
This gives you the total effect (c). Baron and Kenny originally required this to be significant before proceeding. Modern practice allows you to continue even if c is non-significant (more on this below).
Step 2: Estimate Path a (X → M)
Run a regression of M on X:
M = β₀ + a·X + ε
Path a tells you how much X affects the mediator. If a = 0, there's no mediation — X doesn't influence the mechanism.
Step 3: Estimate Path b and c' (M → Y, X → Y)
Run a regression of Y on both X and M:
Y = β₀ + c'·X + b·M + ε
This gives you:
- Path b: The effect of M on Y, holding X constant
- Direct effect c': The effect of X on Y, controlling for M
If b = 0, M doesn't affect Y, so there's no mediation pathway.
Step 4: Calculate the Indirect Effect
The indirect effect is the product: a×b.
This is the amount of the total effect that flows through the mediator. Compare it to the direct effect (c') to see how much of the effect is explained by the mechanism versus other pathways.
Run This Analysis Free
Upload your CSV with columns for X (independent variable), M (mediator), and Y (dependent variable). Get path coefficients, indirect effects, Sobel test, and bootstrapped confidence intervals in 60 seconds.
Testing the Indirect Effect: Sobel Test vs. Bootstrapping
Calculating a×b is straightforward. Testing whether it's significantly different from zero is harder. The problem: the sampling distribution of a product of coefficients is not normal, especially with smaller samples.
The Sobel Test (Classic Approach)
The Sobel test uses a normal approximation to compute a z-statistic for the indirect effect:
SE(a×b) = √(a²·SE(b)² + b²·SE(a)²)
z = (a×b) / SE(a×b)
If |z| > 1.96, the indirect effect is significant at α = 0.05.
The problem: this assumes the sampling distribution of a×b is normal, which is often false. The Sobel test is conservative (low power) and less accurate than modern alternatives.
Bootstrapping (Recommended Approach)
Bootstrapping makes no normality assumption. Here's how it works:
- Resample your data with replacement (n = original sample size)
- Estimate paths a and b on the resampled data
- Calculate a×b for that bootstrap sample
- Repeat 5,000–10,000 times
- Use the 2.5th and 97.5th percentiles of the bootstrap distribution as your 95% confidence interval
If the confidence interval excludes zero, the indirect effect is significant. This method has higher power and better Type I error rates than the Sobel test.
When you run mediation analysis with MCP Analytics, you get both the Sobel test and bootstrapped percentile confidence intervals so you can compare.
Worked Example: Does Training Increase Sales Through Product Knowledge?
Let's work through a concrete example. You run a sales training program and want to know if it increases revenue by improving product knowledge.
The Setup
- X: Training (1 = received training, 0 = control)
- M: Product knowledge score (0–100 scale)
- Y: Monthly sales revenue ($1000s)
- Sample size: 240 sales reps (120 per group)
Did you randomize? Yes. Reps were randomly assigned to training vs. control. This is critical — without randomization, selection bias could confound all three relationships.
Step 1: Total Effect (c)
Regression of Y (revenue) on X (training):
Revenue = 42.3 + 8.6·Training
(SE) (2.1) (1.8)
Total effect c = 8.6, p < 0.001
Training increases revenue by $8,600 per month on average. But how?
Step 2: Path a (X → M)
Regression of M (knowledge) on X (training):
Knowledge = 62.4 + 14.3·Training
(SE) (2.6) (3.2)
Path a = 14.3, p < 0.001
Training increases product knowledge by 14.3 points on average.
Step 3: Path b and c' (M → Y, X → Y controlling for M)
Regression of Y (revenue) on both X and M:
Revenue = 18.7 + 2.4·Training + 0.43·Knowledge
(SE) (4.2) (1.6) (0.05)
Direct effect c' = 2.4, p = 0.13
Path b = 0.43, p < 0.001
Here's what this tells us:
- Path b = 0.43: Each point of product knowledge increases revenue by $430/month
- Direct effect c' = 2.4: The effect of training on revenue, controlling for knowledge, is $2,400/month (not significant)
Step 4: Indirect Effect
Indirect effect = a × b = 14.3 × 0.43 = 6.15
The mechanism through product knowledge accounts for $6,150 of the total $8,600 effect.
Proportion mediated = (a×b) / c = 6.15 / 8.6 = 71.5%
This is partial mediation. Most of the effect operates through improved knowledge, but not all. There may be other mechanisms (confidence, motivation, network effects) contributing to the remaining $2,400.
Bootstrapped Confidence Interval
Using 5,000 bootstrap resamples, the 95% CI for the indirect effect is [3.82, 8.91]. This excludes zero, confirming significant mediation.
Sobel test: z = 4.23, p < 0.001. Both methods agree.
Interpretation
Training increases sales primarily by improving product knowledge. The mechanism is clear and quantifiable: 72% of the training effect flows through this pathway. To amplify the program's impact, focus on improving the knowledge component. To capture the remaining 28%, investigate other mediators.
Complete vs. Partial Mediation: What's the Difference?
Complete mediation: The entire effect of X on Y operates through M. The direct effect (c') is zero or non-significant. Once you account for the mediator, X no longer predicts Y.
Partial mediation: The mediator explains some, but not all, of the effect. There's a significant direct effect (c') even after controlling for M. Other pathways exist.
In our example above, we had partial mediation (c' = 2.4, though not statistically significant). If c' were truly zero, we'd have complete mediation — training would affect sales only through product knowledge.
Complete mediation is rare in real data. Most treatments operate through multiple mechanisms. Don't force a complete mediation interpretation if the data show partial mediation. The goal is to quantify mechanisms, not to oversimplify them.
When Mediation Analysis Breaks Down: Critical Assumptions
Mediation analysis makes strong causal assumptions. Violate them, and your conclusions are wrong. Before you interpret mediation results, check these requirements:
1. No Unmeasured Confounding (X → M, M → Y)
The X → M and M → Y relationships must not be confounded by unmeasured variables. This is especially challenging for the M → Y path.
Example: In the training example, what if high-performing reps naturally acquire more product knowledge and generate more sales (reverse causation or third-variable confounding)? Even with randomized X, the M → Y relationship could be biased.
Solution: Randomize M if possible (rare in practice). Otherwise, control for observed confounders and acknowledge the limitation. Sensitivity analysis (see Imai et al. 2010) can quantify how robust your findings are to unmeasured confounding.
2. Correct Temporal Ordering
The causal sequence must be X → M → Y. Measure M after X but before Y. If you measure all variables simultaneously (cross-sectional data), you can't establish temporal precedence.
This is why panel data or experiments with staged measurement are ideal for mediation analysis. Upload time-stamped data to verify temporal ordering in your analysis.
3. No Measurement Error in the Mediator
Measurement error in M attenuates path b and inflates the direct effect (c'), leading to underestimation of mediation. Use reliable, validated measures of your mediator.
4. Linear Relationships (for Standard OLS Mediation)
The basic mediation model assumes linear relationships. If M → Y is nonlinear, the a×b product may not accurately capture the indirect effect. Check residual plots. Consider transformations or generalized models if needed.
5. No Mediator-Outcome Confounding Induced by X
If X opens a backdoor path between M and Y (e.g., X affects both M and an unmeasured confounder of M → Y), the M → Y relationship is biased even in a randomized experiment.
This is subtle but important. Randomizing X doesn't automatically make the M → Y relationship causal. See VanderWeele (2015) for detailed discussion of identification assumptions.
When to Trust Mediation Results
High confidence: Randomized X, mediator measured post-treatment, strong theory predicting X → M → Y, no plausible confounders of M → Y.
Moderate confidence: Observational X with good covariate control, proper temporal ordering, sensitivity analysis shows robustness.
Low confidence: Cross-sectional data, weak theory, no control for M–Y confounders, simultaneous measurement.
Data-Driven Decisions: Building Your Mediation Dataset
Before you run the analysis, you need the right data structure. Here's what your dataset should look like:
Required Variables
- X (independent variable): Treatment, exposure, or predictor. Can be binary (0/1) or continuous.
- M (mediator): The mechanism variable measured after X. Continuous or ordinal.
- Y (outcome): The dependent variable. Continuous for standard OLS mediation.
Recommended Variables
- Covariates: Variables that might confound X → M, X → Y, or M → Y relationships. Include these as controls.
- Timestamps: Verify temporal ordering (X happens before M, M happens before Y).
- Randomization indicators: If X was randomized, document it. If not, note sources of selection bias.
Sample Size Considerations
The indirect effect (a×b) has lower statistical power than direct effects. Why? You're estimating two parameters and multiplying them. Sampling variability compounds.
Rules of thumb:
- N ≥ 200: Minimum for detecting medium indirect effects with bootstrapping
- N ≥ 400: Recommended for Baron-Kenny or Sobel test (lower power methods)
- N ≥ 500: Comfortable sample for small-to-medium effects
Underpowered mediation analysis is worse than no analysis. If your test lacks power, you'll conclude "no mediation" when a real mechanism exists. Run a power analysis before you collect data.
Fritz & MacKinnon (2007) provide detailed power tables for mediation designs. Or use simulation: generate data under your assumed effect sizes and see how often you detect significance.
Common Mistakes That Invalidate Mediation Claims
I've reviewed hundreds of mediation analyses. Here are the errors I see repeatedly:
1. Testing Mediation Without a Significant Total Effect
The original Baron-Kenny procedure required a significant c before testing mediation. Modern statisticians reject this (Hayes 2009). Why?
If the direct effect (c') and indirect effect (a×b) have opposite signs, they cancel out, making c ≈ 0. You could have strong mediation with a non-significant total effect. Requiring c to be significant misses these cases.
Correct approach: Test the indirect effect directly with bootstrapping. Ignore the significance of c.
2. Claiming Causation With Cross-Sectional Data
You cannot establish X → M → Y causation if all variables are measured simultaneously. The observed associations might reflect reverse causation (Y → M) or third-variable confounding.
Correct approach: Use experimental or longitudinal data with proper temporal spacing. If you only have cross-sectional data, frame your results as "consistent with" mediation, not "proving" mediation.
3. Ignoring Confounders of M → Y
Even if X is randomized, the M → Y relationship is observational. Unmeasured variables that affect both M and Y bias path b.
Correct approach: Include covariates that predict M and Y. Conduct sensitivity analysis to assess robustness to unmeasured confounding.
4. Using the Sobel Test With Small Samples
The Sobel test assumes normality of the a×b distribution, which fails in small samples (N < 200). It's also less powerful than bootstrapping.
Correct approach: Use bias-corrected bootstrap confidence intervals (5,000+ resamples). Report percentile CIs, not standard errors.
5. Testing Multiple Mediators Without Adjustment
If you test 10 potential mediators at α = 0.05, you'll get 1–2 "significant" results by chance alone (Type I error inflation).
Correct approach: Prespecify your mediator based on theory. If testing multiple mediators is necessary, adjust for multiple comparisons (Bonferroni, FDR) or use joint significance testing. Better yet: run multiple mediator analysis (see Hayes 2013 PROCESS macro) to estimate unique indirect effects for each mediator while controlling for others.
6. Interpreting Mediation as "Proof" of Mechanism
Finding that a×b ≠ 0 doesn't prove M is the mechanism. Other unmeasured mediators might explain the effect. The causal chain requires strong assumptions that are rarely fully met.
Correct approach: Frame mediation results as evidence consistent with a mechanistic hypothesis, not definitive proof. Triangulate with other evidence (theory, qualitative data, experimental manipulation of M).
Beyond Simple Mediation: Extensions and Variations
The single-mediator model is just the starting point. Real-world mechanisms are often more complex. Here are extensions to consider:
Multiple Mediators in Parallel
X might affect Y through several mediators simultaneously (M₁, M₂, M₃). Each mediator contributes a unique indirect effect. Multiple mediator analysis estimates these simultaneously, partitioning the total indirect effect.
Example: Training affects sales through product knowledge (M₁), confidence (M₂), and customer relationships (M₃). You can quantify how much each mechanism contributes.
Serial Mediation (Causal Chains)
X → M₁ → M₂ → Y. The effect flows through mediators in sequence. Example: Training improves knowledge (M₁), which increases confidence (M₂), which drives sales (Y).
Serial mediation tests the indirect effect through the chain: a₁ × a₂ × b₂.
Moderated Mediation
The indirect effect (a×b) differs across levels of a moderator (W). Example: The mechanism through product knowledge might be stronger for complex products than simple ones.
This tests conditional indirect effects: Is a×b significant at W = low? At W = high? Does it differ?
Binary or Count Outcomes
If Y is binary (purchase yes/no) or a count (number of purchases), use logistic or Poisson regression instead of OLS. The indirect effect is calculated on the appropriate scale (logit or log) and then transformed back.
VanderWeele & Vansteelandt (2010) provide methods for mediation with non-continuous outcomes.
These extensions require more complex modeling. The PROCESS macro (Hayes 2022) and mediation package in R (Tingley et al. 2014) implement them. Create a custom analysis pipeline if you need to automate these for recurring reporting.
How MCP Analytics Handles Mediation
When you upload your dataset to MCP Analytics' mediation tool, here's what happens:
1. Path Estimation
The tool fits three OLS regressions (Y ~ X, M ~ X, Y ~ X + M) and extracts coefficients for c, a, b, and c'. You get standardized and unstandardized estimates.
2. Indirect Effect Calculation
The indirect effect (a×b) is computed with standard errors from the Sobel formula. The tool also calculates the proportion of the total effect mediated: (a×b) / c.
3. Significance Tests
You get both the Sobel z-test and bias-corrected bootstrap confidence intervals (5,000 resamples by default). The bootstrap CI is the primary test; the Sobel test is included for comparison.
4. Diagnostic Output
The tool flags potential issues:
- Non-significant path a or path b (mediation unlikely)
- Total effect and indirect effect with opposite signs (possible suppression)
- Small sample warnings (N < 200)
5. Reproducible R Code
Every analysis includes embedded R code showing exactly how the estimates were computed. You can cite the methodology, verify the approach, and extend it. The code uses fixed random seeds for bootstrap reproducibility.
6. APA/MLA/BibTeX Citations
Need to cite the analysis in a paper? The tool generates publication-ready citations in multiple formats, including methods descriptions you can adapt for your manuscript.
Sample Output
MEDIATION ANALYSIS RESULTS
==========================
Total Effect (c): 8.62 (SE = 1.78), p < 0.001
Direct Effect (c'): 2.41 (SE = 1.63), p = 0.139
Path a (X → M): 14.32 (SE = 3.18), p < 0.001
Path b (M → Y | X): 0.433 (SE = 0.051), p < 0.001
Indirect Effect (a×b): 6.20
Sobel test: z = 4.23, p < 0.001
Bootstrap 95% CI: [3.82, 8.91]
Proportion Mediated: 71.9%
Interpretation: Partial mediation detected
Alternatives to Mediation Analysis: When to Use Something Else
Mediation analysis is powerful, but it's not always the right tool. Here are situations where alternative methods work better:
When You Want to Test Interaction (Use Moderation Analysis)
If your question is "Does X affect Y differently depending on Z?", you need moderation (interaction) analysis, not mediation. Moderation asks when an effect occurs. Mediation asks how it occurs.
When You Have Complex Causal Structures (Use SEM or DAGs)
If multiple variables affect each other in a network (X affects M₁ and M₂, M₁ affects M₂, both affect Y), structural equation modeling (SEM) or directed acyclic graphs (DAGs) provide a more flexible framework than simple mediation.
When Temporal Dynamics Matter (Use Time Series or Dynamic Models)
If the effect of X on M and Y unfolds over time with feedback loops, use time series methods, vector autoregression (VAR), or dynamic panel models rather than static mediation.
When You Can't Measure the Mediator (Use Instrumental Variables)
If you hypothesize a mediator but can't measure it directly, instrumental variable (IV) methods can estimate the causal effect of X on Y under different assumptions.
When You Want Causal Bounds Without Strong Assumptions
If you're unwilling to assume no unmeasured confounding, sensitivity analysis (Imai et al. 2010) or bounds analysis (Manski) can tell you how strong unobserved confounding would need to be to overturn your conclusions.
Reporting Mediation Results: What to Include
When you report mediation findings (in a paper, presentation, or report), include these elements:
1. Model Specification
State your hypothesized causal model clearly: "We hypothesized that training (X) increases sales (Y) by improving product knowledge (M)."
2. Sample Characteristics
Report N, data source, measurement timing, and whether X was randomized or observational.
3. Path Coefficients
Report unstandardized coefficients with SEs and p-values for c, a, b, and c'. Use a diagram or table.
4. Indirect Effect
Report the point estimate for a×b, the bootstrap 95% CI (not SE), and the Sobel test for comparison. State whether the CI excludes zero.
5. Proportion Mediated
Report (a×b) / c as a percentage. Interpret whether you have complete or partial mediation.
6. Assumptions and Limitations
Acknowledge potential confounders, measurement error, temporal ambiguity, and other threats to validity. Be honest about what you can and cannot conclude.
7. Sensitivity Analysis (If Applicable)
If you conducted sensitivity analysis for unmeasured confounding, report the robustness of your findings.
Example: "The indirect effect would only be reduced to non-significance if an unmeasured confounder explained >40% of the variance in both M and Y, which is unlikely given our observed covariates."
FAQ: Mediation Analysis Questions Answered
What's the difference between moderation and mediation?
Moderation asks "when" or "for whom" an effect occurs (X affects Y differently depending on Z). Mediation asks "how" or "why" an effect occurs (X affects Y through intermediate variable M). Moderation involves interaction terms; mediation involves sequential causal paths.
Can I run mediation analysis with observational data?
Yes, but be cautious with causal language. Mediation analysis requires strong assumptions: no unmeasured confounding for X→M, X→Y, and M→Y relationships. With observational data, you're testing whether the data are consistent with a mediation model, not proving causation. Experimental designs with randomized X provide stronger evidence.
How large should my sample be for mediation analysis?
The indirect effect (a×b) has lower power than direct effects. For 80% power to detect a small-to-medium indirect effect with bootstrapping, aim for N ≥ 200. For Baron-Kenny or Sobel test, you may need N ≥ 400. Underpowered mediation tests frequently miss real mechanisms.
What if the total effect (c) isn't significant?
You can still have meaningful mediation. If X affects M, and M affects Y, but the direct effect (c') counteracts the indirect effect (a×b), the total effect (c = c' + a×b) may be near zero. Modern approaches test the indirect effect directly rather than requiring a significant total effect first.
Should I use Baron-Kenny, Sobel test, or bootstrapping?
Use bootstrapping. It provides the most accurate confidence intervals for indirect effects without assuming normality. Baron-Kenny has limited power and outdated logic. The Sobel test assumes normality of the a×b distribution, which is often violated. Bootstrapped percentile confidence intervals are now the gold standard.
Can I have multiple mediators?
Yes. Multiple mediator analysis estimates unique indirect effects for each mediator while controlling for the others. This partitions the total indirect effect into components attributable to each mechanism. Use parallel mediation models (all mediators at the same causal level) or serial mediation (mediators in sequence).
What if my mediator is binary or categorical?
You can still run mediation analysis, but the path a model (X → M) should use logistic regression if M is binary. The indirect effect calculation adjusts for the non-linear link function. Hayes' PROCESS macro and the mediation package in R handle this automatically.
How do I handle covariates in mediation analysis?
Include covariates as additional predictors in all three regression models (Y ~ X + covariates, M ~ X + covariates, Y ~ X + M + covariates). This controls for confounding and increases precision. Be sure covariates are measured before or concurrent with X, not after.
Key Takeaway: Mechanisms Drive Better Decisions
You ran an experiment. You found an effect. Now you need to understand why it worked so you can replicate it, scale it, and improve it. That's what mediation analysis delivers.
The step-by-step methodology is straightforward: estimate three regressions, calculate a×b, bootstrap the confidence interval, and interpret the proportion mediated. But the value comes from what you do next.
When you know 72% of your training effect flows through product knowledge, you can double down on knowledge transfer. When you discover the remaining 28% operates through other pathways, you can investigate and optimize those too. Mediation analysis transforms "it works" into "here's how it works" — and that's how you build data-driven strategy.
Before we draw conclusions, let's check the experimental design. Did you randomize X? Did you measure M after X but before Y? Have you controlled for confounders of the M → Y relationship? If yes, your mediation analysis has a solid foundation. If no, interpret cautiously and plan better data collection for next time.
Upload your data, run the analysis, and start understanding your mechanisms. The tool is free, the method is proven, and the insights will change how you think about your interventions.
Run your mediation analysis now. CSV upload, results in 60 seconds, reproducible R code included.