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.
Structure of This Article
- Principles: The identification logic and core assumptions of DID
- Intuition: The Card & Krueger minimum wage natural experiment
- 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:
Regression Framework
The classic 2×2 DID model:
where is the DID estimator, i.e., the treatment effect (ATT).
Two-Way Fixed Effects (TWFE)
Equivalently, using unit and time fixed effects:
where denotes unit fixed effects, denotes time fixed effects, and .
Core Assumption: Parallel Trends
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:
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: (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")
restoreCommon Mistakes
- Forgetting clustered standard errors: Panel DID requires clustering; otherwise, standard errors are severely underestimated
- Incorrect clustering level: Clustering should be at the level where the treatment variable varies (usually the unit or region level)
- 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.