Fixed-Effects Panel Regression in Stata: What the Within Transformation Actually Drops
Fixed-effects regression in Stata sweeps out entity-specific intercepts, but the within transformation drops all time-invariant variables and requires strict exogeneity. This blog walks through the mechanics, a worked firm-level example, and the trade-offs you accept when you type xtreg, fe.
17 Jun 2026, 05:18 UTC

The Problem: Unobserved Heterogeneity That Biases Your Coefficients
You have panel data—say, firm-year observations on R&D spending and productivity. Firms differ in ways you cannot measure: management culture, patent portfolio quality, historical luck. If those time-invariant traits correlate with both R&D and productivity, a pooled OLS estimate will be biased. Fixed-effects (FE) regression solves this by sweeping out each entity's mean, but the mechanics of that sweep have consequences many applied researchers overlook.
Takeaway: Stata's xtreg, fe implements the within transformation, which identifies coefficients only from within-entity variation. Anything that does not change within a firm—industry dummies, founding year, time-invariant policy regimes—drops out automatically. The trade-off is you lose between-entity information, and the estimator assumes strict exogeneity conditional on the fixed effects.
How the Within Transformation Works in Stata
When you run xtreg y x1 x2, fe, Stata de-means every variable by entity: y_it - ȳ_i, x1_it - x̄1_i, etc. The entity-specific intercept α_i vanishes because it is constant within each panel. The resulting estimator is numerically identical to running OLS on the de-meaned data with areg absorbing the panel identifier, but xtreg, fe reports the correct degrees of freedom for the fixed effects.
Two practical defaults matter:
- Robust standard errors: Add
, vce(robust)to guard against heteroskedasticity across entities. The default conventional SEs assume homoskedastic idiosyncratic errors, which is rarely plausible in firm or country panels. - Clustered standard errors: If you suspect serial correlation within entities (common in macro panels), use
, vce(cluster idvar)instead. This also handles heteroskedasticity.
Worked Example: Firm R&D and Productivity
Suppose you have an unbalanced panel of 1,200 manufacturing firms over 2005–2020. The variable lprod is log value-added per worker, lrd is log R&D stock, and age is firm age. You want the elasticity of productivity with respect to R&D, controlling for firm fixed effects.
* Declare panel structure\nxtset firm_id year\n\n* Fixed-effects with clustered SEs\nxtreg lprod lrd age, fe vce(cluster firm_id)\n\n* Marginal effect of R&D at mean age (Stata 15+)\nmargins, dydx(lrd) atmeans\n\n* Hausman test vs. random effects\nxtreg lprod lrd age, re\nestimates store re\nxtreg lprod lrd age, fe\nestimates store fe\nhausman fe re\n\n* Serial correlation check\nestat bgodfrey, lags(1 2)\n
Where to run: Stata 15 or later (for margins after xtreg, fe). Requires xtset with a panel identifier and time variable. The hausman command needs both models stored; a significant p-value (e.g., < 0.05) favors FE.
Expected checks: The FE coefficient on lrd uses only within-firm R&D changes. If a firm never changes R&D, it contributes zero information. estat bgodfrey tests for AR(1) or AR(2) serial correlation in the idiosyncratic errors; rejection suggests clustered SEs are necessary but not sufficient—consider xtregar, fe or dynamic panel methods.
What Gets Dropped and Why It Matters
The within transformation removes any regressor that is constant within an entity. In the example, if industry never changes for a firm, its coefficient is not identified. This is a feature, not a bug: FE controls for all time-invariant confounders, observed or not. But it also means you cannot estimate the effect of time-invariant policies (e.g., a one-time deregulation that affects all firms equally) without interacting them with time-varying variables.
Unbalanced panels introduce a subtlety. Stata keeps all observations where the dependent variable and regressors are non-missing. If a firm appears only once, it drops out entirely because within-firm variation is undefined. With xtreg, fe, this is automatic; with areg, you must drop singletons manually or the degrees of freedom will be wrong.
Limitation: Strict Exogeneity Is Strong
FE consistency requires E[ε_it | X_i1, ..., X_iT, α_i] = 0 for all t—strict exogeneity. Lagged dependent variables violate this because y_i,t-1 correlates with ε_i,t-1, which is part of the fixed effect after de-meaning. If your research question needs dynamics, FE is biased; consider Arellano-Bond (xtabond) or system GMM (xtdpdsys) instead.
Another limitation: FE estimates the average within-entity effect. If the true R&D elasticity varies systematically with firm size, the FE coefficient is a weighted average that may not represent any specific firm. Random-effects or correlated random-effects models (xtreg, re with Mundlak terms) can recover between variation, but at the cost of stronger assumptions.
Actionable Checklist Before You Report
- Run
xtdescribeto confirm panel balance and identify singletons. - Estimate FE with
vce(cluster idvar)as default; report conventional SEs only if you have a theoretical reason. - Run Hausman test; if it fails to reject RE, consider whether RE's efficiency gain justifies its assumption.
- Check serial correlation with
estat bgodfrey; if present, note that clustered SEs handle it but dynamic bias may remain. - Use
marginsfor predicted values or marginal effects at meaningful covariate values (Stata 15+). - Document which time-invariant variables dropped out and why they cannot be estimated.
Fixed-effects regression is a powerful tool for causal inference with panel data, but its identification comes from a specific source of variation. Knowing exactly what variation you are using—and what you are discarding—separates a defensible estimate from a fragile one.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.