SKILL.md
Causal Inference Guide
A skill for applying quasi-experimental causal inference methods in observational research. Covers difference-in-differences, instrumental variables, regression discontinuity designs, and synthetic control methods with implementation code and diagnostic checks.
Difference-in-Differences (DiD)
Classic Two-Period DiD
import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
def did_estimation(df: pd.DataFrame, outcome: str, treatment: str,
post: str, covariates: list[str] = None) -> dict:
"""
Estimate a difference-in-differences model.
Args:
df: Panel DataFrame
outcome: Name of outcome variable column
treatment: Name of treatment group indicator (0/1)
post: Name of post-treatment period indicator (0/1)
covariates: Optional list of control variable names
"""
# Create interaction term
df = df.copy()
df['did'] = df[treatment] * df[post]
# Build formula
formula = f"{outcome} ~ {treatment} + {post} + did"
if covariates:
formula += ' + ' + ' + '.join(covariates)
model = smf.ols(formula, data=df).fit(cov_type='cluster',
cov_kwds={'groups': df.get('unit_id', df.index)})
return {
'did_estimate': model.params['did'],
'se': model.bse['did'],
'p_value': model.pvalues['did'],
'ci_95': (model.conf_int().loc['did', 0], model.conf_int().loc['did', 1]),
'r_squared': model.rsquared,
'n_obs': model.nobs,
'interpretation': (
f"The treatment effect is {model.params['did']:.3f} "
f"(SE = {model.bse['did']:.3f}, p = {model.pvalues['did']:.4f}). "
f"{'Statistically significant' if model.pvalues['did'] < 0.05 else 'Not significant'} "
f"at the 5% level."
)
}
Parallel Trends Test
The key identifying assumption. Test it with pre-treatment data:
def test_parallel_trends(df: pd.DataFrame, outcome: str,
treatment: str, time: str,
treatment_period: int) -> dict:
"""
Test the parallel trends assumption using event study specification.
"""
df = df.copy()
pre_periods = sorted(df[df[time] < treatment_period][time].unique())
# Create period dummies interacted with treatment
for t in pre_periods:
df[f'pre_{t}'] = ((df[time] == t) & (df[treatment] == 1)).astype(int)
period_vars = [f'pre_{t}' for t in pre_periods[:-1]] # omit last pre-period (reference)
formula = f"{outcome} ~ {' + '.join(period_vars)} + C({time}) + C(unit_id)"
model = smf.ols(formula, data=df).fit()
# Joint F-test: all pre-treatment interactions = 0
f_test = model.f_test(' = '.join([f'{v} = 0' for v in period_vars]))
return {
'pre_period_coefficients': {v: model.params[v] for v in period_vars},
'f_statistic': f_test.fvalue[0][0],
'f_pvalue': f_test.pvalue,
'parallel_trends_hold': f_test.pvalue > 0.05,
'interpretation': (
'Parallel trends assumption supported (cannot reject joint null)'
if f_test.pvalue > 0.05
else 'WARNING: Parallel trends assumption may be violated'
)
}
