Drinking age with a scikit-learn model#

This example uses the sharp regression discontinuity design to make claims about the causal effects of the minimum legal drinking age (21 in the USA) upon all cause mortality rates. The dataset is from a study by Carpenter and Dobkin [2009].

from sklearn.linear_model import LinearRegression

import causalpy as cp
%config InlineBackend.figure_format = 'retina'
df = (
    cp.load_data("drinking")
    .rename(columns={"agecell": "age"})
    .assign(treated=lambda df_: df_.age > 21)
    .dropna(axis=0)
)
result = cp.RegressionDiscontinuity(
    df,
    formula="all ~ 1 + age + treated",
    running_variable_name="age",
    model=LinearRegression(),
    treatment_threshold=21,
)
/Users/benjamv/git/CausalPy/causalpy/experiments/base.py:235: UserWarning: LinearRegression had fit_intercept=True, but CausalPy requires fit_intercept=False because the intercept is already included in the design matrix by patsy. A cloned copy of the model with fit_intercept=False will be used; the original instance is unchanged.

Examine results#

result.plot();
../_images/8ce744bb88c05171938c6ccef168802d3e09c381d3d27e4daff2d0331ecaadf2.png
result.summary()
Regression Discontinuity experiment
Formula: all ~ 1 + age + treated
Running variable: age
Threshold on running variable: 21
Bandwidth: inf
Donut hole: 0.0
Observations used for fit: 48

Results:
Discontinuity at threshold = 7.66


Model coefficients:
  Intercept      	       112
  treated[T.True]	       7.7
  age            	     -0.97

We can get nicely formatted tables from our integration with the maketables package.

from maketables import ETable

ETable(result, coef_fmt="b:.3f")
all
(1)
coef
treated=True 7.663
age -0.975
Intercept 112.310
stats
N 48
R2 0.595
Format of coefficient cell: Coefficient