How this calculation works
Mediation separates a treatment association into a pathway through a mediator and a remaining direct component. In the supported linear additive model, the indirect component is the treatment-to-mediator slope multiplied by the mediator-to-outcome slope.
For continuous mediator/outcome and binary or continuous treatment without treatment-mediator interaction: OLS mediator M=alpha0+a A+alphaC C+error; OLS outcome Y=beta0+cPrime A+b M+betaC C+error with same baselineC. Unit treatment contrast indirect=a*b; direct=cPrime; total=cPrime+a*b. For A0→A1 multiply by deltaA. Check total against OLSY~A+C for same complete rows (linear decomposition). Optional delta-method product SE requires covariance convention and bootstrap preferable; do not fabricate causalCI. Causal interpretation requires sequential ignorability and no posttreatmentconfounders.A worked example
With the prefilled inputs, totalEffect = 4.817578782876623. Rows [binaryTreatment,continuousMediator,continuousOutcome,baselineControls...].
Good to know
Linear mediator and outcome g-computation; D(0)+I(1)=D(1)+I(0). Bootstrap unit rows, percentile intervals, no simultaneous coverage. Sequential ignorability and no exposure-induced mediator confounding assumed.
Reference: Primary method reference or author-maintained documentation (opens in a new tab) ↗
Decompose a linear treatment pathway
Mediation separates a treatment association into a pathway through a mediator and a remaining direct component. In the supported linear additive model, the indirect component is the treatment-to-mediator slope multiplied by the mediator-to-outcome slope.
Calling those components causal requires more than the regression arithmetic. The mediator-outcome relationship can remain confounded even when treatment itself was randomly assigned.
Compare with the Simple linear regression calculator. Decompose a treatment contrast into model-based natural direct/indirect effects through a specified mediator; a regression coefficient alone does not supply this decomposition.
Define the treatment contrast
Rows [binaryTreatment,continuousMediator,continuousOutcome,baselineControls...].For a binary treatment coded zero and one, the default contrast is one minus zero. A continuous-treatment contrast needs explicit starting and ending values, because both pathway components scale with that change.
Keep baseline covariates consistent across the two fitted equations. Variables affected by treatment can create a different mediation problem and should not simply be added as if they were ordinary baseline controls. The supported rows contain binary treatment, continuous mediator, continuous outcome and up to three baseline controls. The mediator model is linear in treatment and controls. The outcome model includes treatment, mediator, controls and, when selected, a treatment-by-mediator interaction. Controls are held at their observed baseline distribution for the averaging calculation. Variables measured after treatment should not be placed in the baseline columns just because they improve a regression fit. Doing so can change the causal estimand or create an identification problem that no algebraic decomposition can repair.
Fit the mediator equation
The first regression estimates how the mediator changes with treatment after the selected baseline controls. Its treatment slope is commonly called a, and its intercept describes the chosen reference coding.
Residual mediator variation is essential for separating the mediator coefficient from the treatment coefficient in the outcome model. If the mediator is an exact linear combination of treatment and controls, that separation is not identified.
Fit the outcome equation
The second regression includes treatment, mediator and the same baseline controls. The treatment coefficient is the direct linear component c-prime, while the mediator coefficient b describes its conditional outcome relationship.
The indirect component is a times b for a unit contrast. The direct and indirect components add to the total linear treatment coefficient only under the supported additive specification and the same analyzed observations.
A fully checkable example
For treatment values(0,0,1,1), mediator values(1,3,4,6) follow a fitted intercept of two and treatment slope three. Set outcome values to(5,13,19,27), which satisfy Y=1+2A+4M.
The direct component is two and the indirect component is3×4=12. Their sum is14, matching the difference in mean outcomes between the two treatment groups in this constructed sample.
Avoid misleading mediated percentages
Dividing the indirect component by the total can produce unstable or surprising values when the total is near zero or direct and indirect components have opposite signs. Such a ratio is not automatically a meaningful percentage of an effect.
Report the signed components and treatment contrast first. A result above100% or below zero can reflect cancellation rather than an arithmetic error, but the underlying model and identification assumptions still need scrutiny.
Know when the simple product fails
Treatment-mediator interactions make direct and indirect effects depend on the treatment level and mediator distribution. Binary outcomes or nonlinear links also break the simple additive coefficient decomposition.
This page's linear product is useful precisely because its assumptions are explicit. More complicated mechanisms need a corresponding mediation model and uncertainty procedure rather than relabeling the same product as a general natural effect. With interaction enabled, the direct and indirect effects depend on which exposure level is used for the other part of the counterfactual contrast. The page reports D(0), D(1), I(0) and I(1), with total effect equal to D(0)+I(1) and also to D(1)+I(0). These identities are useful implementation checks. Uncertainty uses a seeded bootstrap that resamples complete independent rows and refits both models, returning percentile intervals rather than delta-method intervals. A singular bootstrap design leaves the calculation unresolved rather than silently discarding difficult replicates. Rare treatment groups or highly collinear controls should therefore be investigated before relying on the reported intervals. Linear mediator and outcome g-computation; D(0)+I(1)=D(1)+I(0). Bootstrap unit rows, percentile intervals, no simultaneous coverage. Sequential ignorability and no exposure-induced mediator confounding assumed.
A few questions, answered
Does randomized treatment make mediation automatically causal?
No. Mediator-outcome confounding and other sequential identification assumptions still matter.
Why is the indirect component a times b?
In the additive linear equations, treatment changes the mediator by a units and each mediator unit changes the outcome by b units.
Can I use a logistic outcome model here?
Not with the same simple additive product interpretation; a nonlinear mediation procedure is required.
Why can the mediated fraction exceed100%?
Opposing direct and indirect components or a small total can make that ratio exceed one; report the components before interpreting a percentage.