Lectures 13–15
PIMES/UFPE
\(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.
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.
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.
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}.\]
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.
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.
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 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}.\]
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}}.\]
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.
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}}.\]
One estimator, two readings — and the equivalence buys us two concrete things.
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.
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.
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}.\]
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.
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.\]
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}}.\]
\(\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.
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.
\[\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}.\]
(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).
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.
Fixed effects remove time-invariant confounders. They are not a cure-all — and selling them as one is a common error.
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}.\]

DiD buys identification with one untestable assumption: absent the policy, treated and control would have moved on parallel paths, so the control’s post−pre change stands in for the treated’s missing counterfactual.

With many periods and unit and time effects, estimate one coefficient per period relative to adoption — the event study (normalise \(\delta_{-1}=0\)): \[y_{it}=\alpha_i+\gamma_t+\textstyle\sum_{k\neq-1}\delta_k\,\mathbf 1\{t-t^{*}_i=k\}+\varepsilon_{it}.\]

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.

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


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.
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.
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.
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.
did, fixest::sunab, did2s in R).