Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Section 4.5 — Model selection for causal inference

This notebook contains the code examples from Section 4.5 Model selection for causal inference from the No Bullshit Guide to Statistics.

Notebook setup
Note: you may need to restart the kernel to use updated packages.
<Figure size 640x480 with 0 Axes>
datasets/ directory present and ready.

Definitions

Causal graphs

Simple graphs

More complicated graphs

Unobserved confounder

The fork pattern

Example 1: simple fork

Consider a dataset where there is zero causal effect between XX and YY.

First let’s fit a native model y ~ x to see what happens.

Intercept -0.118596 x 1.268185 dtype: float64
<Figure size 700x400 with 1 Axes>

Recall the true strength of the causal association X→YX \to Y is zero, so the β^x=1.268\widehat{\beta}_{\tt{x}}=1.268 is a wrong estimate.

Now let’s fit another model that controls for the common cause WW, and thus removes the confounding.

Intercept -0.089337 x 0.077516 w 2.859363 dtype: float64

The model lm1b correctly recovers causal association close to zero.

<Figure size 700x400 with 1 Axes>

Example 2: student’s memory capacity

First let’s fit a model that doesn’t account for the common cause.

Intercept 64.275333 effort -0.840428 dtype: float64

We see a negative effect β^effort=−0.84\widehat{\beta}_{\tt{effort}}=-0.84, the true effect is +2.

Now we fit a model that includes mem, i.e., we control for the common cause confounder.

Intercept 2.650981 effort 1.889825 mem 9.568862 dtype: float64

The model lm2b correctly recovers causal association 1.89, which is close to the true value 2.

Benefits of random assignment

TODO explain

Randomly assigned effort

Example 2R: random assignment of the effort variable

Suppose students are randomly assigned into a low effort (5h/week) and high effort (15h/week) groups by flipping a coin.

The effect of the random assignment is to decouple effort from mem, thus removing the association, as we can see if we compare the correlations in the original dataset df2 and the randomized dataset df2r.

(-0.9452404905554658, 0.03700564639546951)

Randomization allows us to recover the correct estimate, even without including the common cause.

Intercept 49.176242 effort 2.077639 dtype: float64

Note we can also including mem in the model, and that doesn’t hurt.

Intercept -0.343982 effort 2.002295 mem 10.063906 dtype: float64

The pipe pattern

Example 3: simple pipe

First we fit the model y ~ 1 + x that doesn’t include the variable MM, which is the correct thing to do.

Intercept 0.060272 x 0.922122 dtype: float64
<Figure size 700x400 with 1 Axes>

However, if we were choose to include MM in the model, we get a completely different result.

Intercept 0.081242 x -0.005561 m 0.965910 dtype: float64
<Figure size 700x400 with 1 Axes>

Example 4: student competency as a mediator

Students score as a function of effort. We assume the final scores for the course was mediated through improvements in competency with the material (comp).

The causal graph in this situation is an instance of the mediator pattern, so the correct modelling decision is not to include the comp variable, which allows us to recover the correct parameter.

Intercept -0.538521 effort 6.079663 dtype: float64

However, if we make the mistake of including the variable comp in the model, we will obtain a zero parameter for the effort, which is a wrong conclusion.

Intercept 0.545934 effort -0.030261 comp 2.979818 dtype: float64

It even appears that effort has a small negative effect.

The collider pattern

Example 5: simple collider

Note y is completely independent of x. And if we were to fit the model y ~ x we get the correct result.

Intercept -0.021710 x -0.039576 dtype: float64
<Figure size 700x400 with 1 Axes>

Including ZZ in the model incorrectly shows X→YX \to Y association.

Intercept -0.170415 x -0.244277 z 1.639660 dtype: float64
<Figure size 700x400 with 1 Axes>

Example 6: student GPA as a collider

When we don’t adjust for the collider gpa, we obtain the correct effect of effort on scores.

Intercept 12.747478 effort 4.717006 dtype: float64

But with adjustment for gpa reduces the effect significantly.

Intercept 1.464950 effort 1.177600 gpa 16.624215 dtype: float64

Example 6SB: selection bias

Suppose the study was conducted only on students who obtained an award, which is given to students with GPA 3.4 or greater.

Intercept 78.588495 effort -0.126503 dtype: float64

A negative causal association appears, which is very misleading.

Explanations

Backdoor path criterion

Revisiting the three patterns
Examples of more complicated graphs
(a) Pipe and fork

Summary:

  • confounder of the mediator MM also confounds XX and YY

  • unadjusted estimate is biased

  • adjusting for ZZ blocks backdoor path

  • ZZ is a good control

Intercept   -0.034927
x            1.458856
dtype: float64
Intercept   -0.008234
x            0.932850
w            1.072638
dtype: float64
(b) M-bias example

