Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 37 ยท Instrumental Variables and DiD

Difference-in-Differences

Learn difference-in-differences: the 2x2 design, the parallel trends assumption, the regression form with an interaction term and clustered standard errors, with runnable Python.

A city raises its minimum wage. A company launches a new feature in one country. A school district adopts a new curriculum. You have data from before and after, for the places that changed and for places that did not. How do you separate the effect of the change from everything else that was happening over time?

Difference-in-differences (DiD) does it with simple subtraction. The control group shows what would have happened to the treated group without the intervention, up to a level difference. You compare changes rather than levels.

The 2x2 design

Take four averages: treated and control groups, before and after.

Before After Change
Treated A B B - A
Control C D D - C

DiD = (B - A) - (D - C)

The treated group's change includes the treatment effect plus the general time trend. The control group's change contains only the trend. Subtracting removes the trend, and comparing changes instead of levels removes any fixed difference between the groups. That is why DiD copes with unmeasured confounders that are constant over time.

Python A 2x2 difference-in-differences by hand

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The simple "after" comparison mixes the effect with the level gap between groups (about 10 extra), and the before/after change of the treated group mixes it with the shared trend (about 5 extra). DiD recovers close to 8.

DiD is valid if, without treatment, the average outcomes of both groups would have moved in parallel. The groups may differ in level but not in trend. This is an assumption about a counterfactual, so it cannot be proved. You can only make it plausible, usually by showing that the trends were parallel before the intervention (see event studies) and by arguing that nothing else changed for one group only.

flowchart LR
    A["Treated: before"] --> B["Treated: after"]
    C["Control: before"] --> D["Control: after"]
    D --> E["Control change applied to treated start = counterfactual"]
    B --> F["Actual minus counterfactual = effect"]
    E --> F

The control group's change is used to build the counterfactual for the treated group.

The regression form

With one regression you get the estimate, its standard error and room for covariates:

Y = a + b*Treated + c*Post + d*(Treated x Post) + error

The coefficient d on the interaction is the DiD estimate. b is the baseline gap, c the common trend.

Python DiD as a regression with cluster-robust standard errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last row should be near 8. Because each unit appears in both periods, errors within a unit are correlated, so the standard errors are clustered by unit. Ignoring this makes standard errors too small, and with many periods the problem gets worse.

Extensions you will meet

  • Many periods and many groups with staggered adoption: the simple two-way fixed effects regression can give misleading results when effects change over time, because it compares newly treated units with already-treated ones. Modern estimators (Callaway and Sant'Anna, Sun and Abraham, de Chaisemartin and D'Haultfoeuille) fix this.
  • Covariates. Add them if trends may differ by observed characteristics, but do not add post-treatment variables.
  • Repeated cross-sections. DiD works even if you see different people in each period, as long as groups are stable.
  • Transformations. Parallel trends hold on a particular scale: logs or levels. Check that the scale makes sense.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does DiD remove from the comparison?

    Subtracting the before-after change of the control group removes common trends, and differencing within groups removes fixed level gaps.
  2. 2. What is the parallel trends assumption?

    Levels may differ; the counterfactual changes over time must match.
  3. 3. In the regression Y = a + bTreated + cPost + d*(Treated x Post), which coefficient is the DiD estimate?

    The interaction coefficient captures the extra change in the treated group after treatment.
  4. 4. Why cluster standard errors by unit in a panel?

    Ignoring within-unit correlation understates uncertainty.

Summary

  • DiD compares the change over time in a treated group with the change in a control group.
  • It removes fixed group differences and common trends, but rests on parallel trends.
  • The regression with a treated-by-post interaction gives the estimate and allows covariates.
  • Cluster standard errors by unit (or by the level of treatment assignment).
  • With staggered adoption, use modern estimators instead of plain two-way fixed effects.

Next: Event Studies and Parallel Trends