ML Systems Lab Open interactive version →
Intermediate 32 min read DAGconfoundingcollider bias

DAGs and Confounding

Directed acyclic graphs, backdoor criterion, collider bias

You observe that coffee drinkers have higher lung cancer rates. Should coffee drinkers stop? Probably not — because smoking confounds the relationship. Smokers both drink more coffee and have higher cancer rates. The Coffee → Cancer association is a spurious path through the confounder Smoking. Without a way to represent this structure, you would add "coffee drinker" as a control variable in a cancer regression and be satisfied. But whether that controls for the right thing, blocks the wrong thing, or introduces new bias depends entirely on the causal structure — and the regression output will not tell you which case you are in.

Directed Acyclic Graphs (DAGs) make that structure explicit. Nodes are variables. Directed arrows are direct causal claims. Three path types determine which variables to condition on. A confounding path runs Smoking → Coffee AND Smoking → Cancer: the backdoor path Treatment ← Confounder → Outcome. It must be blocked — condition on the confounder. A mediation path runs Treatment → Mediator → Outcome: the indirect causal channel. Conditioning on the mediator blocks the path you want to measure, isolating only the direct effect. Example: suppose a training program (T) raises quiz scores (Y) partly by increasing study hours (M) — a total effect of +12 points made up of a +9-point direct effect and a +3-point effect that runs through study hours. Control for study hours and the regression recovers only the +9-point direct effect, silently dropping the +3 points that ran through the mediator — a 25% understatement of the true total effect. That understatement only happens when the direct and mediated pieces share the same sign, as they do here; if they have opposite signs (suppression — say the program hurts scores directly but raises study hours enough to help overall), conditioning on the mediator can overestimate the total effect or flip its sign instead, because you have kept the piece that opposes the total rather than the whole picture. Either way, if the total effect is what you are measuring, leave the mediator out. A collider is caused by two otherwise-unconnected variables: A → Collider ← B. Conditioning on the collider opens a spurious path between A and B that was never causally present. Classic collider bias: conditioning on hospitalization (collider of disease severity and treatment choice) creates spurious correlation between diseases and treatments within the hospitalized sample.

The backdoor criterion formalizes this. If you can find a set Z that blocks all backdoor paths (confounding paths from Treatment to Outcome) without blocking any frontdoor paths and without containing any descendant of Treatment, you can identify the causal effect by conditioning on Z.

Sometimes you cannot block the backdoor path at all because the confounder is unmeasured. The frontdoor criterion handles that narrower case: if every Treatment → Outcome path routes through a single mediator M, and that mediator is itself unreachable from the unmeasured confounder, you can identify the effect by chaining two adjustments through M instead of one adjustment on the confounder. The canonical case is smoking, tar deposits, and lung cancer: an unmeasured genetic confounder might affect both smoking and cancer directly, so you cannot block that backdoor path with observed data — but if smoking's entire effect on cancer runs through tar deposits in the lungs, and the confounder does not reach tar directly, you can still identify the smoking effect by combining P(tar | smoking) with P(cancer | tar). This requires the mediator to carry the whole treatment effect and to sit outside the confounder's reach — a narrower condition than the backdoor criterion's, which is why frontdoor adjustment is rare in practice but valuable exactly when the confounder cannot be measured.

What this is not: "control for everything." Controlling for a collider creates bias where none existed before. Controlling for a mediator blocks the path you want to measure. Controlling for a post-treatment variable that is a descendant of Treatment can do both. You need the DAG to know which variables to condition on and which to leave out. A "control for everything" strategy without a DAG is a systematic way to introduce collider bias while believing you removed confounding.

Key points

Takeaway

Which variables you control for is a causal decision, not a statistical one — the regression cannot tell you whether your conditioning set was right, only what coefficient it produces given that set.

Recap

Check your understanding

Q1. You want to estimate the effect of exercise (T) on heart disease (Y). You have data on body weight (W). Select the two true statements about how to handle W.

Q2. A researcher conditions on 'hospitalisation status' when studying the effect of a drug on mortality. Why might this create collider bias?

Q3. What is the difference between the backdoor criterion and the frontdoor criterion, and when would you use the frontdoor criterion?

Q4. A researcher wants to know whether years of experience affects performance rating, so they regress performance rating on experience while using salary as a control variable. They find a strong negative coefficient on experience — more experienced employees appear to have systematically lower performance ratings once salary is held fixed. Should they trust this?

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →