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.
Related reading
- Event Study: Dynamic Effect Estimation and Coefficient Plots
A comprehensive guide to the event study method, covering dynamic effect estimation, coefficient plot construction, normalization, and Leads/Lags settings.
DID2×2 之后的动态效应:事件研究(31 次访问)。 - Staggered DID: TWFE Bias and New Estimators (CS/SA/BJS)
An in-depth explanation of TWFE bias in staggered DID designs, introducing the principles and practical implementation of three new estimators: Callaway-Sant'Anna, Sun-Abraham, and Borusyak-Jaravel-Spiess.
DID现实政策很少同时发生,下一步是 staggered(21 次访问)。 - How to Understand Interactions of Dummy Variables? How Does This Understanding Map to Difference-in-Differences (DID)?
The general meaning of an interaction term is that the effect of X on Y varies with the value of a third variable M. When M is continuous (e.g., income), the interaction term tells you \"how the marginal effect of X changes at different levels of M.\" But when M is a dummy variable (e.g., gender), the interpretation of the interaction term has a more intuitive and concrete geometric understanding...
Econometrics2×2 的回归就是虚拟变量交互(11 次访问)。 - Panel Data and Fixed Effects: Choosing Between FE and RE with TWFE in Practice
A systematic introduction to panel data analysis methods: the logic of choosing between fixed effects and random effects, the Hausman test, two-way fixed effects (TWFE), and the choice of clustered standard errors.
Panel - Parallel Trends Assumption: Intuition, Testing, and Visualization
An in-depth understanding of the core assumption of DID—parallel trends. Explains the meaning of the assumption, visualization testing methods, and pre-trend tests, with accompanying Stata code.
DID