Difference-in-Differences in R: didimputation, did, and fixest
A complete guide to modern DiD estimation in R — using fixest, did, and didimputation — with parallel trends testing and event study plots.
R has become a first-class language for modern causal inference. The fixest, did, and didimputation packages implement the latest DiD estimators with clean syntax, fast computation, and publication-ready visualizations.
Classic TWFE with fixest
fixest is the R equivalent of Stata’s reghdfe: fast multi-way fixed effects with arbitrary clustering. The feols() function handles everything from simple OLS to multi-way FE. The i() function creates factor interactions with a reference level — perfect for event studies.
Staggered DiD with the did Package
Stop fighting with syntax.
Sytra is an AI research assistant built specifically for statistical computing. No more copy-pasting code into ChatGPT.
Get Early Accessdidimputation: Borusyak et al.
The imputation estimator from Borusyak, Jaravel, and Spiess (2024) takes a different approach: it imputes the untreated potential outcomes for treated units using the not-yet-treated observations, then computes treatment effects as the difference. It’s efficient, handles covariates naturally, and produces clean event study plots.
Which Package to Use
- No staggered adoption:
fixest::feols()with TWFE - Staggered, want flexibility:
did::att_gt() - Staggered, want efficiency:
didimputation::did_imputation() - Sun-Abraham estimator:
fixestwithsunab()
Comparison with Stata
R’s DiD ecosystem is arguably ahead of Stata’s. fixest is faster than reghdfe on large datasets, did has a cleaner interface than csdid, and the visualization tools (ggplot2-based) are more flexible. The tradeoff: R requires more ecosystem knowledge. You need to know which packages to install, how they interact, and which is appropriate for your design. Sytra handles this selection logic automatically.