Skip to content

brycewang-stanford/Auto-Empirical-Research-Skills / skills/11-James-Traina-compound-science/skills/causal-inference

causal-inference

Verified

This skill covers causal inference methods in observational and quasi-experimental settings. Use when the user is implementing, choosing between, or debugging causal identification strategies — including instrumental variables, difference-in-differences, regression discontinuity, synthetic control, or matching estimators. Triggers on "causal effect", "identification strategy", "instrumental variable", "2SLS", "GMM", "difference-in-differences", "DiD", "staggered treatment", "regression discontinuity", "RDD", "synthetic control", "matching", "propensity score", "IPW", "AIPW", "doubly robust", "LATE", "ATT", "ATE", "parallel trends", "exclusion restriction", "first stage", "weak instruments", or "endogeneity".

FileKindSize
SKILL.mdskill md12.3 KB
references/method-implementations.mdmarkdown10.9 KB
references/staggered-did.mdmarkdown3.3 KB
references/synthetic-control.mdmarkdown3.5 KB

references/method-implementations.md

View on GitHub
# Causal Inference: Full Method Implementation Code

Full implementation code for IV/2SLS, DiD, RDD, and Synthetic Control. Referenced from `SKILL.md`.

---

## IV/2SLS Full Implementation

### Standard 2SLS

```python
from linearmodels.iv import IV2SLS
import pandas as pd

# Basic 2SLS
model = IV2SLS.from_formula(
    'lwage ~ 1 + exper + expersq + [educ ~ nearc4 + nearc2]',
    data=df
)
result = model.fit(cov_type='robust')
print(result.summary)

# Key diagnostics accessible from result:
# result.first_stage — first stage results for each endogenous variable
```

### First-Stage Diagnostics

```python
# Always report the first stage
first_stage = IV2SLS.from_formula(
    'educ ~ 1 + exper + expersq + nearc4 + nearc2',
    data=df
).fit(cov_type='robust')

# Effective F-statistic (Stock-Yogo / Olea-Pflueger)
# Rule of thumb: F > 10 for single endogenous regressor
# For multiple endogenous regressors, use Cragg-Donald or Kleibergen-Paap

# With linearmodels, check:
# result.first_stage.diagnostics — partial F, Shea partial R²
```

### Weak Instruments

When instruments are weak (F < 10), 2SLS is biased toward OLS:

```python
# Anderson-Rubin test: valid regardless of instrument strength
from linearmodels.iv import IV2SLS

# LIML is less biased than 2SLS with weak instruments
from linearmodels.iv import IVLIML
model_liml = IVLIML.from_formula(
    'lwage ~ 1 + exper + expersq + [educ ~ nearc4 + nearc2]',
    data=df
)
result_liml = model_liml.fit(cov_type='robust')

# Compare 2SLS and LIML: if estimates differ substantially,
# weak instruments are a concern
```

### Overidentification

When you have more instruments than endogenous regressors:

```python
# Sargan-Hansen J-test (only valid under homoskedasticity for Sargan)
# result.sargan — Sargan test
# result.wooldridge_overid — robust overidentification test

# Interpretation: rejection means at least one instrument is invalid
# Caution: the test has low power with weak instruments
# Caution: passing the test does NOT prove instruments are valid
```

### IV in R and Stata

```r
# R: fixest
library(fixest)
result <- feols(lwage ~ exper + expersq | educ ~ nearc4 + nearc2, data = df,
                se = "hetero")
# First stage: result$iv_first_stage

# R: AER
library(AER)
result <- ivreg(lwage ~ exper + expersq + educ | exper + expersq + nearc4 + nearc2,
                data = df)
summary(result, diagnostics = TRUE)  # includes weak instrument, Wu-Hausman tests
```

```stata
* Stata: ivregress
ivregress 2sls lwage exper expersq (educ = nearc4 nearc2), robust
estat firststage  // first stage F-statistics
estat overid      // overidentification test (if over-identified)

* With fixed effects via ivregress + absorb:
ivregress 2sls y x (d = z), absorb(state_id) robust
```

---

## DiD Full Implementation

### Classic Two-Period DiD

```python
import statsmodels.formula.api as smf

# Standard 2x2 DiD
model = smf.ols('y ~ treated + post + treated:post', data=df)
result = model.fit(cov_type='cluster', cov_kwds={'groups': df['state']})

# The coefficient on treated:post is the DiD estimate
# Cluster standard errors at the level of treatment assignment
```

### Event Study / Dynamic DiD

```python
# Event study with leads and lags (relative time indicators)
# Omit period -1 as the reference

# Create relative time dummies
df['rel_time'] = df['year'] - df['treatment_year']
df.loc[df['rel_time'].isna(), 'rel_time'] = -1  # never-treated as reference

# Generate indicators for each relative time period
for k in range(-5, 8):
    if k == -1:
        continue  # omit reference period
    col = f'rel_{k}' if k >= 0 else f'rel_m{abs(k)}'
    df[col] = (df['rel_time'] == k).astype(int)

# Regression with fixed effects
import linearmodels.panel as plm

df = df.set_index(['unit', 'year'])
formula = 'y ~ ' + ' + '.join([f'rel_m{k}' for k in range(5, 1, -1)] +
                                [f'rel_{k}' for k in range(0, 8)]) + ' + EntityEffects + TimeEffects'

model = plm.PanelOLS.from_formula(formula, data=df)
result = model.fit(cov_type='clustered', cluster_entity=True)
```

### Pre-Trends Sensitivity

```python
# honestdid package (R) — no mature Python equivalent yet
# In R:
# library(HonestDiD)
# honest_did <- HonestDiD::createSensitivityResults(
#     betahat = event_study_coefs,
#     sigma = event_study_vcov,
#     numPrePeriods = 5,
#     numPostPeriods = 7,
#     Mvec = seq(0, 0.05, by = 0.01)  # grid of M values
# )
```

### Staggered DiD: Callaway-Sant'Anna

The `did` package in R implements C-SA with doubly robust estimation and flexible aggregation.

```r
library(did)

# Estimate group-time ATTs
atts <- att_gt(
    yname   = "y",
    tname   = "year",
    idname  = "unit_id",
    gname   = "first_treat",    # 0 for never-treated
    xformla = ~x1 + x2,         # covariates for doubly-robust adjustment
    data    = panel_df,
    est_method = "dr",           # doubly robust (recommended)
    control_group = "nevertreated"  # or "notyettreated"
)

# Aggregate to event-study
es <- aggte(atts, type = "dynamic", min_e = -5, max_e = 7)
ggdid(es)  # plot

# Aggregate to single ATT
overall <- aggte(atts, type = "simple")
summary(overall)
```

### Staggered DiD: Sun-Abraham

```r
library(fixest)

# sunab() integrates directly into feols
result <- feols(
    y ~ sunab(first_treat, year) + x1 + x2 | unit_id + year,
    data = panel_df,
    cluster = ~state_id
)

# Event study plot
iplot(result, main = "Sun-Abraham Event Study")

# Aggregate to simple ATT
summary(result, agg = "att")
```

### Bacon Decomposition

```r
library(bacondecomp)

# Decompose TWFE estimate into its components
decomp <- bacon(y ~ treat, data = panel_df, id_var = "unit_id", time_var = "year")
# Shows: how much weight falls on "clean" vs "contaminated" comparisons
# If contaminated comparisons have large weight, switch to C-SA or S-A

ggplot(decomp, aes(x = weight, y = estimate, shape = type, color = type)) +
    geom_point() + theme_minimal()
```

### de Chaisemartin-D'Haultfoeuille

Use when treatment can turn on AND off (treatment reversals):

```stata
* Stata: csdid and did_multiplegt
* did_multiplegt handles non-binary and reversing treatments
did_multiplegt y unit_id year treatment, robust_dynamic dynamic(5) placebo(3)
```

---

## RDD Full Implementation

### Sharp RDD

```python
# rdrobust package (available in Python, R, Stata)
from rdrobust import rdrobust, rdbwselect, rdplot

# Basic RD estimate
result = rdrobust(y=df['outcome'], x=df['running_var'], c=0)
print(result)
# Reports: point estimate, robust bias-corrected CI, bandwidth, N left/right

# Bandwidth selection
bw = rdbwselect(y=df['outcome'], x=df['running_var'], c=0)
# Reports: MSE-optimal and CER-optimal bandwidths

# RD plot
fig = rdplot(y=df['outcome'], x=df['running_var'], c=0,
             nbins=(20, 20))  # bins left and right of cutoff
```

### Fuzzy RDD

When crossing the threshold increases the probability of treatment but doesn't guarantee it:

```python
# Fuzzy RD = IV where the instrument is 1(X >= c)
result = rdrobust(
    y=df['outcome'],
    x=df['running_var'],
    c=0,
    fuzzy=df['treatment']  # actual treatment indicator
)
# Estimates LATE at the cutoff
```

