Difference in Differences: Method, Assumptions and R Workflow

Difference in differences (DiD) estimates the effect of a policy or exposure by comparing how outcomes changed over time in a treated group against how they changed in a comparison group. The treated group’s before-and-after change, minus the comparison group’s change, is the estimate. It is credible only if the two groups would have moved in parallel without the treatment. This guide covers the arithmetic, the assumptions, event studies, the two-way fixed effects trap and a reproducible workflow, and it shows where agentic analytics for researchers fits around your existing statistical software.
TL;DR
- DiD is a quasi-experimental design for when randomization is not possible. The estimate is (B−A)−(D−C): the treated group’s change minus the comparison group’s change.
- The identifying assumption is parallel trends in untreated outcomes, plus no anticipation. It cannot be proven, only made more or less plausible.
- Event-study plots are diagnostics, not proof. Pair them with power or sensitivity checks.
- With staggered treatment timing and heterogeneous effects, conventional two-way fixed effects (TWFE) can mislead. Use an estimator built for that setting.
- Reviewers and supervisors want the code, the plan and the output together. PlotStudio AI keeps all three reviewable.
Table of Contents
- What difference in differences does
- When DiD is the right design
- The parallel trends assumption
- Event studies: leads, lags and the reference period
- Two-way fixed effects and the staggered adoption problem
- Running DiD in R
- Reporting checklist
- Where PlotStudio AI fits in a DiD project
- Promo: try it on your own panel
- Perspective
- FAQ
- Sources
What difference in differences does
The World Bank’s DIME Wiki describes DiD as a quasi-experimental method that compares before-and-after outcome changes in treated and comparison groups to estimate program impact when randomization is unavailable. Columbia’s Mailman School of Public Health adds the data requirement: you need outcomes before and after the intervention for both a treatment and a control population, in settings where individual randomization is not possible.
Why two differences? A simple before-and-after comparison of the treated group mixes the treatment effect with anything else that changed over time. A simple treated-versus-control comparison after the intervention mixes the effect with pre-existing gaps. Differencing twice removes both: stable group differences drop out in the first difference, and shared time shocks drop out in the second.

