Fixed Effects in Panel Data
- #econometrics
- #panel-data
- #fixed-effects
- #within-estimator
- #demeaning
- #clustered-standard-errors
- #two-way-fixed-effects
- #instrumental-variables
- #time-fixed-effects
Fixed Effects in Panel Data
Part of: Econometrics Lecture 08 — Applied Econometrics, Dr. Aluma Dembo Key concepts: Panel Data, Fixed Effects, Within Estimator, Demeaning, Between Variation, Within Variation, Time Fixed Effects, Two-Way Fixed Effects, Clustered Standard Errors, Individual Fixed Effect, First Stage, Two Stage Least Squares
What is Panel Data?
Panel data tracks the same individuals (people, firms, cities, judges…) across multiple time periods.
- Each individual appears in the dataset more than once — once per time period
- This gives you both cross-sectional variation (differences between individuals) and time variation (changes within an individual over time)
Why panel data is powerfulWith cross-sectional data, you can only compare different individuals to each other. With panel data, you can compare the same individual to themselves at different points in time — which controls for all the stable, individual-level characteristics you can't observe or measure.
Motivating Example: Temperature and Immigration Judges
Paper: Heyes & Saberian (2019)
Question: Does outdoor temperature affect how immigration judges make decisions?
Why ask this? If judges are supposed to be impartial, then outdoor temperature — which is outside their control — should have zero effect on their decisions. A significant temperature effect would suggest that physiological or psychological factors are influencing what should be a purely legal judgement.
Data:
- 206,924 asylum decisions
- 266 immigration judges
- 43 cities
- January 2000 – September 2004
Outcome (): whether the judge granted asylum (1 = yes, 0 = no), as a fraction across decisions in a given period
Treatment (): outdoor temperature (°F) on the day of the hearing
Building Up the Model: Step by Step
Step 0: The Naive Model
Just regress asylum grant rate on temperature. Problem: this ignores that asylum grant rates differ enormously across nationalities (some countries have much higher grant rates than others), and across cities (some courts are more liberal).
Step 1: Control for Nationality
After adding nationality fixed effects, the coefficient on temperature is marginally insignificant ().
Why add nationality controls?Nationality strongly predicts grant rate — Syrian or Afghan asylum seekers have much higher grant rates than others. If certain nationalities cluster in certain cities, and those cities happen to be hotter, nationality would confound the temperature-grant rate relationship. We control for it to remove this bias.
Time Fixed Effects: Controlling for Calendar Shocks
Any event that affects all judges in all cities on the same day is a "time shock". Examples:
- A big Supreme Court ruling that changes interpretation of asylum law
- A major news event that shifts public opinion
Time fixed effects absorb these: one dummy variable per time period that captures everything that changes uniformly across all observations in that period.
Repeated Cross Sections with Time Controls
We can add time controls in two ways:
Option A — Linear time trend: include a variable (day, month, or year number)
Option B — Dummy variable time fixed effects: one dummy per time period (more flexible — allows any non-linear time pattern)
Linear trend vs. dummy time fixed effectsA linear time trend forces the relationship with time to be a straight line. Dummy time fixed effects make no such assumption — they let the time effect be completely flexible, zig-zagging in any pattern. Dummy FEs use more degrees of freedom but are more robust to non-linear time patterns.
What does a time fixed effect "absorb"?Imagine a nationwide policy change that makes judges tougher in a particular month. A month fixed effect absorbs this — it picks up the average shift in grant rates in that month across all cities. After including it, the remaining variation in grant rates is within a given month, across cities.
Between Variation vs. Within Variation
This distinction is central to understanding fixed effects.
Between Variation: Comparing Averages Across Individuals
Between variation: the variation in mean grant rates across judges / cities.
Why between variation is dangerous for causal inferenceIf some cities are both hotter and more lenient (e.g. because of the local political culture), then comparing city averages will show a correlation between temperature and leniency — but it's driven by the city culture, not temperature. This is between-individual confounding (also called self-selection or omitted variable bias).
Example: Alfie lives in Tel Aviv (hot and strict courts) while Barak lives in Seattle (cold and lenient courts). Comparing them tells you about Tel Aviv vs. Seattle culture, not about temperature effects.
type: fixed-effects
mode: within-between
The sign can flip entirelyEach coloured cluster is one individual (judge/city), and within every cluster the slope is negative — hotter days mean a lower grant rate. But the cluster means (black X's) climb from bottom-left to top-right, so pooled OLS reports a positive between-slope. That dashed line is pure confounding: hotter places happen to be stricter for unrelated reasons. Fixed effects throw away the between variation and keep only the within slopes — recovering the true negative effect.
Within Variation: Comparing the Same Individual Across Time
Within variation: how much a variable deviates from that individual's own mean.
asks: "On this day, did judge grant asylum more or less than she usually does?" asks: "Was today hotter or cooler than this city's average temperature?"
The power of within variationBy looking only at deviations from individual means, you control for everything that is stable about that individual — their personality, their background, the court culture, the city's climate on average. Within variation compares Alfie to Alfie on a hot day vs. a cool day, not Alfie to Barak.
The Individual Fixed Effects Model
- : outcome for individual at time
- : time-varying treatment/covariate
- : individual fixed effect — a catch-all for everything about individual that doesn't change over time
- : remaining idiosyncratic error
What does absorb?captures all time-invariant characteristics of individual — observed or unobserved. For a judge, this might include:
- Personality traits (inherent strictness/leniency)
- Legal training and philosophy
- Regional court culture
- Average climate of their city
You never need to measure these things — the fixed effect absorbs them all. This is why fixed effects are so powerful for causal inference.
- measures the average within-individual effect of on : "when increases for individual , how does change for that same individual ?"
How to Estimate Fixed Effects: Within-Estimation (Demeaning)
You could, in principle, add a dummy variable for every individual in the regression. But with many individuals (e.g. 266 judges), this is unwieldy. Within-estimation (demeaning) achieves the same result more efficiently.
Step 1: Demean all variables
For each individual , compute their time-average, then subtract it:
This removes the individual fixed effect entirely (since for a time-constant term):
Step 2: Run OLS on the demeaned data
Demeaning in practice — judge exampleJudge Smith makes decisions on 200 cases. Her average grant rate is 35%. On hot days, her grant rate is 30%; on cool days, 40%.
After demeaning:
- Hot days:
- Cool days:
We regress these deviations on deviations of temperature from Smith's average temperature. If hotter-than-usual days consistently correspond to lower-than-usual grant rates, .
type: fixed-effects
mode: demeaning
What demeaning does to the dataOn the left, each individual sits at its own height — the X's mark their personal means . Subtracting those means slides every cluster onto a common origin (right), erasing the individual fixed effect . All that remains is the within variation, and a single regression through the stacked clusters recovers the within slope — identical to adding a dummy for every individual, but far cheaper.
Adding individual dummies vs. demeaning: same resultRunning OLS with a dummy for every individual produces exactly the same as demeaning. The demeaning approach just avoids including hundreds of dummy variables explicitly. This equivalence is the Frisch-Waugh-Lovell theorem in action again.
City Fixed Effects
Apply individual fixed effects at the city level:
where is a fixed effect for city — absorbing all time-invariant city characteristics (court culture, demographics, local legal norms, average climate).
feols(res ~ Temperature + region | city, data = asy.data)
Result: After adding city fixed effects, the temperature coefficient becomes insignificant ().
What this tells usThe raw (unadjusted) relationship between temperature and grant rate is explained by city-level confounders — not by temperature itself. Hotter cities may simply have stricter courts for unrelated reasons. Once we control for city fixed effects, the within-city variation in temperature is unrelated to grant rates.
Two-Way Fixed Effects: City + Month
Control for both city-level and month-level fixed effects simultaneously:
feols(res ~ Temperature + region | city + month, data = asy.data)
- : absorbs city characteristics
- : absorbs any systematic variation in grant rates across months (e.g. summer vs. winter court backlogs)
Why add month fixed effects?Temperature is seasonal — it's higher in summer months than winter months. If courts are also systematically busier or stricter in summer (e.g. because immigration flows spike), then without month FEs, you'd be comparing "hot summer days in city X" to "cold winter days in city X" — confounded by the summer/winter court dynamic. Month FEs remove this confound.
Interacted Fixed Effects: City × Month
Go further: a fixed effect for each city-month combination:
feols(res ~ Temperature + region | city^month, data = asy.data)
- 43 cities × ~12 months = ~463 fixed effects
- Absorbs anything that is stable within a given city-month cell (e.g. "Boston courts in January always behave this way")
- More demanding — uses more degrees of freedom but provides much cleaner identification
Judge × Month Two-Way Fixed Effects
The most demanding specification: a fixed effect for each judge-month combination:
feols(res ~ Temperature | judge^month, data = asy.data)
- 266 judges × ~12 months ≈ 2,677 fixed effects
- Absorbs individual judge tendencies that vary by month (e.g. Judge Smith is always stricter in January)
- Highest within-: this specification explains the most variance in grant rates
Progression of fixed effect specifications
Specification Fixed effects What's controlled Nationality only Nationality Applicant country + City FE Nationality + City Everything stable about a city + Month FE Nationality + City + Month Stable city traits + seasonal patterns City × Month FE Nationality + City×Month City traits + city-specific seasonality Judge × Month FE Judge×Month Everything stable about each judge each month
Clustered Standard Errors
When using fixed effects, the standard OLS standard errors are usually wrong. Why? Because observations within the same group (same judge, same city) are likely correlated with each other in ways the model doesn't fully capture.
What are clustered standard errors?
They account for within-group correlation of errors — the fact that observations in the same cluster are not independent.
Rule of thumbCluster your standard errors at the level of your fixed effect (or coarser). If you have city fixed effects, cluster at the city level. If you have judge fixed effects, cluster at the judge level. This ensures your inference is valid even when errors are correlated within cities/judges.
Comparison of SE types
| SE type | When to use | Code |
|---|---|---|
| Huber-White (HC) | Heteroskedasticity across observations (different error variance for different obs) | vcov = 'hetero' |
| Newey-West (HAC) | Time series: serial correlation + heteroskedasticity | vcov = 'newey_west' |
| Clustered | Panel data: within-group error correlation | cluster = ~city |
# Heteroskedasticity-robust SEs
feols(res ~ Temperature | city, data = asy.data, vcov = 'hetero')
# Newey-West HAC SEs
feols(res ~ Temperature | city, data = asy.data, vcov = 'newey_west')
# City-clustered SEs
feols(res ~ Temperature | city, data = asy.data, cluster = ~city)
Don't use default (homoskedastic) SEs with panel dataDefault OLS standard errors assume all errors are i.i.d. — equal variance and independent. In panel data with fixed effects, within-group error correlation is almost guaranteed. Using default SEs will make your estimates look more precise than they are, inflating t-statistics and producing spuriously significant results.
Preview: Instrumental Variables in Panel Data
Paper: Cai, De Janvry & Sadoulet (2013) — insurance take-up among Chinese rice farmers.
Research question: Does social learning / peer effects drive insurance take-up? (i.e. if your friends buy insurance, does that make you buy it too?)
The endogeneity problem: A farmer's insurance take-up might be correlated with their friends' take-up simply because they face similar risks (shared weather, shared information) — not because of peer influence. This is correlated unobservables — a classic OVB problem.
The Instrument: Default Buy Option
In a randomised trial, some farmers were assigned a default "buy" option (opt-out rather than opt-in for insurance). This randomisation:
- Affects their friends' actual take-up rate (relevant ✅)
- Only affects the farmer's own take-up via how it affects their friends' take-up (excludable ✅) — the default assignment was random, so it has no direct effect on the farmer's fundamentals
2SLS in feols
feols(takeup ~ 1 | pre_takeup_rate ~ default_buy, data = farmers)
The | separates the first-stage instrument from the main equation. The F-stat of 54.8 far exceeds the rule-of-thumb threshold of 10, confirming a strong first stage.
Wu-Hausman test for endogeneityThe Wu-Hausman test checks whether the endogenous regressor is actually endogenous (i.e. whether OLS is biased). Null hypothesis: OLS is consistent (regressor is exogenous). If the test rejects, we have evidence that IV is necessary.
A significant Wu-Hausman test here confirms that friends' take-up rate is genuinely endogenous — OLS would have given biased estimates.
Why does randomisation of default options solve endogeneity?Default assignment is random — farmers didn't self-select into "default buy" vs. "default don't buy". So it's uncorrelated with the farmer's own risk exposure, risk preferences, and other confounders. The only channel through which default assignment can affect a farmer's outcome is via their peers' take-up. This is exactly what makes it a valid instrument.
Summary
-
Panel data tracks the same individuals over time. It enables within-individual comparisons that control for all time-invariant characteristics.
-
Between variation compares averages across individuals — vulnerable to cross-individual confounders (self-selection, culture, etc.).
-
Within variation compares an individual to themselves over time — controls for all stable individual traits, observed or not.
-
Individual fixed effects model: . The absorbs all time-invariant traits of individual . is the within-individual effect of .
-
Within-estimation (demeaning): subtract each variable's individual mean (), then run OLS on the demeaned data. Equivalent to adding individual dummy variables.
-
City fixed effects control for time-invariant city characteristics. After adding them, temperature's effect on asylum grant rates disappears — city confounders were driving the raw correlation.
-
Two-way fixed effects add a second dimension (e.g. city + month, or judge × month) for more rigorous control of multiple sources of confounding.
-
Clustered standard errors are required when observations within a group (city, judge) have correlated errors. Cluster at the level of the fixed effect.
-
IV in panel data: when the key regressor is endogenous even within individuals, use instruments. The default buy randomisation isolates exogenous variation in peers' take-up, enabling causal identification of peer effects.
Related Notes
- Previous: Lec_07-Time Trends and Event Studies — time trends, detrending, event studies
- Hub: Econometrics
- Key concepts: Panel Data, Fixed Effects, Within Estimator, Demeaning, Between Variation, Within Variation, Time Fixed Effects, Two-Way Fixed Effects, Clustered Standard Errors, Individual Fixed Effect
- IV concepts: Instrumental Variables, Two Stage Least Squares, First Stage, Wu-Hausman Test, Endogeneity