### McCrary Density Test

Test for manipulation of the running variable around the cutoff:

```python
from rddensity import rddensity, rdplotdensity

# Test for bunching at the cutoff
density_test = rddensity(X=df['running_var'], c=0)
print(f"T-statistic: {density_test.hat['t']:.3f}")
print(f"P-value: {density_test.hat['p']:.3f}")

# Visual: density plot
fig = rdplotdensity(density_test, df['running_var'])
```

### RDD in R and Stata

```r
library(rdrobust)

# Basic estimate
rdd_result <- rdrobust(y, x, c = 0, all = TRUE)
summary(rdd_result)

# Bandwidth sensitivity
bw_result <- rdbwselect(y, x, c = 0, all = TRUE)

# Covariate balance test (run RD on pre-treatment covariate as placebo outcome)
balance_test <- rdrobust(covariate, x, c = 0)
```

```stata
rdrobust outcome running_var, c(0) all
rdbwselect outcome running_var, c(0) all
rdplot outcome running_var, c(0) nbins(20 20)
rddensity running_var, c(0) plot
```

### Bandwidth Sensitivity Table

```r
# Show results across a range of bandwidths
bandwidths <- c(0.5, 0.75, 1.0, 1.25, 1.5) * bw_optimal

results_bw <- lapply(bandwidths, function(h) {
    r <- rdrobust(y, x, c = 0, h = h, b = 2 * h)
    data.frame(bw = h, est = r$coef[1], ci_lo = r$ci[1, 1], ci_hi = r$ci[1, 2],
               n_l = r$N_h[1], n_r = r$N_h[2])
})
do.call(rbind, results_bw)
```

---

## Synthetic Control Full Implementation

### Basic Synth (R)

```r
library(Synth)

# Prepare data in the format required by Synth
dataprep_out <- dataprep(
    foo = panel_df,
    predictors = c("gdp", "population", "trade"),
    predictors.op = "mean",
    time.predictors.prior = 1970:1989,
    special.predictors = list(
        list("gdp", 1975, "mean"),
        list("gdp", 1980, "mean"),
        list("gdp", 1985, "mean")
    ),
    dependent = "gdp",
    unit.variable = "country_id",
    time.variable = "year",
    treatment.identifier = 7,    # treated unit ID
    controls.identifier = c(2, 13, 17, 29, 36),  # donor pool
    time.optimize.ssr = 1970:1989,   # pre-treatment period for optimization
    time.plot = 1970:2003
)

# Estimate synthetic control weights
synth_out <- synth(dataprep_out)

# Extract results
path.plot(synth_out, dataprep_out, Ylab = "GDP", Xlab = "Year",
          Legend = c("West Germany", "Synthetic"), Legend.position = "bottomright")
```

### Augmented Synthetic Control (augsynth)

```r
library(augsynth)

# augsynth augments SC with ridge regression to improve pre-treatment fit
asc <- augsynth(
    gdp ~ treated,
    unit = country_id,
    time = year,
    data = panel_df,
    progfunc = "Ridge",   # augmentation method
    scm = TRUE            # include standard SC component
)

summary(asc)
plot(asc)
```

### Permutation / Placebo Tests

```r
# Run SC for each donor unit as if it were treated
donor_ids <- c(2, 13, 17, 29, 36)  # donor pool
placebo_gaps <- list()

for (did in donor_ids) {
    # Re-run Synth treating donor unit as treated, all others as controls
    dp_placebo <- dataprep(
        foo = panel_df,
        ...,
        treatment.identifier = did,
        controls.identifier = setdiff(c(7, donor_ids), did)
    )
    synth_placebo <- synth(dp_placebo)
    placebo_gaps[[as.character(did)]] <- compute_gap(synth_placebo, dp_placebo)
}

# Plot all placebo gaps alongside treated unit gap
# Exclude placebos with poor pre-treatment fit (RMSPE > 2x treated RMSPE)
```

### Diagnostics

- **Pre-treatment RMSPE**: Measures synthetic control fit quality. RMSPE = sqrt(mean squared gap in pre-treatment period). Should be small relative to outcome variation.
- **Permutation test p-value**: Fraction of donor placebos with post/pre RMSPE ratio >= treated unit. Conventional threshold: p < 0.10.
- **Leave-one-out**: Drop each donor unit and re-estimate. Results should be stable.
- **Time placebo**: Move the "treatment date" to an earlier period where no effect should exist. Significant effects indicate pre-existing trends or overfitting.
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexUnverifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall