Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 37 ยท Graphs and Identification

Mediators, Direct and Indirect Effects, and Bad Controls

Learn what mediators are, how to separate direct and indirect effects, why controlling for post-treatment variables biases results, and the M-bias example, with Python simulations.

Not every variable you can measure belongs in your model. Variables that sit between the treatment and the outcome (mediators), or are caused by both, behave very differently from confounders. Putting them in a regression by reflex is one of the most common causal mistakes, and this lesson shows when it hurts and when mediation is itself the question.

Mediators

A mediator M lies on a causal path from treatment to outcome:

flowchart LR
  T["Treatment: job training"] --> M["Mediator: new skills"]
  M --> Y["Outcome: salary"]
  T --> Y

Training raises salary directly (say, certificates signal quality) and indirectly by building skills.

  • The total effect is everything T does to Y: direct + indirect.
  • The direct effect is the part not going through M.
  • The indirect effect is the part going through M.

If you control for M when your question is about the total effect, you remove the indirect part and estimate only the direct effect.

Python Controlling for a mediator removes part of the effect

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Even in a perfectly randomized experiment, adding the mediator changes the estimate from the total to the direct effect. Nothing is "wrong", but the question changed.

Mediation analysis (the product of coefficients)

In a linear model with no confounding of the paths, the indirect effect is a * b, where a is the effect of T on M and b is the effect of M on Y holding T fixed. This is the classic Baron-Kenny style decomposition.

Python Estimating direct and indirect effects

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This works when T is randomized and there is no unmeasured confounder between M and Y. That second condition is the weak spot: M is rarely randomized, so confounders of the mediator-outcome relation need their own adjustment (and the assumptions are strong).

Bad controls

A bad control is a variable that, when adjusted for, creates or worsens bias:

  1. Mediators (when you want the total effect).
  2. Colliders and their descendants, see selection and collider bias.
  3. Outcome proxies (descendants of Y).
  4. M-bias variables: pre-treatment variables that are colliders.

M-bias

flowchart LR
  U1["Unmeasured U1"] --> T["T"]
  U1 --> X["X (pre-treatment)"]
  U2["Unmeasured U2"] --> X
  U2 --> Y["Y"]

X happens before T but is a collider of U1 and U2. Without adjusting for X, T and Y are unconfounded. Adjusting for X opens T <- U1 -> X <- U2 -> Y.

Python M-bias: adjusting for a pre-treatment collider

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The simple model is nearly right. "Controlling for X" because it is measured and pre-treatment makes it wrong. In practice the bias is often modest, but the example shows why graphs matter more than timing alone.

A decision guide for covariates

Variable Relation to T and Y Adjust?
Common cause (confounder) Causes both Yes
Mediator Caused by T, causes Y No (for total effect)
Collider Caused by both T-side and Y-side variables No
Cause of Y only Predicts Y Optional, improves precision
Cause of T only Predicts T Usually not, can amplify bias
Descendant of Y Caused by outcome No

Ask of every control: "Does this come before the treatment, and does it affect both T and Y (or proxy such a variable)?" If you cannot say yes with a reason, think twice.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a randomized experiment, adding a mediator to the regression changes the treatment coefficient from the total effect to:

    Holding the mediator fixed blocks the path through it.
  2. 2. The indirect effect in a linear mediation model equals:

    It is the product of the two path coefficients.
  3. 3. M-bias shows that:

    Timing alone does not make a variable a valid control; the graph structure does.
  4. 4. Which variable is a bad control for the total effect of T on Y?

    That is a mediator.

Summary

  • Mediators carry part of the effect; controlling for them yields the direct effect, not the total.
  • In linear models the indirect effect is a * b, with strong extra assumptions on the M-Y relation.
  • Colliders, descendants of the outcome and M-bias variables are bad controls.
  • Choose controls by the graph, not by availability or timing alone.

Next: Front-Door Adjustment and the do-Operator