Fixed Effects (Two-Way FE)
Removes time-invariant unobserved confounders by exploiting within-unit variation over time.
Quick Reference
- When to Use
- When you have panel data and worry about unobserved time-invariant confounders that differ across units.
- Key Assumption
- Strict exogeneity: E[error | X_all_periods, unit_effect] = 0. All confounders are either observed or time-invariant — FE cannot remove time-varying unobservables.
- Common Mistake
- Claiming FE eliminates all confounders — it only removes time-invariant ones. Time-varying confounders remain. Also, not checking whether the key variable has sufficient within-unit variation.
- Estimated Time
- 3 hours
One-Line Implementation
reghdfe y x1, absorb(unit_id year) vce(cluster unit_id)feols(y ~ x1 | unit_id + year, data = df, vcov = ~unit_id)PanelOLS(df['y'], df[['x1']], entity_effects=True, time_effects=True).fit(cov_type='clustered', cluster_entity=True)Download Full Analysis Code
Complete scripts with diagnostics, robustness checks, and result export.
Motivating Example: The Effect of Unionization on Wages
You want to know whether joining a union raises a worker's wages. You have panel data tracking thousands of workers over ten years, observing their union status and wages each year. A simple cross-sectional OLS regression of wages on union membership is almost certainly biased: workers who join unions differ systematically from those who do not — in ability, motivation, industry, occupation, and dozens of other ways you cannot fully measure.
But here is the key insight: with panel data, you can compare the same worker before and after they join a union. Whatever unobserved characteristics make worker different from worker — ability, motivation, family background — those characteristics are constant over time for the same worker. The fixed effects model strips all of that confounding out.
This within-unit comparison is the power of fixed effects: it exploits within-unit variation over time, effectively asking "what happens to the same worker's wages when their union status changes?" rather than "do workers in unions earn more than workers not in unions?"
A. Overview: The Logic of Fixed Effects
The Panel Data Model
Consider a simple panel model:
where:
- is the outcome (wages) for unit at time
- is the treatment/covariate of interest (union status)
- is a unit fixed effect — everything about unit that does not change over time (ability, gender, race, permanent personality traits)
- is a time fixed effect — everything that changes over time but affects all units the same way (macroeconomic conditions, inflation, policy changes)
- is the idiosyncratic error
What Fixed Effects Do
Including (unit fixed effects) is equivalent to including a separate dummy variable for each unit. This inclusion absorbs all time-invariant differences between units — both observed and unobserved.
Including (time fixed effects) absorbs all temporal shocks that are common across units.
Together, two-way fixed effects (TWFE) control for unit-level permanent differences and common time trends. The identifying variation that remains is within-unit, over-time variation that deviates from common trends.
What Fixed Effects Do NOT Do
Common Confusions
B. Identification: The Within Transformation
The Math Behind "Demeaning"
The within transformation works by subtracting the unit-specific mean from each variable. Define:
Subtracting the unit mean from the original equation:
The fixed effect has vanished — because . What remains is the within-unit variation, which is why this approach is called the "within estimator."
The Key Assumption: Strict Exogeneity
For the FE estimator to be consistent, you need strict exogeneity:
This condition means the error at time is uncorrelated with the regressors at all time periods — past, present, and future. This restriction rules out feedback effects (where past outcomes affect future treatment). If there are lagged dependent variables, strict exogeneity fails, and FE is biased (Nickell bias).
(Nickell, 1981)The Hausman Test: FE vs. RE
The Hausman test compares FE and RE estimates. Under the null hypothesis that RE is consistent (i.e., the unit effect is uncorrelated with the regressors), both FE and RE are consistent but RE is more efficient. Under the alternative, only FE is consistent.
A significant test statistic rejects RE in favor of FE.
C. Visual Intuition
Imagine a scatterplot of wages (Y) vs. years of experience (X) for multiple workers. Without fixed effects, you would draw one regression line through all the data. But each worker has a different starting wage (some are high-ability, some low-ability), creating parallel clouds at different heights.
Fixed effects is like drawing a separate regression line for each worker (or equivalently, centering each worker's data on their own mean). You are no longer comparing high-ability workers to low-ability workers. You are looking at how each worker's own wages change when their experience (or union status) changes.
The slope you estimate is the average of these within-person slopes — purged of all between-person differences.
Within vs Between Variation
The pooled OLS estimate is biased by between-group confounding; given time-invariant confounding, the FE within estimator stays on target regardless of how strong that confounding is, provided the DGP satisfies strict exogeneity (no time-varying confounders).
Computed Results
- Pooled OLS Estimate (biased in this DGP)
- 2.60
- FE Within Estimate (unbiased in this DGP)
- 1.00
- OLS Bias
- 1.60
Why Fixed Effects?
Panel DGP: Yᵢₜ = αᵢ + γₜ + 2.0·Xᵢₜ + ε, where αᵢ is correlated with X (strength = 1.5). 10 units observed over 8 periods.
Estimation Results
| Estimator | β̂ | SE | 95% CI | Bias |
|---|---|---|---|---|
| Pooled OLS | 3.488 | 0.066 | [3.36, 3.62] | +1.488 |
| Individual FE | 2.458 | 0.156 | [2.15, 2.76] | +0.458 |
| Two-Way FEclosest | 1.967 | 0.115 | [1.74, 2.19] | -0.033 |
| True β | 2.000 | — | — | — |
Cross-sectional units in the panel
Time periods per unit
The causal effect of X on Y
How strongly αᵢ correlates with X (0 = no confounding)
Why the difference?
Pooled OLS is biased (+1.49) because individual effects αᵢ are correlated with X. Units with higher inherent characteristics tend to have both higher X and higher Y, inflating the estimated slope. Individual FE removes this bias by using only within-unit variation (demeaning by unit), yielding a much closer estimate (β̂ = 2.458). Two-Way FE additionally removes common time trends, providing the most accurate estimate in this panel setting.
D. Mathematical Derivation
Don't worry about the notation yet — here's what this means in words: Subtracting unit-specific means from all variables removes the unobserved fixed effect, leaving only within-unit variation for identification.
Start with the model:
Take the unit-specific time average:
Subtract:
where (the demeaned variable).
The OLS estimator on the demeaned data is:
Standard errors: Because demeaning introduces serial correlation in the transformed error , it is recommended to cluster standard errors at the unit level. This clustering also handles arbitrary within-unit heteroskedasticity and autocorrelation.
Degrees of freedom: FE estimation uses degrees of freedom, where is the number of units. With many units and short panels, this matters.
Equivalence: The FE estimator is numerically identical to OLS with unit dummy variables (the Least Squares Dummy Variable estimator, LSDV). Software uses the within transformation for computational efficiency.
E. Implementation
library(fixest)
# One-way FE
fe1 <- feols(wages ~ union + tenure + hours_worked | worker_id,
data = df, vcov = ~worker_id)
summary(fe1)
# Two-way FE
fe2 <- feols(wages ~ union + tenure + hours_worked | worker_id + year,
data = df, vcov = ~worker_id)
summary(fe2)
# Hausman test
library(plm)
pdata <- pdata.frame(df, index = c("worker_id", "year"))
fe_plm <- plm(wages ~ union + tenure + hours_worked, data = pdata, model = "within")
re_plm <- plm(wages ~ union + tenure + hours_worked, data = pdata, model = "random")
phtest(fe_plm, re_plm)F. Diagnostics and Robustness Checks
Is There Enough Within Variation?
If your key variable barely changes within units over time, FE will have very low power. Check the within variation:
- In Stata:
xtsum variablereports between and within standard deviations. - If the within SD is tiny relative to the between SD, FE is identifying off very little variation, and your estimates will be imprecise.
F-Test for Joint Significance of Fixed Effects
Test whether the unit fixed effects are jointly significant. If not, pooled OLS may be preferred. In Stata: test after areg or check the F-stat from reghdfe.
Cluster Your Standard Errors
(Bertrand et al., 2004)Robustness to Functional Form
Report results with and without time fixed effects. If the coefficient changes dramatically when you add time FE, common time trends were confounding your estimate. Also consider adding unit-specific linear trends if you worry about differential trends.
Interpreting Results
- The FE coefficient measures the within-unit effect: "when union status changes for a given worker, their wages change by on average."
- This coefficient is not the same as the cross-sectional comparison: "workers in unions earn more than workers not in unions."
- If the FE estimate is much smaller than the OLS estimate, it suggests that positive selection (higher-ability workers select into unions) was inflating the OLS coefficient.
- Time fixed effects control for common shocks. If wages grew nationally during your sample period, time FE prevent you from attributing that growth to changes in union status.
G. What Can Go Wrong
| Problem | What It Does | How to Fix It |
|---|---|---|
| Time-varying confounders | FE does not remove them; estimates are biased | Add controls, use DiD design, or conduct sensitivity analysis |
| Nickell bias | Including lagged dependent variables with FE produces biased estimates in short panels | Use Arellano-Bond GMM or long panels |
| Low within variation | Estimates are very imprecise, wide confidence intervals | Check xtsum; consider RE if the Hausman test supports it |
| Not clustering SEs | Dramatically understated standard errors | Cluster at the unit level (at minimum) |
| Cannot estimate time-invariant effects | Perfectly collinear with unit dummies | Use RE, Mundlak approach, or Hausman-Taylor |
| TWFE with staggered treatment | Biased if treatment effects are heterogeneous | Use modern estimators (Callaway-Sant'Anna, Sun-Abraham) |
Time-Varying Confounders Bias FE Estimates
Researcher includes controls for time-varying confounders (job changes, hours worked) alongside worker FE
FE estimate of union wage premium: 0.08 (SE = 0.02). Adding controls for concurrent job changes reduces the estimate to 0.06, suggesting some time-varying confounding. Both estimates are reported for transparency.
Nickell Bias from Lagged Dependent Variables
Panel has T = 30 periods and does not include a lagged dependent variable, or uses Arellano-Bond GMM for dynamic specifications
FE estimate of union premium: 0.08 (SE = 0.02). No lagged dependent variable is included. Specification with Arellano-Bond GMM for the dynamic model gives a similar estimate of 0.07.
Insufficient Within Variation
Key variable (union status) changes for 30% of workers during the panel, providing substantial within-variation for identification
FE estimate: 0.08 (SE = 0.02). The within standard deviation of union status is 0.35 (compared to between SD of 0.46). The estimate is precisely identified with adequate variation.
You estimate the effect of union membership on wages using (1) pooled OLS and (2) worker fixed effects. The OLS coefficient is 0.18 and the FE coefficient is 0.08. What is the most likely explanation for the difference?
H. Practice
A researcher includes worker fixed effects and claims her estimate is 'free of omitted variable bias.' She finds that workers who get promoted earn 15% more. A colleague points out that promotions coincide with relocations to higher-cost-of-living cities. Is the colleague's concern valid?
You want to estimate the effect of a worker's gender on wages using panel data. Can you include gender in a worker fixed effects model?
A study uses firm fixed effects and year fixed effects (two-way FE) to estimate the impact of adopting a new technology on firm productivity. The researcher has 500 firms over 20 years, but only 15 firms adopt the technology during the sample period. What concern should you raise?
A researcher adds lagged wages as a control variable in a worker fixed effects model with T = 5 periods. The coefficient on union membership drops from 0.08 to 0.02. What is the most likely explanation?
Read the paper summary below and write a brief referee critique (2-3 sentences) of the identification strategy.
Paper Summary
A study examines whether CEO education (MBA vs. no MBA) affects firm performance. Using a panel of 2,000 firms over 15 years, the authors regress firm ROA on a CEO MBA dummy, controlling for firm size, industry, and year. They include firm fixed effects to control for unobserved firm characteristics.
Key Table
| Variable | Coefficient | SE | p-value |
|---|---|---|---|
| CEO has MBA | 0.024 | 0.009 | 0.008 |
| Firm size (log) | 0.031 | 0.012 | 0.010 |
| Firm FE | Yes | ||
| Year FE | Yes | ||
| Clustered SEs | Firm level | ||
| N | 18,500 | ||
| R-squared (within) | 0.12 |
Authors' Identification Claim
Firm fixed effects control for all time-invariant firm characteristics. Therefore, the coefficient on CEO MBA captures the causal effect of hiring an MBA-educated CEO.
Applying Fixed Effects: Teacher Effectiveness and Student Test Scores
A district administrator wants to know whether students taught by teachers with a master's degree score higher on standardized tests. She has panel data on 800 teachers observed across 5 years, and plans to estimate a model with teacher fixed effects and year fixed effects.
Read the analysis below carefully and identify the errors.
Select all errors you can find:
Read the analysis below carefully and identify the errors.
Select all errors you can find:
I. Swap-In: When to Use Something Else
- Random Effects: When you believe unobserved unit effects are uncorrelated with regressors (Hausman test does not reject). More efficient than FE and can estimate time-invariant effects.
- Correlated Random Effects (Mundlak): Add group means of time-varying regressors to the RE model. Gives FE-equivalent coefficients for time-varying variables while also estimating time-invariant effects.
- First differencing: Instead of demeaning, take first differences: . Equivalent to FE with two periods; with more periods, FE is generally more efficient unless errors follow a random walk.
- Arellano-Bond GMM: For dynamic panels (lagged dependent variable) where FE is biased. Uses past levels as instruments for first-differenced equations.
J. Reviewer Checklist
Critical Reading Checklist
Paper Library
Foundational (7)
Mundlak, Y. (1978). On the Pooling of Time Series and Cross Section Data.
Mundlak showed that the fixed effects estimator can be understood as an OLS regression that includes the group means of all time-varying regressors. This 'correlated random effects' interpretation bridges the fixed effects and random effects models and clarifies exactly what assumption is being relaxed.
Hausman, J. A. (1978). Specification Tests in Econometrics.
Hausman developed a general test for comparing two estimators—one consistent under a broader set of assumptions (fixed effects) and one efficient under stronger assumptions (random effects). The 'Hausman test' for choosing between fixed and random effects is one of the most frequently used specification tests in applied economics.
Wooldridge, J. M. (2010). Econometric Analysis of Cross Section and Panel Data.
Wooldridge's graduate textbook is the standard reference for panel data econometrics. Chapters 10–14 provide a thorough treatment of fixed effects, random effects, and related panel data methods, covering both linear and nonlinear models with careful attention to assumptions.
Chamberlain, G. (1980). Analysis of Covariance with Qualitative Data.
Chamberlain extended the fixed effects approach to nonlinear models like logit, showing how to condition out the fixed effects in discrete choice settings. This work is fundamental for researchers who need fixed effects in models where the dependent variable is binary or categorical.
Imai, K., & Kim, I. S. (2019). When Should We Use Unit Fixed Effects Regression Models for Causal Inference with Longitudinal Data?.
Imai and Kim provided a modern causal-inference framework for understanding when unit fixed effects regression yields unbiased estimates with longitudinal data. They clarified the often-implicit assumptions about treatment history and carryover effects, offering a more rigorous foundation for applied fixed effects analysis.
de Chaisemartin, C., & D'Haultfoeuille, X. (2020). Two-Way Fixed Effects Estimators with Heterogeneous Treatment Effects.
This paper showed that two-way fixed effects (TWFE) estimators can produce severely biased estimates when treatment effects are heterogeneous across groups or time periods, particularly with staggered adoption designs. It proposed alternative estimators and catalyzed a major literature on robust difference-in-differences methods.
Correia, S. (2016). Linear Models with Multi-Way Fixed Effects and Clustered Standard Errors.
Introduces the reghdfe Stata command for fast estimation of linear models with multiple levels of fixed effects, now the standard tool for applied researchers working with high-dimensional fixed effects in panel data.
Application (5)
Abowd, J. M., Kramarz, F., & Margolis, D. N. (1999). High Wage Workers and High Wage Firms.
This landmark paper used worker and firm fixed effects jointly to decompose wage variation into worker ability and firm pay premia. The 'AKM' model has become the standard framework for studying labor market sorting, wage inequality, and the role of firms in wage-setting.
Bertrand, M., & Schoar, A. (2003). Managing with Style: The Effect of Managers on Firm Policies.
Bertrand and Schoar used manager fixed effects (tracking CEOs who moved between firms) to show that individual managerial 'style' explains a significant portion of the variation in corporate investment, financial, and organizational practices. This paper is a key reference linking fixed effects methods to management questions.
Chetty, R., Friedman, J. N., & Rockoff, J. E. (2014). Measuring the Impacts of Teachers I: Evaluating Bias in Teacher Value-Added Estimates.
This paper used teacher fixed effects (value-added models) and quasi-experimental validation to measure individual teachers' causal impacts on student outcomes. It demonstrated that teacher fixed effects capture real causal effects, not just selection, and has influenced education policy worldwide.
Henderson, A. D., Miller, D., & Hambrick, D. C. (2006). How Quickly Do CEOs Become Obsolete? Industry Dynamism, CEO Tenure, and Company Performance.
This strategy paper used firm fixed effects to study how CEO tenure affects performance in dynamic versus stable industries. It found that long CEO tenure becomes a liability in fast-changing industries, demonstrating the value of fixed effects for controlling for stable firm characteristics in strategy research.
Bennedsen, M., Nielsen, K. M., Pérez-González, F., & Wolfenzon, D. (2007). Inside the Family Firm: The Role of Families in Succession Decisions and Performance.
Uses exogenous variation in CEO succession decisions driven by the gender of the departing CEO's firstborn child to study the effect of family versus professional management on firm performance. A widely cited example of using a natural experiment to address endogeneity in corporate governance research.
Survey (3)
Nickell, S. (1981). Biases in Dynamic Models with Fixed Effects.
Nickell showed that including a lagged dependent variable in a fixed effects regression creates a bias that does not vanish as the number of cross-sectional units grows. This 'Nickell bias' is a critical concern for researchers using fixed effects in dynamic panel models with short time series.
Angrist, J. D., & Pischke, J.-S. (2009). Mostly Harmless Econometrics: An Empiricist's Companion.
Chapter 5 provides an excellent treatment of fixed effects in practice, including the case for always preferring FE over RE. This widely read textbook shaped a generation of applied economists' approach to panel data methods.
Cameron, A. C., & Trivedi, P. K. (2005). Microeconometrics: Methods and Applications.
Chapter 21 covers panel data methods comprehensively, including fixed effects, random effects, and dynamic panel models. A standard graduate-level reference for microeconometric methods.