SKILL.md
Difference-in-Differences (DID) Skill
This skill guides complete DID analysis: from assumption validation and model specification to staggered treatment designs and event study regressions. Designed for policy evaluation and natural experiment settings.
Core DID Logic
DID compares the change in outcomes for a treatment group before and after treatment to the change for a control group over the same period.
DID Estimator = (Ȳ_treat,post − Ȳ_treat,pre) − (Ȳ_ctrl,post − Ȳ_ctrl,pre)
Key Assumption (Parallel Trends): In the absence of treatment, the treatment group's outcome would have evolved in parallel with the control group.
DID Workflow
- Design check: Confirm treatment/control assignment and timing
- Parallel trends: Test with pre-treatment event study regression
- Baseline regression: 2×2 DID or TWFE regression
- Staggered design check: If adoption dates vary, use robust estimators
- Robustness: Placebo treatment, alternative control groups, callaway-santanna
Basic 2×2 DID Model
Y_it = β₀ + β₁·Treat_i + β₂·Post_t + β₃·(Treat_i × Post_t) + ε_it
β₃ = DID estimate (ATT)
Code Templates
# Python — 2×2 DID with TWFE
import statsmodels.formula.api as smf
# Simple 2x2
model = smf.ols('y ~ treat + post + treat_post', data=df).fit(cov_type='HC3')
# TWFE with entity and time FE (preferred)
from linearmodels.panel import PanelOLS
df_panel = df.set_index(['entity_id', 'year'])
twfe = PanelOLS(df_panel['y'], df_panel[['treat_post']],
entity_effects=True, time_effects=True)
result = twfe.fit(cov_type='clustered', cluster_entity=True)
print(result.summary)
# R — TWFE
library(plm); library(lmtest); library(sandwich)
panel_df <- pdata.frame(df, index = c("entity_id", "year"))
twfe <- plm(y ~ treat_post, data = panel_df, model = "within", effect = "twoways")
coeftest(twfe, vcov = vcovHC(twfe, cluster = "group"))
