Search

Search papers and lessons

Difference-in-Differences

Level 3 · 10 min read

Difference-in-differences estimates a policy's effect by subtracting the change in an unaffected group from the change in the group that was affected.

On this page

  • Colour = topic mix
  • Discrete math
  • Probability
  • Calculus
  • Linear algebra
  • Classical models
  • Losses & regularization
  • Sign in to see your progress

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.

2018.520192019.520202020.520212021.520222022.52025303540yearaverage outcome (illustrative)
  • treated group
  • comparison group
  • treated, without the change
Illustrative numbers. The treated group ends 2 units above where its own trend would have put it. The dashed line is that counterfactual, built from the comparison group's 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 TT that receives the change and a control group CC that does not, measured before the change (period 00) and after it (period 11). Let Yˉg,t\bar{Y}_{g,t} be the average outcome in group gg at time tt. Four averages describe the study:

YˉT,0,YˉT,1,YˉC,0,YˉC,1.\bar{Y}_{T,0}, \quad \bar{Y}_{T,1}, \quad \bar{Y}_{C,0}, \quad \bar{Y}_{C,1}.

The estimator is

δ^DiD=(YˉT,1−YˉT,0)−(YˉC,1−YˉC,0).\hat{\delta}_{\text{DiD}} = \left(\bar{Y}_{T,1} - \bar{Y}_{T,0}\right) - \left(\bar{Y}_{C,1} - \bar{Y}_{C,0}\right).

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 E[⋅]\mathbb{E}[\cdot] for the average (expected value) over the units in a group. Write Yg,t(0)Y_{g,t}(0) for the outcome group gg would have at time tt without the change, and Yg,t(1)Y_{g,t}(1) for the outcome with the change. The quantity usually wanted is the average effect on the treated,

δ=E[YT,1(1)−YT,1(0)],\delta = \mathbb{E}\left[Y_{T,1}(1) - Y_{T,1}(0)\right],

which needs the counterfactual E[YT,1(0)]\mathbb{E}[Y_{T,1}(0)]. 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:

E[YT,1(0)]≈YˉT,0+(YˉC,1−YˉC,0).\mathbb{E}\left[Y_{T,1}(0)\right] \approx \bar{Y}_{T,0} + \left(\bar{Y}_{C,1} - \bar{Y}_{C,0}\right).

The approximation holds under the parallel trends assumption: in the absence of treatment, the two groups would have changed by the same amount, E[YT,1(0)−YT,0]=E[YC,1(0)−YC,0]\mathbb{E}[Y_{T,1}(0) - Y_{T,0}] = \mathbb{E}[Y_{C,1}(0) - Y_{C,0}]. Under that assumption δ^DiD\hat{\delta}_{\text{DiD}} is an unbiased estimate of δ\delta. 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 DiD_i equals 1 for treated units and 0 otherwise. A post-period indicator PtP_t equals 1 after the change and 0 before. The model is

Yit=β0+β1Di+β2Pt+β3(Di×Pt)+εit.Y_{it} = \beta_0 + \beta_1 D_i + \beta_2 P_t + \beta_3 (D_i \times P_t) + \varepsilon_{it}.

Here β0\beta_0 is the average outcome of the control group before the change, β1\beta_1 is the baseline gap between groups, β2\beta_2 is the common change over time, and β3\beta_3 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 δ^DiD\hat{\delta}_{\text{DiD}} exactly when the data are two groups and two periods.

Worked examples

Example
1. A two-by-two table with small numbers (illustrative)

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.

  1. Change in the treated group: 26−20=626 - 20 = 6.
  2. Change in the comparison group: 21−18=321 - 18 = 3.
  3. Difference of the changes: 6−3=36 - 3 = 3.

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.

Example
2. The same numbers through regression

Using the table from Example 1, set up the regression Y=β0+β1D+β2P+β3(D×P)Y = \beta_0 + \beta_1 D + \beta_2 P + \beta_3 (D \times P).

  1. The control group before the change is the reference cell, so β0=18\beta_0 = 18.
  2. Treated before the change is 20, so β1=20−18=2\beta_1 = 20 - 18 = 2.
  3. Control after the change is 21, so β2=21−18=3\beta_2 = 21 - 18 = 3.
  4. Treated after the change is predicted as β0+β1+β2+β3=18+2+3+β3\beta_0 + \beta_1 + \beta_2 + \beta_3 = 18 + 2 + 3 + \beta_3. It equals 26 when β3=3\beta_3 = 3.
  5. Check: the interaction β3=3\beta_3 = 3 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.

Example
3. A realistic panel with pre-trend checks (illustrative)

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).

YearTreated stateComparison stateGap (treated minus comparison)
201930255
202031265
202132275
202236297
  1. 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.
  2. The treated state's change from 2021 to 2022 is 36−32=436 - 32 = 4.
  3. The comparison state's change over the same years is 29−27=229 - 27 = 2.
  4. The DiD estimate is 4−2=24 - 2 = 2 thousand, which is the same as the change in the gap, 7−5=27 - 5 = 2.
  5. The counterfactual for the treated state in 2022 is 32+2=3432 + 2 = 34, 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

SituationProblemBetter choice
The groups were on different paths before the changeParallel trends fails, so the estimate mixes the trend with the effectUse a comparison group with a similar pre-trend, or model group-specific trends
Treatment timing differs across groupsA single before-after split is no longer cleanUse staggered designs and event-study plots, with care about how effects vary over time
Groups differ in level but move togetherLevel differences are harmless for DiD, since they cancelKeep the DiD; the level gap is absorbed by the baseline
Something else hit only one group at the same timeDiD attributes that shock to the policyRun 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 trialDiD is more assumption-heavy than neededCompare 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 52−40=1252 - 40 = 12 and the comparison change is 38−35=338 - 35 = 3. The estimate is 12−3=912 - 3 = 9. 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.

Sources

Difference-in-Differences — explained simply | ungoon.me