Causal inference mixtape
Skill Nicowyn/paper-audit-skills/.agents/skills/causal-inference-mixtape
Implement or audit causal inference designs in Python, R, or Stata. Use for difference-in-differences, event studies, instrumental variables, regression discontinuity, synthetic control, matching, randomization inference, parallel-trends diagnostics, weak-instrument diagnostics, or Bacon decomposition. Based on Scott Cunningham's Causal Inference: The Mixtape.From its SKILL.md
npx -y skills add Nicowyn/paper-audit-skills --skill causal-inference-mixtapeAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- 23 days oldThe repository was created 23 days ago. New is not bad, but a brand new repository carrying a familiar-sounding name is the shape a typosquat arrives in, and there has been no time for anyone else to find a problem with it.
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
8.3 KB, ~2.0k tokens by cl100k_base, as published. Nobody here has run it
Causal Inference: The Mixtape — Code Skill
Practitioner-oriented causal inference skill adapted from the upstream Causal Inference Mixtape repository. It covers 10 identification strategies with illustrative Python, R, and Stata patterns. Every pattern is an adapt-and-verify scaffold, not a ready-to-run or automatically valid analysis.
Before using a pattern, reconstruct the study's assignment mechanism and estimand, adapt the sample and variance estimator, check the installed package API, and validate the adapted code on a known-answer example or simulation. A syntactically valid template does not establish identification.
Methods Covered
| Method | Python | R | Stata | Reference |
|---|---|---|---|---|
| OLS / Regression | statsmodels | estimatr | reg/reghdfe | Method patterns §1 |
| Difference-in-Differences | statsmodels + C() | lfe/fixest | xtreg/reghdfe | Method patterns §2 |
| Event Study (Dynamic DiD) | manual lead/lag | estimatr | reghdfe | Method patterns §3 |
| Staggered DiD / TWFE | statsmodels | bacondecomp | bacondecomp | Method patterns §4 |
| Regression Discontinuity | statsmodels local regression | rdrobust/rddensity | rdplot/rdrobust/rddensity | Method patterns §5 |
| Instrumental Variables | linearmodels IV2SLS | AER/ivreg | ivregress 2sls | Method patterns §6 |
| Synthetic Control | rpy2 → R Synth | Synth + SCtools | synth | Method patterns §7 |
| Matching / PSM / IPW | manual logit + weights | MatchIt + Zelig | teffects/cem | Method patterns §8 |
| DAGs / Collider Bias | dagitty (conceptual) | dagitty/ggdag | — | Method patterns §9 |
| Randomization Inference | explicit assignment draws | ri2 | ritest | Method patterns §10 |
Core Workflow
Implement a Causal Method
- Identify the method from the table above
- Load the appropriate illustrative pattern from references/method-patterns.md
- Adapt variable names, fixed effects, and clustering to the user's data
- Add design-appropriate diagnostics (parallel trends for DiD, density and continuity checks for RDD, and first-stage plus weak-instrument diagnostics for IV)
Choose the Right Language
| Scenario | Recommendation |
|---|---|
| ML pipeline integration | Python (statsmodels + linearmodels) |
| Synthetic Control | R (Synth package) or Stata (synth) — Python lacks mature implementation |
| Bacon decomposition | R (bacondecomp) or Stata — no Python equivalent |
| Publication-ready tables | Stata (outreg2/esttab) or R (stargazer/modelsummary) |
| Coarsened Exact Matching | Stata (cem) or R (MatchIt) — no Python equivalent |
| Quick prototyping | Python with statsmodels |
Cross-Language Equivalents
| Task | Python | R | Stata |
|---|---|---|---|
| OLS with robust SE | smf.ols().fit(cov_type='HC1') | lm_robust() | reg y x, robust |
| Cluster SE | fit(cov_type='cluster', cov_kwds={'groups': g}) | `felm(y ~ x | 0 |
| Two-way FE | C(id) + C(time) in formula | `felm(y ~ x | id + time)` |
| IV / 2SLS | IV2SLS.from_formula('y ~ 1 + exog + [endog ~ inst]') | `ivreg(y ~ exog | inst)` |
| DiD | C(treat)*C(post) | treat:post in formula | did_multiplegt or interaction |
Key Python Patterns
DiD with Cluster-Robust SE
import statsmodels.formula.api as smf
model = smf.ols('y ~ C(treated)*C(post) + controls', data=df)
results = model.fit(cov_type='cluster', cov_kwds={'groups': df['firm_id']})
Event Study (Lead/Lag)
# Omit event time -1 from estimation and use it as the reference period.
def event_term(k):
return f'rel_m{abs(k)}' if k < 0 else f'rel_{k}'
event_times = [k for k in range(-4, 5) if k != -1]
rel_cols = [event_term(k) for k in event_times]
for k, col in zip(event_times, rel_cols):
df[col] = (df['relative_time'] == k).astype(int)
formula = 'y ~ ' + ' + '.join(rel_cols) + ' + C(id) + C(year)'
IV / 2SLS
from linearmodels.iv import IV2SLS
model = IV2SLS.from_formula('y ~ 1 + exog + [endog ~ instrument]', data=df)
results = model.fit(cov_type='clustered', clusters=df['cluster_var'])
Design and Robustness Check Patterns
| Method | Checks to adapt to the design |
|---|---|
| DiD | Parallel trends (event study plot), placebo treatment dates |
| RDD | Sorting-risk diagnostics (which may include a density-discontinuity test), bandwidth sensitivity, local-polynomial specification checks, and covariate continuity |
| IV | Design-appropriate first-stage diagnostics, weak-instrument-robust inference, a design/mechanism argument for exclusion with falsifiable implications, and cautious interpretation of over-identification tests when applicable |
| Synthetic Control | Pre-treatment RMSPE, placebo distribution, leave-one-out |
| Matching | Covariate balance table, caliper sensitivity |
Common Pitfalls
- TWFE under staggered adoption — variation in treatment timing alone does not prove bias. With heterogeneous or dynamic treatment effects, forbidden comparisons involving already-treated units can make conventional TWFE differ from the target ATT. Diagnose its comparisons and weights; then use an estimator such as Sun–Abraham or Callaway–Sant'Anna when its assumptions match the design.
- Synthetic Control with many treated units — the Synth package handles one treated unit. For multiple, use augmented synthetic control or stacked approach.
- RDD sorting diagnostics — a density discontinuity can be a signal of sorting or manipulation risk, but it is not a test of researcher or participant intent and is not informative in every design. Choose among McCrary-style,
rddensity, heaping/mass-point, institutional, and covariate-continuity checks as the running variable and assignment process warrant. - IV weak instruments — do not treat a first-stage F-statistic cutoff of 10 as universal proof of instrument strength. Match the diagnostic to the number of endogenous regressors, covariance estimator, and design; report weak-instrument-robust confidence sets or tests when appropriate.
- Python Synth gap — no mature Python Synth package exists. Use
rpy2to call R'sSynthfrom Python. - IV diagnostic overclaims — exclusion is primarily a design and mechanism claim. A placebo is interpretable only when its outcome or covariate is predetermined, and failure to reject an over-identification test does not prove that every instrument is valid.
- Randomization inference mechanism — redraw assignments from the original assignment mechanism. The Python pattern below covers only complete randomization with a fixed treated count; blocked, stratified, paired, or clustered designs need corresponding constrained draws.
Additional Resources
Reference Files
- references/method-patterns.md — Illustrative patterns for all 10 methods; adapt and verify each one
- references/r-stata-comparison.md — Cross-language package comparison and method coverage gaps
- references/third-party-notices.md — Direct upstream provenance, local-change disclosure, and the MIT license required for redistribution
Prompt Files
- prompts/01-implement-method.md — Adaptation prompt for drafting a causal-method workflow
- prompts/02-robustness-checks.md — Adaptation prompt for drafting design-specific robustness checks
What ships with it: 6 files
35.0 KB alongside SKILL.md
agents/
- openai.yaml240 B
prompts/
- 01-implement-method.md4.1 KB
- 02-robustness-checks.md5.3 KB
references/
- method-patterns.md18.6 KB
- r-stata-comparison.md4.8 KB
- third-party-notices.md2.0 KB