Courses › Statistics and Econometrics
Panel data and difference-in-differences
Fixed effects, before–after and treated–control comparisons, parallel trends, event studies and clustered standard errors.
When you can’t randomize, the next best thing is often to compare units with themselves. A café that raises its prices can be compared with its own sales before the change; a state that raises its minimum wage can be compared with its own past and with a similar state that didn’t. Panel data, the same units observed repeatedly, make this possible.
This module covers fixed effects regression, which removes every fixed difference between units; time fixed effects for shocks that hit everyone; the difference-in-differences design and its key assumption of parallel trends; event studies that check that assumption; and the clustered standard errors panel data require.
By the end of this module you will be able to
- Explain how entity fixed effects remove time-invariant omitted variables, and estimate them with demeaning or dummies.
- Add time fixed effects and interpret a two-way fixed effects regression.
- Compute a difference-in-differences estimate from a 2 × 2 table and as a regression interaction.
- State the parallel trends assumption and check it with pre-treatment trends and event studies.
- Explain why panel regressions need clustered standard errors and what goes wrong without them.
What this module covers
- Fixed effects: comparing each unit with itself
- Time fixed effects and two-way fixed effects
- Difference-in-differences
- Event studies and checking parallel trends
- Clustered standard errors
- Problem set with step-by-step solutions
- Module quiz
Lesson 14.1 Free lesson
Fixed effects: comparing each unit with itself
Panel data observe each unit i (a person, firm, café, city or province) in several periods t. Write the regression with a separate intercept for each unit:
The entity fixed effect αi captures everything about unit i that doesn’t change over time: a café’s location, a person’s innate ability, a province’s geography and history. Any such factor, measured or not, is absorbed. This is a powerful answer to omitted variable bias, as long as the omitted variables are fixed over time.
How it is estimated
Two equivalent methods:
- Demeaning (the within transformation): subtract each unit’s own average from Y and X, then run OLS on the demeaned data. The αi disappear because a constant minus its own average is zero.
- Dummies (least squares dummy variables): include a dummy for each unit (leaving one out, or dropping the intercept). The slope on X is identical to the demeaned version.
Either way, β₁ is identified only from changes within units over time. That is the Frisch–Waugh idea of Module 8, with the unit dummies as the controls.
The full module has an interactive graph here that you can move yourself.
Worked example: Two cafés, two months
Café A sells 900 lattes at $4.00 in March and 840 at $4.10 in April. Café B (busier, in a better location) sells 1,800 at $5.00 and 1,750 at $5.10.
Across cafés, the higher-priced café sells far more: a pooled regression would find a large positive price slope.
Within each café, a 10-cent rise goes with 60 fewer lattes at A and 50 fewer at B. A fixed-effects regression uses only these within-café changes and finds a negative slope of about −550 lattes per dollar.
What fixed effects can’t do
- They don’t remove omitted variables that change over time (a new competitor opening next door the month the café raised prices).
- They can’t estimate the effect of anything that never changes within a unit (a café’s location, a person’s birthplace): its variation is absorbed by αi.
- They rely on within-unit variation, which may be small and noisy, and they amplify measurement error (Module 13), because noise is a larger share of within-unit changes than of levels.
Common mistake
Thinking fixed effects solve all omitted variable problems. They remove only time-invariant confounders. Ask what else changed for a unit at the same time as X did.
Key ideas
- Entity fixed effects α_i absorb all time-invariant differences between units, measured or not.
- Estimate by demeaning each unit’s data or by including unit dummies; the slope is the same.
- β₁ is identified only from within-unit changes over time.
- Fixed effects don’t handle time-varying confounders or estimate effects of fixed characteristics.
Practice questions: Panel data and difference-in-differences
Try these, then open each answer to check your work.
- 1.Entity fixed effects remove omitted variable bias from factors that:
- Are constant over time within each unit
- Vary over time but not across units
- Are measured with error
- Are caused by X
Show answer
Answer: A. They absorb all time-invariant differences between units.
- 2.The within (demeaning) transformation and including a dummy for every unit:
- Give the same slope estimates
- Give different slope estimates
- Only work with two periods
- Remove time effects
Show answer
Answer: A. Least squares dummy variables and demeaning are numerically identical for the slope.
- 3.Time fixed effects absorb:
- Differences between units that never change
- Measurement error
- Shocks common to all units in a given period
- Spillovers
Show answer
Answer: C. One dummy per period captures anything that hits everyone at once.
Problems with step-by-step solutions
- 4.A panel of 200 workers observed for 5 years. Name two factors the worker fixed effects absorb and one they don’t.
Show step-by-step solution
- Absorbed: innate ability, family background (anything fixed over the five years), and also fixed education if it doesn’t change.
- Not absorbed: a health shock in year 3, a new manager, or a move to a different city (time-varying factors).
- 5.Café data (price, lattes sold): Café A: ($4.00, 900), ($4.20, 860); Café B: ($5.00, 1,800), ($5.20, 1,740). Compute the within-café slope for each café and their average.
Show step-by-step solution
- Café A: (860 − 900)/(4.20 − 4.00) = −40/0.20 = −200 lattes per dollar.
- Café B: (1,740 − 1,800)/0.20 = −300.
- Average within slope: −250 lattes per dollar (with equal-sized price changes, fixed effects gives this average).
Get the full module
The complete module adds every lesson, interactive graphs, a full problem set with step-by-step solutions, and a quiz. The academy opens in January 2027: join the waitlist for the founding price (lifetime access for $179).
Join the waitlist Try a full module free
More practice: free practice questions for this course.