1 What problem does this tool solve?
When treatment is adopted at different times by different units, the two-way fixed effects (TWFE) regression that generations of applied economists reached for can return a badly biased estimate even a coefficient of the wrong sign because already-treated units serve as controls for newly-treated ones, contaminating the comparison with negative weights [Goodman-Bacon, 2021, de Chaisemartin and D'Haultfœuille, 2020]. A wave of "robust" estimators fixes this. The did2s package [Butts and Gardner, 2022] implements the two-stage imputation estimator of Gardner [2022], closely related to the imputation approach of Borusyak et al. [2024]. Its appeal is conceptual clarity and speed: it estimates the untreated potential outcome from never- and not-yet-treated observations, then reads the treatment effect off the residuals.
2 The estimator in one paragraph
Assume untreated potential outcomes follow a two-way model $Y_{it}(0)=\mu_{i}+\lambda_{t}+\epsilon_{it}$: unit fixed effects $\mu_{i}$ plus time fixed effects $\lambda_{t}$. Stage one estimates $\mu_{i}$ and $\lambda_{t}$ using only observations that are not yet treated (where $Y_{it}=Y_{it}(0)$ is observed), never touching the treated cells. Stage two forms the imputed counterfactual $\hat{Y}_{it}(0)=\hat{\mu}_{i}+\hat{\lambda}_{t}$ and regresses the residual $Y_{it}-\hat{Y}_{it}(0)$ on treatment indicators to recover the effect
$$\hat{\tau} = \text{(residualised outcome projected on treatment dummies)}$$
$$Y_{it} - \hat{Y}_{it}(0) = \tau D_{it} + \text{noise}. \qquad (1)$$
Because the first stage never uses treated observations to estimate the fixed effects, the already-treated-as-control contamination cannot arise. Standard errors are corrected analytically for the two-stage structure (the second stage uses generated regressors), avoiding the naive understatement a plug-in would produce.
3 Installation and setup
4 A minimal working example
We simulate a staggered-adoption panel with heterogeneous, dynamic effects, then estimate the overall ATT and an event study. did2s ships a simulated dataset, df_het, ideal for a first pass.
The first_stage formula specifies the model for untreated potential outcomes here just unit and year fixed effects. The package automatically restricts first-stage estimation to untreated/not-yet-treated rows using the treatment indicator. The second_stage formula specifies which treatment terms to estimate.
For an event study, replace the second stage with relative-time indicators:
The reference categories relative period -1 and the never-treated (Inf) define the baseline against which dynamic effects are measured. Pre-treatment coefficients near zero support the parallel-trends assumption; post-treatment coefficients trace the effect's dynamics.
5 Key options and pitfalls
- Covariates in the first stage. Time-varying controls enter the first stage formula, e.g.
~ x1 + x2 | unit + year. Be careful: a control that is itself affected by treatment is a "bad control" and biases the estimate include only covariates whose paths are unaffected by the policy [Caetano et al., 2022]. - Never-treated vs. not-yet-treated. Identification needs a clean control group. If every unit is eventually treated, the first stage leans entirely on not-yet-treated timing variation; a pure never-treated group strengthens the design.
- Standard errors. did2s implements the analytic two-stage correction; do not substitute a naive clustered SE from a hand-rolled two-step, which ignores the generated-regressor problem and understates uncertainty.
- Anticipation. If units respond before the official adoption date, those periods are effectively treated and must be coded as such, or the first stage is contaminated.
6 Comparison to alternatives
did2s sits in a family of robust estimators, and in practice they agree far more with each other than any of them agrees with naive TWFE [Roth et al., 2023]. Table 1 sketches the landscape.
Choose did2s when you want a fast, transparent imputation estimator whose assumptions (a two-way model for untreated outcomes and clean controls) are easy to state and whose event-study output plugs directly into fixest's plotting tools.[cite: 23] When conditional parallel trends require flexible covariate adjustment, pair the intuition here with the doubly robust did package; when efficiency is paramount, compare against staggered.[cite: 23] The healthy habit, as always, is to run two or three and report that they agree.[cite: 23]
References
Roth, J., Sant'Anna, P. H. C., Bilinski, A., and Poe, J. (2023). What's trending in difference-in-differences? A synthesis of the recent econometrics literature. Journal of Econometrics, 235(2), 2218-2244.[cite: 23]