A worked example with made-up numbers
Suppose a hospital system introduces a sepsis screening protocol in some units (treated) but not others (comparison). The outcome is the mean length of stay in days. These figures are hypothetical, for illustration only.
| Before | After | Change | |
|---|---|---|---|
| Treated units | 9.0 (A) | 7.5 (B) | −1.5 |
| Comparison units | 8.0 © | 7.5 (D) | −0.5 |
The DiD estimate is (7.5 − 9.0) − (7.5 − 8.0) = −1.5 − (−0.5) = −1.0 days. Length of stay fell 1.5 days in treated units, but 0.5 of that decline also happened where there was no protocol, so the attributable effect is 1.0 day.
The same number comes from a regression: outcome on a treated-group indicator, a post-period indicator and their interaction. The interaction coefficient is the DiD estimate. Interpret it as the average effect on the treated units, the quantity our guide to the treatment effect on the treated unpacks in more detail. The 2025 practitioner’s guide by Baker and colleagues states that under parallel trends the canonical two-group, two-period design identifies the average treatment effect on treated units.
When DiD is the right design
Use DiD when all of these hold:
- Treatment was assigned at a group level (a state, a hospital, a ward, a school) or arrived at a known time, and you could not randomize it.
- You have outcomes measured before and after, for treated and untreated units.
- You can defend a comparison group that would have followed a similar path absent treatment.
- Treatment of one unit did not spill over onto the comparison units. Columbia’s guidance lists no spillovers and stable composition of repeated cross-sections among the DiD-specific requirements.
If assignment turned on a score cutoff rather than a group and a date, a different design fits better; see our regression discontinuity articles. For a broader map of which test answers which question, start with our statistical test selection guide.
The parallel trends assumption
Parallel trends means that, without treatment, the treated group’s average outcome would have changed by the same amount as the comparison group’s. You never observe that counterfactual for the treated group after treatment, so the assumption cannot be proven. The DIME Wiki says exactly this and lists ways to judge plausibility: several pre-treatment periods, placebo-treated groups, placebo outcomes and alternative comparison groups.
Three points trip up researchers most often.
- No anticipation. Canonical DiD also assumes units did not change behavior before treatment because they knew it was coming. Roth and coauthors’ 2023 review in the Journal of Econometrics treats this as part of the core assumptions.
- Scale dependence. The practitioner’s guide warns that parallel trends depends on how the outcome is measured. Trends can be parallel in levels but not in logs, so the functional form is part of your identifying claim and belongs in your methods section.
- Pre-trend tests are weak evidence. The same 2023 review cautions that such tests can be underpowered and do not establish the assumption. A non-significant pre-trend does not mean the trends were parallel. Assess the test’s power or use non-inferiority diagnostics.
When a reviewer asks you to justify the design, this is where the question lands. Your answer should combine subject-matter reasoning for why the groups are comparable, pre-period evidence and a sensitivity analysis.
Event studies: leads, lags and the reference period
An event study replaces the single post-treatment indicator with a set of coefficients, one for each period relative to treatment. According to the Roth review, you index observations by time relative to treatment, omit a stated reference period (often −1, the period just before treatment) and estimate leads and lags. Lead coefficients describe pre-treatment differences. Lag coefficients describe how the effect evolves relative to the reference period.
How to read the plot:
- Leads near zero with tight intervals are reassuring, but only as far as the intervals are tight enough to rule out meaningful violations.
- A visible break at the treatment date supports a treatment story. A drift that started earlier suggests a pre-existing trend.
- State the reference period in the figure caption. Coefficients are relative to it, so changing it changes every bar.
The R documentation for the did2s package demonstrates an event study with unit and year fixed effects, relative-time indicators and −1 as the reference category. The package’s companion paper shows event-time indicators specified through the fixest package’s i() function.
Two-way fixed effects and the staggered adoption problem
The familiar regression puts unit fixed effects, time fixed effects and a treatment indicator in one model. That is TWFE. If you are unsure what the fixed-effects choice implies, our explainer on fixed effects versus random effects covers the logic.
In the canonical two-group, two-period case, TWFE reproduces the DiD estimate. Trouble begins when units adopt treatment at different times and effects differ across groups or over time. The R Journal article on did2s says conventional TWFE can be problematic in exactly that case. The reason, in that article’s account, is that estimating fixed effects with treated observations in the sample contaminates the model of untreated outcomes. Its two-stage approach estimates the first stage on untreated observations only.
The literature offers several alternatives. The Roth review names Callaway and Sant’Anna’s group-time aggregation, Sun and Abraham’s event-study estimator, Gardner’s two-stage estimator (implemented in did2s) and imputation estimators. Choose among them by the comparison group you can defend (never-treated or not-yet-treated units) and the assumptions you are willing to make.
The Annual Review of Public Health offers a step-by-step guide and checklist for staggered-adoption designs, including treatment-effect heterogeneity. The practitioner’s guide gives a useful mental model: complex DiD analyses are aggregations of 2×2 comparisons, so identify the causal quantity each building block represents.
Running DiD in R
A reproducible DiD workflow in R looks like this:
- Define the estimand. Average effect on the treated at which horizons? For which units?
- Check the design. Tabulate treatment timing by unit. Is adoption simultaneous or staggered? Are there never-treated units?
- Fit the basic 2×2 or TWFE model as a baseline and keep it, even if you will not rely on it.
- Fit a heterogeneity-robust estimator. With did2s, you specify the outcome, the treatment indicator, first- and second-stage formulas and the clustering variable. The documentation says the treatment argument marks active treatment, the first stage uses untreated observations, the second stage uses the full sample and standard errors can be clustered by unit or a higher-level cluster. The package also provides a common-syntax event-study function for comparing estimators.
- Plot the event study with the reference period stated.
- Run sensitivity checks (below).
- Save code, session information and output together so a collaborator can rerun it.
Uncertainty and sensitivity
The Roth review recommends clustered standard errors at the treatment-assignment level, event-study plots with power or non-inferiority diagnostics and formal sensitivity analysis for parallel-trends violations. Columbia’s guidance recommends robust standard errors to account for within-individual autocorrelation and examining how the treated and control populations’ composition changes. For background on variance estimators, see our robust standard errors articles.
For sensitivity, the peer-reviewed HonestDiD paper by Rambachan and Roth proposes confidence sets that allow post-treatment trend violations bounded by what the pre-treatment trends show. Instead of insisting on exact parallel trends, you report how conclusions change as the restrictions loosen. That is a far stronger answer to a skeptical reviewer than a flat pre-trend p-value.
Reporting checklist
What a methods section needs:
- The estimand and the comparison group, with the reasoning for why it is comparable.
- Outcome scale (levels or logs) and why.
- The estimator, and why it suits your timing structure.
- The reference period for any event-study figure.
- The clustering level for standard errors.
- Pre-trend diagnostics with power or non-inferiority, plus a sensitivity analysis.
- Software, package versions and the full code.
If your data are patient-level or otherwise special category data, also record where the analysis ran. Many ethics approvals and data-sharing agreements restrict uploading records to cloud tools.
Where PlotStudio AI fits in a DiD project
R, Python and Jupyter give you the statistical computing. PlotStudio AI is an additional layer of agentic analytics for researchers: you connect tabular data and define the analytical objective, and specialized agents plan the investigation, write and run real code, inspect intermediate results and synthesize findings.
Several parts of that map onto the DiD workflow above:
- Plan Mode lets you review and modify the proposed approach, including methods, assumptions and steps, before anything runs. For DiD, that is the moment to confirm the estimand, comparison group, outcome scale and clustering level.
- Skills encode how a lab or discipline analyzes data, including procedures, statistical conventions and reporting expectations. A group can write its DiD conventions down once and have them applied consistently.
- Local execution. Analyses can run on your own machine, which suits datasets that should not be uploaded to conventional cloud platforms.
- Reviewable outputs. Completed work is kept as searchable Analysis Pages and exported as notebooks and PDF reports, so a supervisor who could not reproduce your numbers can inspect the methodology, code and results.
PlotStudio AI supports Python and R workflows and methods such as regression, mixed-effects models and multiple-comparison procedures. It does not remove the need for the judgment in this article. You still decide whether the comparison group is credible. What it adds is an agent that pursues the multi-step analysis and documents each step.
Promo: try it on your own panel
If you have a panel or repeated-measures dataset and a reviewer waiting, you can start with the Free Trial on the pricing page. The Bring Your Own Key plan costs $39.99 per month, or $399.90 per year on the academic page. Managed Credits costs $69.99 per month.

Groups looking at organization-wide use can see the one-off $500 pilot fee on the enterprise page.
Perspective
We built PlotStudio AI around a simple observation: a DiD result is only as good as the argument behind it. The coefficient is one line of output. The comparison group, the outcome scale, the treatment timing, the diagnostics and the sensitivity checks are what a reviewer actually interrogates.
That is why we think of agentic analytics as an emerging layer in the modern research toolkit, next to R, Python, SPSS, SAS and Jupyter, not a replacement for them. Those tools provide the statistical computing. The agentic layer plans a multi-step analysis, executes it, inspects intermediate results and documents the whole chain, so the methodology, code, outputs and charts stay available for review.
For DiD specifically, we would want you to review the plan before it runs, keep your lab’s conventions in a Skill and export a notebook your supervisor can rerun. For researchers who need reproducible, multi-step analysis rather than one-shot chat answers, that is the workflow PlotStudio AI is designed for.
Aymen
FAQ
How do I interpret a difference-in-differences coefficient?
In the 2×2 regression, the interaction between treated group and post period is the estimated change in the treated group’s outcome beyond what the comparison group’s trend predicts. In the example above, −1.0 means one fewer day of stay attributable to the protocol, under parallel trends. State the scale (levels or logs) when you report it.
How many pre-treatment periods do I need?
Two periods give you the basic design but no way to look at pre-trends. More pre-treatment periods let you draw an event-study plot and judge whether the groups were moving together. The DIME Wiki lists repeated pre-treatment trends as one way to assess plausibility.
Is a flat pre-trend plot enough to defend parallel trends?
No. Pre-trend tests can be underpowered, and passing one does not establish the assumption. Combine subject-matter justification, power or non-inferiority diagnostics and a sensitivity analysis such as the HonestDiD approach.
When should I avoid plain TWFE?
Avoid it as your main estimate when units are treated at different times and the effect may differ across groups or over time. Use a heterogeneity-robust estimator such as the two-stage, group-time or imputation approaches, and report the TWFE result only as a baseline.
Can I run DiD without writing the code myself?
You still need to choose the design and defend it, but the code can be drafted and run for you. In PlotStudio AI, agents write and execute the code and you review the plan first in Plan Mode, so the method and its assumptions are visible before the numbers appear.
Sources
- Difference-in-Differences | Dime Wiki, World Bank
- Difference-in-Differences Designs: A Practitioner’s Guide
- Difference-in-Difference Estimation, Columbia Public Health
- Difference-in-Differences: A Review, Journal of Econometrics (2023)
- A More Credible Approach to Parallel Trends (HonestDiD)
- did2s: Calculate two-stage difference-in-differences
- did2s: Two-Stage Difference-in-Differences (arXiv)
- Difference-in-Differences, The R Journal (2022)
- Designing Difference-in-Difference Studies with Staggered Treatment Adoption, Annual Review of Public Health