Day 57. Selection Bias in DAGs
Who ends up in your data is itself a variable. Add a selection node S to the DAG; analysing only S = 1 is conditioning on S, and when S is a collider or a descendant of the outcome, associations, even in randomised trials, are distorted. Graphs say when the bias can be undone and how.
- ●Add a selection node to a DAG and diagnose collider-stratification, outcome-dependent and survivorship bias.
- ●State and prove the conditions under which P(y | x) and odds ratios are recoverable from selected data.
- ●Correct selection bias with external covariate data or inverse-probability-of-selection weights.
- ●Recognise selection bias in case-control studies, testing data, wage regressions and trials with loss to follow-up.
Theory
The selection node
Let indicate that a unit is in the data set. Draw in the DAG with arrows from whatever determines inclusion. Every analysis of the sample conditions on , so the d-separation rules apply: if is a collider (or a descendant of one) on a path between and , that path is opened.
Typical structures
| Structure | Example | Effect |
|---|---|---|
| hospital patients (Berkson), tested individuals | spurious association | |
| trial drop-out due to side effects and poor health | bias even with randomised | |
| wages observed only for the employed, case-control sampling | distorted | |
| , | studying survivors, firms that survived | survivorship bias |
Recoverability
With only selected data we can estimate . A quantity is recoverable if it can be written in terms of this (plus any external data).
- Conditional distributions: is recoverable from the selected sample alone if in the graph; then (Bareinboim and Pearl).
- With external data on covariates: if and is known from a population source (a census),
- Weighting: equivalently, if and is known or estimable, weight selected units by (inverse probability of selection weighting; Day 14 used the same idea for missing data).
- Odds ratios: if selection depends only on (and on nothing else that relates to ), the – odds ratio is unchanged by selection. That is why case-control studies estimate odds ratios, not risks.
Causal effects under selection
For we need two things at once: a back-door set , and a way to undo selection, for example plus population data on :
Randomisation removes confounding but not selection bias created after randomisation.
Part 1 · Warm-up
Exercise 1 · Is selection a collider?
For each, is the – association in the selected sample biased? (a) , . (b) , . (c) , . (d) , , with unrelated to .
Show solution
Step 1. (a) (the only path is blocked by ): unbiased.
Step 2. (b) and are adjacent: in general.
Step 3. (c) Collider: a spurious association appears.
Step 4. (d) is d-separated from : no bias (just a smaller sample).
Exercise 2 · Check recoverability
DAG: , , . Is recoverable from the selected sample alone? Is it recoverable with census data on ?
Show solution
Step 1. Paths –: and . Given alone, the first stays open: fails. Not recoverable from the sample alone.
Step 2. Given and , both paths are blocked: .
Answer: recoverable with external data: .
Exercise 3 · Odds ratios survive outcome selection
In a population, and . A study keeps all units with and 10% of units with . Compute the odds ratio in the population and in the sample.
Show solution
Step 1. Population OR: .
Step 2. In the sample, the odds for become and for , .
Step 3. Sample OR: .
Answer: 2.25 in both; the factor 0.1 cancels.
Exercise 4 · Re-weighting to the population
In a selected sample, and , and . A census shows , while in the sample it is 0.6. Recover and compare with the naive sample value.
Show solution
Step 1. Recovered: .
Step 2. Naive (sample mix): .
Answer: 0.29; the sample over-represents .
Exercise 5 · Survivors
A study of 10-year-old firms finds that firms that started with high debt are as profitable as low-debt firms. Draw a DAG with debt , survival , managerial quality (unobserved) and profitability , and explain why the finding may mislead.
Show solution
Step 1. (debt raises failure risk), , , possibly .
Step 2. Studying survivors conditions on the collider : among survivors, high-debt firms needed better managers to survive, so they have higher .
Answer: the path is opened, pushing high-debt survivors' profits up; the effect of debt is understated.
Part 2 · Proofs
Exercise 6 · Selection independent of the outcome
Prove that if , then .
Show solution
Step 1. By the global Markov property, the d-separation implies in the distribution.
Step 2. So for every with , in particular .
Exercise 7 · Odds-ratio invariance
Suppose selection depends only on : . Prove that the odds ratio of and is the same in the selected sample as in the population.
Show solution
Step 1. .
Step 2. The odds ratio is (arguments ).
Step 3. In the sample, each cell is multiplied by ; numerator and denominator each contain one and one cell, so the factors cancel.
Risk ratios do not cancel this way, so case-control studies cannot estimate absolute risks without external information.
Exercise 8 · Recovery with external covariate data
Prove: if , then .
Show solution
Step 1. Law of total probability: .
Step 2. By the d-separation and Exercise 6's argument, .
Step 3. Substitute. The first factor comes from the sample, the second from the census.
Exercise 9 · Inverse probability of selection weighting
Assume and . Prove .
Show solution
Step 1. By the LIE, .
Step 2. , by conditional independence.
Step 3. So .
Exercise 10 · Randomisation does not protect against drop-out
In a trial, is randomised, , and units stay in the study () when (the programme's support visits encourage staying, and healthier patients stay), with independent normals. Explain with d-separation why in general.
Show solution
Step 1. DAG: , , , .
Step 2. Conditioning on opens .
Step 3. Here makes staying easier, so treated units stay even with low , while controls need a higher to stay. Hence .
Step 4. The completers' contrast equals .
Part 3 · Applied
Exercise 11 · A case-control study of smoking and lung cancer
In the population, 1% have lung cancer . Among cases 60% smoke; among non-cases 20% smoke. A case-control study samples cases and an equal number of controls. Compute the odds ratio and the risk ratio in the population and in the study.
Show solution
Step 1. Population OR (exposure odds among cases versus controls): .
Step 2. Population risks: ; . RR .
Step 3. In the 1:1 study (per 1 case, 1 control): smokers cases and controls, non-smokers and . OR .
Step 4. Study "risks": and ; RR .
Answer: the OR (6) is preserved; the risk ratio is not (2.25 in the sample vs 5.85). With a rare disease the OR approximates the RR.
Exercise 12 · Does smoking protect against COVID-19?
In a population, 20% smoke () and 5% are infected (), independently. Only tested people are in the data: (smokers cough more; infection causes symptoms). Compute the test positivity for smokers and non-smokers among the tested.
Show solution
Step 1. Smokers: tested and infected ; tested and not infected .
Step 2. Positivity among tested smokers: .
Step 3. Non-smokers: and ; positivity .
Answer: 24% versus 62%: smokers appear protected, although smoking and infection are independent. Testing is a collider (). Early pandemic studies reported exactly this pattern.
Exercise 13 · Women's wages and selection into work
Wage offers depend on education: . Women work () if the offer exceeds a reservation wage that rises with the number of young children . Wages are observed only for workers. In the simulation below (38% work), what does the regression of observed log wages on education find, and why?
Show solution
Step 1. DAG: . Selection depends on the outcome itself.
Step 2. fails (): .
Step 3. Among workers, low-education women are in the sample only if they drew a high , which raises their average wage and flattens the education gradient.
Answer: the simulation finds a slope of about 0.056 instead of 0.10. Heckman's selection model uses , which affects work but not wage offers, to correct this: the same exclusion idea as an instrument.
Exercise 14 · Drop-out in a trial of an exercise programme
A trial randomises an exercise programme for heart patients; outcome is fitness at one year. Baseline health (observed) raises fitness and lowers drop-out; the programme itself raises drop-out (it is demanding). In the simulation below, compare the completers' difference in means with the truth (1.0), and with an inverse-probability-of-completion-weighted (IPCW) analysis.
Show solution
Step 1. DAG: , , , . Completers condition on , opening .
Step 2. Completers' contrast: about 1.35. Programme completers are healthier at baseline than control completers, because less healthy patients drop out of the demanding programme more.
Step 3. holds, so weighting completers by recovers the full-trial comparison (Exercise 9).
Answer: IPCW gives about 1.01; the completers' analysis overstates the benefit by a third.
Exercise 15 · Studying successful entrepreneurs
A business school surveys founders of firms that reached $10 million in revenue and finds that founders without a university degree performed as well as graduates, concluding that "education doesn't matter for entrepreneurial success". Draw the DAG and explain the flaw. What data would answer the question?
Show solution
Step 1. DAG: education success , talent and luck ; the sample is selected on itself (the outcome).
Step 2. Within the sample, the outcome no longer varies much, and among successes, founders without degrees must have had more talent or luck: conditioning on the collider opens .
Answer: selecting on the outcome destroys the comparison. Follow a cohort of all founders (or all potential founders) and compare success rates by education, adjusting for pre-founding confounders.
Verify in Python
import numpy as np
# Exercise 11: case-control sampling
pD, pS_case, pS_ctrl = .01, .6, .2
or_pop = (pS_case / (1 - pS_case)) / (pS_ctrl / (1 - pS_ctrl))
r1 = pD * pS_case / (pD * pS_case + (1 - pD) * pS_ctrl); r0 = pD * (1 - pS_case) / (pD * (1 - pS_case) + (1 - pD) * (1 - pS_ctrl))
print(f"population OR {or_pop:.2f}, RR {r1 / r0:.2f}; 1:1 study OR {(.6 * .8) / (.4 * .2):.2f}, RR {(.6 / .8) / (.4 / 1.2):.2f}")
# Exercise 12: COVID testing
for x in (1, 0):
pos = .05 * (.02 + .10 * x + .60); neg = .95 * (.02 + .10 * x)
print(f"smoker={x}: positivity among tested {pos / (pos + neg):.2f}")
rng = np.random.default_rng(57); n = 1_000_000
# Exercise 13: selection into work
E = rng.normal(12, 2, n); W = 1.0 + .10 * E + rng.normal(0, .5, n); K = rng.binomial(2, .5, n)
S = (W - .3 * K + rng.normal(0, .3, n)) > 2.1
print(f"work share {S.mean():.2f}; slope all {np.polyfit(E, W, 1)[0]:.3f}, workers only {np.polyfit(E[S], W[S], 1)[0]:.3f}")
# Exercise 14: drop-out in a trial and IPCW
X = rng.random(n) < .5; L = rng.normal(size=n); Y = 1.0 * X + 2 * L + rng.normal(size=n)
pS = 1 / (1 + np.exp(-(1.0 - 1.0 * X + 1.5 * L))); R = rng.random(n) < pS # L = baseline health
w = 1 / pS
ipcw = (w * Y * (X & R)).sum() / (w * (X & R)).sum() - (w * Y * (~X & R)).sum() / (w * (~X & R)).sum()
print(f"completion {R.mean():.2f}; full trial {Y[X].mean() - Y[~X].mean():.3f}, completers {Y[X & R].mean() - Y[~X & R].mean():.3f}, IPCW {ipcw:.3f}")
Key takeaways
- Being in the data is a variable; analysing a selected sample conditions on it.
- Selection on a collider, on a descendant of the outcome, or on a post-treatment variable biases associations, even in randomised trials.
- Recovery needs a d-separation between outcome and selection given what you can measure, plus external data or selection weights; odds ratios survive outcome-only selection.
Simulation Lab: Selection on Testing
Open full screen →Only the tested enter the data: smoking can look protective against infection, and only the odds ratio can survive.
Only tested people enter the data. Testing depends on smoking (cough) and on infection (symptoms), so it is a collider. Set the true relative risk to 1 and watch smokers look protected among the tested. With no smoking effect on testing (selection on the outcome only), the odds ratio survives selection even though risks do not.