DIDFEPanel

Classic 2×2 Difference-in-Differences (DID): Model Specification, Estimation, and Inference

A systematic introduction to the classic difference-in-differences method, two-way fixed effects estimation, clustered standard error selection, with complete Stata code.

作者:Econometrics Research Navigation Station发布:2025-03-10★★★

Structure of This Article

  1. Principles: The identification logic and core assumptions of DID
  2. Intuition: The Card & Krueger minimum wage natural experiment
  3. Code: Complete Stata implementation (simulated data + estimation + visualization)

Layer 1: Principles

The Core Idea of DID

Difference-in-differences identifies causal effects by comparing two differences:

τ^DID=(YˉT,postYˉT,pre)Treatment group before-after difference(YˉC,postYˉC,pre)Control group before-after difference\hat{\tau}_{DID} = \underbrace{(\bar{Y}_{T,post} - \bar{Y}_{T,pre})}_{\text{Treatment group before-after difference}} - \underbrace{(\bar{Y}_{C,post} - \bar{Y}_{C,pre})}_{\text{Control group before-after difference}}

Regression Framework

The classic 2×2 DID model:

Yit=α+β1Treati+β2Postt+τ(Treati×Postt)+εitY_{it} = \alpha + \beta_1 \cdot Treat_i + \beta_2 \cdot Post_t + \tau \cdot (Treat_i \times Post_t) + \varepsilon_{it}

where τ\tau is the DID estimator, i.e., the treatment effect (ATT).

Two-Way Fixed Effects (TWFE)

Equivalently, using unit and time fixed effects:

Yit=αi+λt+τDit+εitY_{it} = \alpha_i + \lambda_t + \tau \cdot D_{it} + \varepsilon_{it}

where αi\alpha_i denotes unit fixed effects, λt\lambda_t denotes time fixed effects, and Dit=Treati×PosttD_{it} = Treat_i \times Post_t.

E[Y(0)postY(0)preTreat=1]=E[Y(0)postY(0)preTreat=0]E[Y(0)_{post} - Y(0)_{pre} \mid Treat = 1] = E[Y(0)_{post} - Y(0)_{pre} \mid Treat = 0]

That is, in the absence of treatment, the treatment group and control group share the same time trend.

⚠️ The parallel trends assumption cannot be directly tested, but pre-trend tests can provide indirect evidence (see the next article).

Clustered Standard Errors

When errors in panel data exhibit serial correlation at the unit level, clustered standard errors are required:

V^cluster=(XX)1(g=1GXgε^gε^gXg)(XX)1\hat{V}_{cluster} = (X'X)^{-1} \left(\sum_{g=1}^{G} X_g' \hat{\varepsilon}_g \hat{\varepsilon}_g' X_g \right) (X'X)^{-1}

General rule: cluster at the level at which the treatment variable varies.


Layer 2: Intuition

Card & Krueger (1994): Minimum Wages and Employment

New Jersey (treatment group) Pennsylvania (control group) Difference
Before policy 20.44 23.33 -2.89
After policy 21.03 21.17 -0.14
Before-after difference +0.59 -2.16 +2.76

DID estimate: τ^=2.76\hat{\tau} = 2.76 (raising the minimum wage actually increased employment)

This counterintuitive result challenged the predictions of traditional labor economics and became a milestone in causal inference methodology.


Layer 3: Stata Code

// ═══════════════════════════════════════════════
// Complete implementation of classic 2×2 DID
// ═══════════════════════════════════════════════
 
clear all
set seed 12345
 
// ═══ Simulate data ═══════════════════════════════
set obs 1000
gen id = _n
gen treat = (id > 500)
gen post = (runiform() > 0.5)
 
// DGP: true treatment effect τ = 3
gen y = 2 + 0.5*treat + 1.5*post + 3*treat*post + rnormal(0, 1)
 
// ═══ Method 1: Interaction term regression ═══════════════════
reg y i.treat##i.post, robust
// The coefficient on treat#post is the DID estimator
 
// ═══ Method 2: reghdfe (recommended) ════════════════
reghdfe y treat#post, absorb(id post) cluster(id)
 
// ═══ Visualization ═══════════════════════════════
preserve
collapse (mean) y, by(treat post)
twoway (connected y post if treat==0, lcolor(blue)) ///
       (connected y post if treat==1, lcolor(red)), ///
       legend(order(1 "Control group" 2 "Treatment group")) ///
       title("DID: Trends for Treatment and Control Groups")
restore

Common Mistakes

  1. Forgetting clustered standard errors: Panel DID requires clustering; otherwise, standard errors are severely underestimated
  2. Incorrect clustering level: Clustering should be at the level where the treatment variable varies (usually the unit or region level)
  3. Confusing FE with DID: Adding unit and time fixed effects ≠ DID; the treatment variable must also be correctly defined

References

  • Card, D., & Krueger, A. B. (1994). Minimum Wages and Employment. AER, 84(4), 772-793.
  • Angrist, J. D., & Pischke, J. S. (2009). Mostly Harmless Econometrics. Princeton.
  • Correia, S. (2016). reghdfe: Estimating Linear Models with Multi-way Fixed Effects. Stata Journal.

关联代码文件