TL;DR
Difference-in-differences, often shortened to DiD, estimates the effect of a change that hit one group but not another. You measure both groups before and after the change, take the change in the affected group, and subtract the change in the unaffected group. What is left is the estimated effect, under one key assumption: without the change, both groups would have moved together.
Where you'll see it: in studies of a minimum wage that hits one state and not its neighbour, in evaluations of a new program rolled out in some regions first, and in any policy analysis where a comparison group is available but randomisation was not. It is a tool of Causal Inference: it asks what the policy caused, not only what changed.
Intuition
Start from a before-and-after question
Suppose a city raises a tax on sugary drinks, and you want to know whether sales fell because of the tax. A simple before-and-after comparison in that city is not enough. Sales might have been falling anyway, for reasons such as a new health campaign, a drop in tourism or a change in prices. The before-after change mixes the tax with everything else that changed in the same years.
The fix is to find a neighbouring city that did not raise the tax, and watch it over the same years. If that city's sales also fell, then the common shock is not the tax. The tax effect is the extra fall in the taxed city beyond the fall in the comparison city. That extra fall is the difference of the two differences, which gives the method its name.
Parallel lines are the whole idea
If both groups are tracked over time, the picture below is what the method needs. Before the change, the two lines move side by side, with a steady gap between them. At the change, one line bends. The bend is measured against where that line would have gone without the change, drawn as a dashed extension of the trend.
- treated group
- comparison group
- treated, without the change
The dashed line is the counterfactual: what the treated group would have done without the change. Nobody sees it directly, so DiD builds it from the comparison group's movement. That is the entire trick.
The math
Setting up the two groups and two periods
Consider two groups, a treated group that receives the change and a control group that does not, measured before the change (period ) and after it (period ). Let be the average outcome in group at time . Four averages describe the study:
The estimator is
The first bracket is the change in the treated group. The second is the change in the comparison group, which stands in for the common shocks. The difference is the estimate of the effect on the treated.
Potential outcomes and the assumption behind it
Write for the average (expected value) over the units in a group. Write for the outcome group would have at time without the change, and for the outcome with the change. The quantity usually wanted is the average effect on the treated,
which needs the counterfactual . That term cannot be observed, because the treated group was treated. DiD replaces it with a forecast, and the forecast comes from the control group:
The approximation holds under the parallel trends assumption: in the absence of treatment, the two groups would have changed by the same amount, . Under that assumption is an unbiased estimate of . Parallel trends is a claim about an unseen world, so you defend it with evidence, not with arithmetic.
The regression form
With many units and periods, DiD is run as a linear regression with three 0-1 indicator variables. A treatment-group indicator equals 1 for treated units and 0 otherwise. A post-period indicator equals 1 after the change and 0 before. The model is
Here is the average outcome of the control group before the change, is the baseline gap between groups, is the common change over time, and is the interaction term. The interaction is the DiD estimate: it is the only term that is switched on for treated units after the change, so it picks up the extra change in that cell. Its value equals exactly when the data are two groups and two periods.
Worked examples
Let the average outcome be 20 for treated units before the change and 26 after. For the comparison group it is 18 before and 21 after.
- Change in the treated group: .
- Change in the comparison group: .
- Difference of the changes: .
The estimated effect of the change is 3 units. Without the comparison group, you would have said the change added 6, and that would overstate the effect by the 3 units that everyone experienced.
Using the table from Example 1, set up the regression .
- The control group before the change is the reference cell, so .
- Treated before the change is 20, so .
- Control after the change is 21, so .
- Treated after the change is predicted as . It equals 26 when .
- Check: the interaction matches the difference of the changes from Example 1.
The regression gives the same estimate and also reports standard errors (a measure of how much the estimate would vary from sample to sample), which the plain difference does not. In a real study you would have many units and periods (a panel) and would use clustered standard errors, because outcomes for the same unit are correlated over time.
A state expands a job-training programme in 2022 and not its neighbour. The yearly average earnings, in thousands, are as follows (illustrative figures, not from real data).
| Year | Treated state | Comparison state | Gap (treated minus comparison) |
|---|---|---|---|
| 2019 | 30 | 25 | 5 |
| 2020 | 31 | 26 | 5 |
| 2021 | 32 | 27 | 5 |
| 2022 | 36 | 29 | 7 |
- Before 2022 the gap is 5 in every year. This is evidence that the two states were moving together, which is what parallel trends needs.
- The treated state's change from 2021 to 2022 is .
- The comparison state's change over the same years is .
- The DiD estimate is thousand, which is the same as the change in the gap, .
- The counterfactual for the treated state in 2022 is , shown as the dashed line in the plot above. The actual value of 36 is 2 above it.
A steady pre-period gap makes the parallel trends claim believable, but it does not prove it. Something else that hit only the treated state in 2022 would bias the estimate, and no pre-period check can rule that out.
Tradeoffs and failure modes
| Situation | Problem | Better choice |
|---|---|---|
| The groups were on different paths before the change | Parallel trends fails, so the estimate mixes the trend with the effect | Use a comparison group with a similar pre-trend, or model group-specific trends |
| Treatment timing differs across groups | A single before-after split is no longer clean | Use staggered designs and event-study plots, with care about how effects vary over time |
| Groups differ in level but move together | Level differences are harmless for DiD, since they cancel | Keep the DiD; the level gap is absorbed by the baseline |
| Something else hit only one group at the same time | DiD attributes that shock to the policy | Run a placebo test (apply the same method to an outcome or a period where no effect should exist, and check that it finds none) and look for other events |
| You have a randomised trial | DiD is more assumption-heavy than needed | Compare the treated and control means directly, as in a randomised trial |
A common mistake is to report the change in the treated group and call it the effect. That ignores the common shock. A second is to test parallel trends only once and treat a passing test as proof. Pre-trend tests have low power, and passing them does not guarantee that the post-period lines would have stayed parallel.
History
The logic of comparing an affected group with an unaffected one is much older than the name. John Snow used a version of it in London in the 1850s, when he compared cholera deaths among households served by two water companies, one of which had moved its intake to cleaner water upstream. The formal method spread through economics in the 1980s and 1990s. The best-known early example is a 1994 study by David Card and Alan Krueger, who compared fast-food employment in New Jersey, which raised its minimum wage, with eastern Pennsylvania, which did not. Their work showed how a comparison group could be used to study a policy change that could not be randomised. The approach caught on because it is cheap to run, uses data that already exist, and matches the way policies are usually rolled out.
Recap
Difference-in-differences subtracts the change in an unaffected group from the change in an affected group, and reads the result as an effect under parallel trends. Its regression form uses an interaction term, which is the estimate. The method is only as good as the assumption that the groups would have moved together without the change, so pre-period data and placebo checks matter as much as the arithmetic. It sits alongside the instrumental variables approach and the fixed effects model as a way to handle confounding and selection bias when randomisation is unavailable.
Check yourself
QuestionIn a two-group, two-period DiD, what are the four averages you need?Show answer
The treated group's average before and after the change, and the comparison group's average before and after the change. The estimate is the change in the treated group minus the change in the comparison group.
QuestionTreated group: 40 before, 52 after. Comparison group: 35 before, 38 after. What is the DiD estimate?Show answer
The treated change is and the comparison change is . The estimate is . Of the 12-unit rise in the treated group, 3 units would have happened anyway, so the policy is credited with 9.
QuestionWhy does the DiD estimate need the parallel trends assumption, and why can it not be checked directly after the change?Show answer
The estimate needs the comparison group's change to equal the change the treated group would have had without the policy. That counterfactual is never observed for the treated group after treatment. Pre-period data can show that the two groups moved together before the change, which supports the assumption, but it cannot prove the post-period trends would have stayed parallel.
QuestionA study finds that the treated and comparison groups were on rising and falling paths for two years before a policy. Should you still run a DiD?Show answer
Not without strong reasons to believe the divergence would not continue. Differing pre-trends violate parallel trends, so the DiD estimate would mix the policy with the groups' own paths. Better options are a comparison group with similar pre-trends, a model with group-specific trends, or a different design altogether.