Panel Data: Fixed Effects, Random Effects, and the Hausman Test

Lectures 13–15

Author
Affiliation

Henrique Veras

PIMES/UFPE

Where this lecture sits

DGPIdentificationEstimationAsymptoticsInference
Computation

We now observe the same unit repeatedly — \(n\) units, each over \(T\) periods. That extra structure is a gift and a trap. The gift: a unit carries its own unobserved, time-invariant traits — ability, management quality, soil fertility — collected in a single term \(\alpha_i\), and repeated observation lets us sweep it out. The trap: if \(\alpha_i\) is correlated with the regressors (it usually is — it is omitted-variable bias from Lecture 9, now with a name), pooled OLS is inconsistent. The whole block turns on one question — is \(\alpha_i\) correlated with \(\mathbf x_{it}\)? If yes, use fixed effects (the within transformation, which kills \(\alpha_i\) with no assumption about it). If no, random effects is a GLS estimator that is more efficient and keeps time-invariant regressors. The Hausman test decides. We then put fixed effects to work — twin studies, and the honest limits of the design — and close with the cleanest application of all: difference-in-differences.

What is the DGP?—What is the DGP, here?

\(y_{it}=\mathbf x_{it}'\bbeta+\alpha_i+\varepsilon_{it}\), with \(\varepsilon_{it}\) the usual idiosyncratic shock and \(\alpha_i\) a time-invariant unit effect. Everything hinges on \(\Cov(\alpha_i,\mathbf x_{it})\): zero → random effects; nonzero → fixed effects.

1 Part I · The panel model

Units observed over time

A panel follows \(n\) units across \(T\) periods. The payoff is a term for everything about a unit that does not change over time — \(\alpha_i\) — that a single cross-section could never separate from the error.

Pooled OLS: the benchmark and its two failures

Stack all \(\sum_i T_i\) observations and run OLS, ignoring the panel structure. Fold \(\alpha_i\) into a composite error \(w_{it}=\alpha_i+\varepsilon_{it}\): \[y_{it}=\mathbf x_{it}'\bbeta+\underbrace{\alpha_i+\varepsilon_{it}}_{w_{it}}.\] If the classical assumptions held for \(w_{it}\), pooled OLS would be efficient and we would be done. They usually do not — in two distinct ways.

Cluster-robust inference for the pooled model

Take Failure 2 first. Stack unit \(i\)’s \(T_i\) rows as \(\mathbf y_i=\mathbf X_i\bbeta+\mathbf w_i\), with \(\Var[\mathbf w_i\mid\mathbf X_i]=\boldsymbol\Omega_i\) — unknown, but block-diagonal across units (independence across \(i\), arbitrary correlation within). The sandwich then sums over units: \[\widehat{\Var}(\mathbf b)=\Big(\textstyle\sum_i\mathbf X_i'\mathbf X_i\Big)^{-1}\Big(\sum_i\mathbf X_i'\hat{\mathbf w}_i\hat{\mathbf w}_i'\mathbf X_i\Big)\Big(\sum_i\mathbf X_i'\mathbf X_i\Big)^{-1}.\]

Researcher's Toolbox—Which level do you cluster on?

Cluster at the coarsest grouping where errors may correlate: a genuine hierarchy (students in schools over time) moves the cluster up to the school, whose block absorbs both over-time and across-student dependence. Clustering too fine understates the SE; with few clusters, use the wild cluster bootstrap.

2 Part II · Three ways to use the variation

The between (group-means) estimator

Collapse each unit to its time-average and regress the means on the means: \[\bar y_i=\bar{\mathbf x}_i'\bbeta+\bar w_i,\qquad \bar w_i=\alpha_i+\bar\varepsilon_i.\] This uses only the between-unit (cross-sectional) variation.

First differences

A second way to kill \(\alpha_i\): difference adjacent periods. Since \(\alpha_i\) is constant, it drops: \[y_{it}-y_{i,t-1}=(\mathbf x_{it}-\mathbf x_{i,t-1})'\bbeta+(\varepsilon_{it}-\varepsilon_{i,t-1}) \quad\Longrightarrow\quad \Delta y_{it}=\Delta\mathbf x_{it}'\bbeta+\Delta\varepsilon_{it}.\]

The within estimator

The third option — and the one that makes panels powerful. Subtract each unit’s time-average (the within, or demeaning, transformation). Since \(\alpha_i\) is constant over \(t\), it cancels — with no assumption about it: \[\underbrace{y_{it}-\bar y_i}_{\ddot y_{it}}=(\mathbf x_{it}-\bar{\mathbf x}_i)'\bbeta+(\varepsilon_{it}-\bar\varepsilon_i) \quad\Longrightarrow\quad \hat\bbeta_{\text{within}}=(\ddot{\mathbf X}'\ddot{\mathbf X})^{-1}\ddot{\mathbf X}'\ddot{\mathbf y}.\]

Within, between, total: one decomposition

The three estimators are tied together by an identity on the sums of squares. With overall means \(\bar{\bar{\mathbf x}}\), \[\mathbf S_{xx}^{\text{total}}=\mathbf S_{xx}^{\text{between}}+\mathbf S_{xx}^{\text{within}}, \qquad \mathbf S_{xy}^{\text{total}}=\mathbf S_{xy}^{\text{between}}+\mathbf S_{xy}^{\text{within}}.\] Hence pooled OLS is a matrix-weighted average of the between and within estimators: \[\mathbf b^{\text{total}}=\mathbf F^{\text{within}}\mathbf b^{\text{within}}+\mathbf F^{\text{between}}\mathbf b^{\text{between}},\qquad \mathbf F^{\text{within}}=\mathbf I-\mathbf F^{\text{between}}.\]

3 Part III · Fixed effects

Fixed effects: the default, and its one assumption

The within estimator of Part II is the fixed-effects estimator — \(\hat\bbeta_{FE}=\hat\bbeta_{\text{within}}\). Its robustness (killing \(\alpha_i\) with no restriction on \(\Cov(\alpha_i,\mathbf x_{it})\)) is why it is the default when correlated heterogeneity is a worry. It is not assumption-free, though.

Within \(\equiv\) LSDV, via FWL

Where does the heterogeneity live? In a unit-specific intercept — each unit sits at its own level \(\alpha_i\). Nothing ties the \(\alpha_i\) across units, so the honest move is to treat each as a free parameter to estimate. A dummy for every unit does exactly that: in the least-squares dummy-variable (LSDV) model the \(i\)-th column of \(\mathbf D\) switches on for unit \(i\), so its coefficient \(\alpha_i\) is that unit’s own intercept — \[\mathbf y=\mathbf D\boldsymbol\alpha+\mathbf X\bbeta+\boldsymbol\varepsilon \qquad(n\text{ intercepts}+K\text{ slopes}).\] With \(n\) in the thousands \(\mathbf D\) is enormous — but we only want \(\bbeta\), and FWL (Lecture 3) says to partial the dummies out first. Partialling them out — applying \(\mathbf M_{\mathbf D}=\mathbf I-\mathbf D(\mathbf D'\mathbf D)^{-1}\mathbf D'\) — is exactly subtracting each unit’s mean, i.e. demeaning: \[\hat\bbeta_{LSDV}=(\mathbf X'\mathbf M_{\mathbf D}\mathbf X)^{-1}\mathbf X'\mathbf M_{\mathbf D}\mathbf y =(\ddot{\mathbf X}'\ddot{\mathbf X})^{-1}\ddot{\mathbf X}'\ddot{\mathbf y}=\hat\bbeta_{\text{within}}.\]

LSDV and within: the payoff

One estimator, two readings — and the equivalence buys us two concrete things.

Researcher's Toolbox—Under the hood

Why feols / reghdfe absorb millions of fixed effects without inverting a huge matrix: they never form \(\mathbf D\) — they just demean (iteratively alternating over units and periods for two-way effects). FWL turned an impossible inversion into a subtraction.

Researcher's Toolbox—A degrees-of-freedom trap

Demeaning spends \(n\) degrees of freedom — one unit-mean each — so the honest residual count is \(nT-n-K\), not \(nT-K\). The \(n\) intercepts you “never estimated” reappear exactly here: software that demeans must add back the \(-n\), or the SE comes out too small.

Two-way fixed effects: adding time

Units are not the only source of common, unobserved shocks — a recession, a nationwide reform, a price spike hits everyone in period \(t\). Add a time effect \(\gamma_t\): \[y_{it}=\mathbf x_{it}'\bbeta+\alpha_i+\gamma_t+\varepsilon_{it}.\]

Fixed effects or first differences?

Both kill \(\alpha_i\) and use only within variation, and at \(T=2\) they coincide. For \(T>2\) they differ, and the choice is about the error structure.

4 Part IV · Random effects

The error-components model

Now assume \(\alpha_i\) (write it \(u_i\)) is uncorrelated with \(\mathbf x_{it}\): \(\E[u_i\mid\mathbf X_i]=0\). Throwing away the between variation (as FE does) is then wasteful. Keep it, and treat the composite error \(\eta_{it}=u_i+\varepsilon_{it}\) as having an equicorrelated structure within each unit: \[\boldsymbol\Sigma=\E[\boldsymbol\eta_i\boldsymbol\eta_i']=\sigma_\varepsilon^2\mathbf I_T+\sigma_u^2\,\mathbf i_T\mathbf i_T', \qquad \boldsymbol\Omega=\mathbf I_n\otimes\boldsymbol\Sigma.\]

Random effects as GLS: quasi-demeaning

GLS is OLS on \(\boldsymbol\Sigma^{-1/2}\)-transformed data, and here the transform is a partial demeaning — subtract only a fraction \(\theta\) of the unit mean: \[y_{it}-\theta\,\bar y_i=(\mathbf x_{it}-\theta\,\bar{\mathbf x}_i)'\bbeta+(\cdots), \qquad \theta=1-\frac{\sigma_\varepsilon}{\sqrt{\sigma_\varepsilon^2+T\sigma_u^2}}.\]

Feasible GLS: estimating the variance components

\(\theta\) depends on the unknown \(\sigma_\varepsilon^2,\sigma_u^2\), so we estimate them first, then run GLS with \(\hat\theta\) — a feasible GLS two-step.

5 Part V · Choosing between them: the Hausman test

FE or RE? Consistency vs. efficiency

The two estimators trade off consistency against efficiency:

\(\Cov(\alpha_i,\mathbf x_{it})=0\) (RE valid) \(\Cov(\alpha_i,\mathbf x_{it})\neq0\)
FE (within) consistent, inefficient consistent
RE (GLS) consistent, efficient inconsistent

So under \(H_0:\Cov(\alpha_i,\mathbf x_{it})=0\) the two should agree; a large gap is evidence against RE.

The Hausman statistic

\[\mathbf H=(\hat\bbeta_{FE}-\hat\bbeta_{RE})'\big[\Var(\hat\bbeta_{FE})-\Var(\hat\bbeta_{RE})\big]^{-1}(\hat\bbeta_{FE}-\hat\bbeta_{RE})\ \dto\ \chi^2_{K}.\]

Researcher's Toolbox—Why we usually report FE anyway — even when Hausman does not reject

(1) Credibility — RE needs \(\Cov(\alpha_i,\mathbf x_{it})=0\), but correlated heterogeneity is the very reason we reached for panel data; assuming it away is usually implausible. (2) The test is weak — “fail to reject” is not “accept”; Hausman has low power, so it often misses a correlation that is really there. (3) Asymmetric stakes — RE buys a modest efficiency gain but risks inconsistency; FE is consistent either way. Use RE mainly when you need time-invariant coefficients — then consider Hausman–Taylor or the Mundlak device (add the unit means \(\bar{\mathbf x}_i\) to RE: it reproduces FE and exposes the very correlation RE assumes away).

6 Part VI · Fixed effects in practice

Matched designs: twin fixed effects

Fixed effects work for any grouping that shares an unobserved confounder — not only units-over-time. A matched pair is a group of size two.

What fixed effects do not solve

Fixed effects remove time-invariant confounders. They are not a cure-all — and selling them as one is a common error.

7 Part VII · Difference-in-differences

Two groups, two periods

The cleanest use of two-way fixed effects. A policy switches on for a treated group (\(Treat_i=1\)) in the post period (\(Post_t=1\)); a control group never gets it. One regression with the interaction: \[y_{it}=\beta_0+\beta_1\,Post_t+\beta_2\,Treat_i+\delta\,(Post_t\times Treat_i)+\varepsilon_{it}.\]

A wide table showing the predicted outcome in each Treatment-by-Post cell, the post-minus-pre difference for each group, and the difference between those differences, which equals delta.

Writing the predicted mean in each of the four cells and taking the two differences, everything cancels except \(\delta\): the difference of the differences.

A clean application: Duflo (2001)

Indonesia built 61,000 schools (1973–78), more intensively where more children were out of school. Children young enough to attend (aged 2–6 in 1974) were exposed; older children (12–17) were not. Two differences — region intensity × cohort — give a DiD with no regression.

A two-by-two table titled Intensity of the Building Program. Rows are the young cohort aged 2 to 6 and the older cohort aged 12 to 17; columns are High and Low intensity. Differences are 0.47 and 0.36; the difference-in-differences is 0.12 with standard error 0.089.

The older cohort is the control — same regions, too old to benefit — so its High−Low gap (0.36) is the counterfactual for the young cohort’s (0.47). DiD \(=0.47-0.36=0.12\) extra years of schooling, from the program.

Duflo: falsification and dynamics

The transparent design makes its assumptions checkable — the two robustness moves every credible DiD now reports.

A placebo difference-in-differences plot for cohorts that should show no effect; the estimated difference is near zero.

Placebo: a DiD on cohorts too old to benefit returns ≈0 — no spurious effect.

Program effect estimated separately per birth cohort; near zero for older cohorts and increasing for younger, exposed cohorts.

Effect by cohort: ≈0 for cohorts too old, rising for the exposed young cohorts — an event study in age.

Threats, extensions, and the frontier

8 Part VIII · Modern difference-in-differences

When adoption is staggered

Textbook DiD has one treatment date. Real policies roll out unit by unit over years — states adopt a law at different times. The natural generalisation is two-way fixed effects with a staggered indicator \(D_{it}=\mathbf 1\{\text{unit } i \text{ treated at } t\}\): \[y_{it}=\alpha_i+\gamma_t+\delta\,D_{it}+\varepsilon_{it}.\] It looks innocent — but \(\hat\delta\) is now a single number summarising many treatments that may bite differently and grow over time.

What TWFE estimates: Goodman–Bacon

Goodman–Bacon (2021): the staggered TWFE estimate is a weighted average of every possible 2×2 DiD between timing groups — and the weights, fixed by group sizes and timing, need not be positive.

Two horizontal timelines, Group A (early) and Group B (late). Each is untreated (faint) until its adoption date, then treated (coloured). Between A's and B's adoption dates, a clean 2x2 compares treated A with not-yet-treated B. After B's adoption, a forbidden 2x2 compares B with already-treated A, flagged as carrying a negative weight if effects grow.

With two adoption dates, TWFE forms 2×2’s of two kinds. Clean: before B adopts, use B as a not-yet-treated control for A. Forbidden: after B adopts, use A — already treated — as a “control” for B; if A’s effect is still growing, this comparison enters with a negative weight.

The modern event study

The event study — one coefficient per period relative to adoption — is the right object (it shows dynamics and lets us check pre-trends). But under staggered timing the naive TWFE event study inherits the same contamination.

The heterogeneity-robust toolkit

A wave of estimators rebuilds DiD from clean comparisons, letting effects vary by group and over time. We name and place them — the formal development is Econometria II.

9 Part IX · Synthesis

What to take away

10 References

References

  • Greene, Econometric Analysis, ch. 11 (Models for Panel Data).
  • Hansen, Econometrics, ch. 17 (Panel Data).
  • Hausman (1978), “Specification Tests in Econometrics”, Econometrica.
  • Black, Devereux & Salvanes (2007), “From the Cradle to the Labor Market? The Effect of Birth Weight”, QJE.
  • Duflo (2001), “Schooling and Labor Market Consequences of School Construction in Indonesia”, AER.
  • Frontier (Econometria II): Callaway & Sant’Anna (2021); Sun & Abraham (2021).
Back to top