Summary:

  • although ZZ is a pre-treament variable, as well as correlated both with XX and YY, it is not a confounder

  • unadjusted estimate is unbiased

  • adjusting for ZZ opens the colliding path X←P→Z←Q→YX \leftarrow P \rightarrow Z \leftarrow Q \rightarrow Y

  • ZZ is a bad control

Intercept   -0.205050
x            0.964068
dtype: float64
Intercept   -0.171269
x            1.720329
z           -1.592446
dtype: float64
(c) Indirect confounder
Intercept   -0.134730
x            0.487507
dtype: float64
Intercept   -0.083954
x            1.014572
w           -1.068072
dtype: float64

Three different goals when building models

The Table 2 fallacy

Case study: smoking and lung function in teens

     age    fev  height sex smoke
651   18  2.853    60.0   F    NS
652   16  2.795    63.0   F    SM
653   15  3.211    66.5   F    NS
           age      fev   height
count  654.000  654.000  654.000
mean     9.931    2.637   61.144
std      2.954    0.867    5.704
min      3.000    0.791   46.000
25%      8.000    1.981   57.000
50%     10.000    2.548   61.500
75%     12.000    3.118   65.500
max     19.000    5.793   74.000
smoke NS 2.566143 SM 3.276862 Name: fev, dtype: float64
0.7107189238605196
<Figure size 700x400 with 1 Axes>
<Figure size 700x400 with 1 Axes>

Fit the unadjusted model

Intercept 2.566143 C(smoke)[T.SM] 0.710719 dtype: float64

The parameter estimate β^smoke\widehat{\beta}_{\texttt{smoke}} we obtain from the unadjusted model echoes the simple difference in means we calculated earlier. Any negative effect of smoke on fev is drowned by the confounding variable age, which is positively associated with both smoke and fev.

Draw a causal graphs

Filled in causal graph for the students smoking affect FEV.

Fit the adjusted model

Intercept 0.237771 C(smoke)[T.SM] -0.153974 C(sex)[T.M] 0.315273 age 0.226794 dtype: float64

The interpretation of β^smoke=−0.153974\widehat{\beta}_{\texttt{smoke}} = -0.153974 is that, according to the adjusted model, smoking decreases your FEV by 0.153974 litres on average.

Practical significance

To better understand the real-world effect of smoking, let’s think about two 15 year old females: one who smokes and the other who doesn’t.

For the 15 year old nonsmoking female, the expected mean FEV is:

3.6396839416862914

For a 15 year old, smoking female, the expected mean FEV is:

3.4857098266910547

The relative reduction in FEV is:

0.04230425428750262

A 4.2%4.2\% reduction in FEV is practically significant, and definitely worth avoiding, especially at this early age.

Discussion

Metrics-based variable selection

Fit the short model

Fit long model with useful loc variable.

Fit long model with useless variable permit.

Comparing metrics
(0.8390497506713147, 0.8506062566189325, 0.8407115273797525)
(1103.2518084235273, 1092.5985552344712, 1102.603062693656)

The value lm2p.aic = 1102.60 is smaller than lm2.aic, but not not that different.

(1115.4512324525256, 1107.8478352707189, 1117.8523427299037)
(12.758115596295536, 0.00047598123084920574)

The pp-value is smaller than 0.05, so we conclude that adding the variable loc improves the model.

(2.5857397307382257, 0.10991892492566138)

The pp-value is greater than 0.05, so we conclude that adding the variable permit doesn’t improve the model.

Stepwise regression

Uses and limitations of causal graphs

Further reading on causal inference

Exercises

TODO

CUT MATERIAL

Bonus examples

Intercept 99.602100 ltime -0.395873 dtype: float64
Intercept 3.580677 ltime 0.081681 iq 0.482373 dtype: float64
Intercept -0.045587 learn 14.890022 dtype: float64
Intercept -0.036520 learn 0.130609 know 2.975806 dtype: float64

Without adjusting for the collider prom, there is almost no effect of the network ability on competence.

Intercept 0.071632 ntwrk -0.041152 dtype: float64

But with adjustment for prom, there seems to be a negative effect.

Intercept -0.290081 ntwrk -0.239975 prom 1.087964 dtype: float64

The false negative effect can also appear from sampling bias, e.g. if we restrict our analysis only to people who were promoted.

Intercept 0.898530 ntwrk -0.426244 dtype: float64

Bonus example 4: benefits of random assignment

Example 4 from Why We Should Teach Causal Inference: Examples in Linear Regression With Simulated Data

(-0.9979264589333364, -0.020129851374243904)
ltime 80 99.980423 120 99.358152 Name: iq, dtype: float64
Intercept 50.688293 ltime 0.091233 dtype: float64
Intercept -0.303676 ltime 0.099069 iq 0.503749 dtype: float64
Intercept 9.896117 iq 0.501168 dtype: float64
Side-quest (causal influence of mem)

Suppose we’re interested in ... mem wasn’t randomized, but because we de

Intercept 17.382804 mem 10.430159 dtype: float64