Methodology
2026-04-0312 min read

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.

Sytra Team
Research Engineering Team, Sytra AI

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

library(fixest)
 
# Two-way FE with clustering
model <- feols(y ~ treatment | unit + year,
data = df, cluster = ~unit)
summary(model)
 
# Event study
es <- feols(y ~ i(rel_time, ref = -1) | unit + year,
data = df, cluster = ~unit)
iplot(es)

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

library(did)
 
# Callaway-Sant’Anna estimator
out <- att_gt(yname = "y",
tname = "year",
idname = "unit",
gname = "first_treat",
data = df,
control_group = "notyettreated")
 
# Aggregate to event study
es <- aggte(out, type = "dynamic")
ggdid(es)
 
# Overall ATT
overall <- aggte(out, type = "simple")
summary(overall)

Stop fighting with syntax.

Sytra is an AI research assistant built specifically for statistical computing. No more copy-pasting code into ChatGPT.

Get Early Access

didimputation: Borusyak et al.

library(didimputation)
 
model <- did_imputation(
data = df,
yname = "y",
gname = "first_treat",
tname = "year",
idname = "unit",
horizon = TRUE,
pretrends = TRUE
)

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: fixest with sunab()

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.

#Diff-in-Diff#R#Causal Inference#Economics

Enjoyed this article?