MethodAtlas
PanelEstablished

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

Stata: reghdfe y x1, absorb(unit_id year) vce(cluster unit_id)
R: feols(y ~ x1 | unit_id + year, data = df, vcov = ~unit_id)
Python: 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 ii different from worker jj — 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:

Yit=βXit+αi+λt+εitY_{it} = \beta X_{it} + \alpha_i + \lambda_t + \varepsilon_{it}

where:

  • YitY_{it} is the outcome (wages) for unit ii at time tt
  • XitX_{it} is the treatment/covariate of interest (union status)
  • αi\alpha_i is a unit fixed effect — everything about unit ii that does not change over time (ability, gender, race, permanent personality traits)
  • λt\lambda_t is a time fixed effect — everything that changes over time but affects all units the same way (macroeconomic conditions, inflation, policy changes)
  • εit\varepsilon_{it} is the idiosyncratic error

What Fixed Effects Do

Including αi\alpha_i (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 λt\lambda_t (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:

Yˉi=1Tit=1TiYit,Xˉi=1Tit=1TiXit\bar{Y}_i = \frac{1}{T_i} \sum_{t=1}^{T_i} Y_{it}, \quad \bar{X}_i = \frac{1}{T_i} \sum_{t=1}^{T_i} X_{it}

Subtracting the unit mean from the original equation:

(YitYˉi)=β(XitXˉi)+(εitεˉi)(Y_{it} - \bar{Y}_i) = \beta(X_{it} - \bar{X}_i) + (\varepsilon_{it} - \bar{\varepsilon}_i)

The fixed effect αi\alpha_i has vanished — because αiαi=0\alpha_i - \alpha_i = 0. 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:

E[εitXi1,Xi2,,XiT,αi]=0  tE[\varepsilon_{it} | X_{i1}, X_{i2}, \ldots, X_{iT}, \alpha_i] = 0 \quad \forall \; t

This condition means the error at time tt 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 αi\alpha_i is uncorrelated with the regressors), both FE and RE are consistent but RE is more efficient. Under the alternative, only FE is consistent.

H=(β^FEβ^RE)[Var(β^FE)Var(β^RE)]1(β^FEβ^RE)χk2H = (\hat{\beta}_{FE} - \hat{\beta}_{RE})'[\text{Var}(\hat{\beta}_{FE}) - \text{Var}(\hat{\beta}_{RE})]^{-1}(\hat{\beta}_{FE} - \hat{\beta}_{RE}) \sim \chi^2_k

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.

Interactive Simulation

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).

03.817.6211.43Simulated ValueTrue Within…Between-Gro…Number ofParameters

Computed Results

Pooled OLS Estimate (biased in this DGP)
2.60
FE Within Estimate (unbiased in this DGP)
1.00
OLS Bias
1.60
05
05
550
Interactive Simulation

Why Fixed Effects?

Panel DGP: Yᵢₜ = αᵢ + γₜ + 2.0·Xᵢₜ + ε, where αᵢ is correlated with X (strength = 1.5). 10 units observed over 8 periods.

-10.3-4.51.27.012.818.5Covariate (X)Outcome (Y)
Pooled OLSTrue slopeWithin-unit lines

Estimation Results

Estimatorβ̂SE95% CIBias
Pooled OLS3.4880.066[3.36, 3.62]+1.488
Individual FE2.4580.156[2.15, 2.76]+0.458
Two-Way FEclosest1.9670.115[1.74, 2.19]-0.033
True β2.000
10

Cross-sectional units in the panel

8

Time periods per unit

2.0

The causal effect of X on Y

1.5

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:

Yit=Xitβ+αi+εitY_{it} = X_{it}'\beta + \alpha_i + \varepsilon_{it}

Take the unit-specific time average:

Yˉi=Xˉiβ+αi+εˉi\bar{Y}_i = \bar{X}_i'\beta + \alpha_i + \bar{\varepsilon}_i

Subtract:

Y¨it=X¨itβ+ε¨it\ddot{Y}_{it} = \ddot{X}_{it}'\beta + \ddot{\varepsilon}_{it}

where Y¨it=YitYˉi\ddot{Y}_{it} = Y_{it} - \bar{Y}_i (the demeaned variable).

The OLS estimator on the demeaned data is:

β^FE=(itX¨itX¨it)1(itX¨itY¨it)\hat{\beta}_{FE} = \left(\sum_i \sum_t \ddot{X}_{it}\ddot{X}_{it}'\right)^{-1} \left(\sum_i \sum_t \ddot{X}_{it}\ddot{Y}_{it}\right)

Standard errors: Because demeaning introduces serial correlation in the transformed error ε¨it\ddot{\varepsilon}_{it}, 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 nkNn - k - N degrees of freedom, where NN 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)
Requiresfixestplm

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 variable reports 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 β^\hat{\beta} on average."
  • This coefficient is not the same as the cross-sectional comparison: "workers in unions earn β^\hat{\beta} 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

ProblemWhat It DoesHow to Fix It
Time-varying confoundersFE does not remove them; estimates are biasedAdd controls, use DiD design, or conduct sensitivity analysis
Nickell biasIncluding lagged dependent variables with FE produces biased estimates in short panelsUse Arellano-Bond GMM or long panels
Low within variationEstimates are very imprecise, wide confidence intervalsCheck xtsum; consider RE if the Hausman test supports it
Not clustering SEsDramatically understated standard errorsCluster at the unit level (at minimum)
Cannot estimate time-invariant effectsPerfectly collinear with unit dummiesUse RE, Mundlak approach, or Hausman-Taylor
TWFE with staggered treatmentBiased if treatment effects are heterogeneousUse modern estimators (Callaway-Sant'Anna, Sun-Abraham)
(Callaway & Sant'Anna, 2021)
Assumption Failure Demo

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.

Assumption Failure Demo

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.

Assumption Failure Demo

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.

Concept Check

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

Concept Check

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?

Concept Check

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?

Concept Check

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?

Concept Check

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?

Referee Exercise

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

VariableCoefficientSEp-value
CEO has MBA0.0240.0090.008
Firm size (log)0.0310.0120.010
Firm FEYes
Year FEYes
Clustered SEsFirm level
N18,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.

Guided Exercise

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.

What does the teacher fixed effect absorb?

Why can she not estimate the effect of gender on test scores in this model?

What type of variation identifies the effect of earning a master's degree mid-career?

If teachers strategically pursue master's degrees when they expect to switch to higher-performing schools, is the estimate still valid?

Error Detective

Read the analysis below carefully and identify the errors.

A labor economist studies whether right-to-work (RTW) laws reduce wages. Using state-level panel data (50 states, 20 years), they estimate: reghdfe avg_wage rtw_law unemployment_rate, absorb(state year) vce(robust) They find: coefficient on RTW law = -0.034 (SE = 0.012, p = 0.005). They write: "After controlling for state and year fixed effects, right-to-work laws reduce average wages by 3.4%. Standard errors are robust to heteroskedasticity."

Select all errors you can find:

Error Detective

Read the analysis below carefully and identify the errors.

A management researcher studies whether firms that adopt enterprise resource planning (ERP) systems experience higher productivity. Using a panel of 3,000 manufacturing firms over 8 years, they run: reghdfe log_productivity erp_adopted firm_size, absorb(firm_id) vce(cluster firm_id) They report: "The coefficient on ERP adoption is 0.15 (SE = 0.04, p < 0.001). With firm fixed effects, we compare each firm to itself before and after ERP adoption, eliminating all confounders. We also include lagged productivity as a control to account for mean reversion." In a robustness check, they add lagged log_productivity to the model and find the coefficient drops to 0.04.

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: ΔYit=βΔXit+Δεit\Delta Y_{it} = \beta \Delta X_{it} + \Delta \varepsilon_{it}. 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.

EconometricaDOI: 10.2307/1913646

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.

EconometricaDOI: 10.2307/1913827

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.

MIT Press

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.

Review of Economic StudiesDOI: 10.2307/2297110

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?.

American Journal of Political ScienceDOI: 10.1111/ajps.12417

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.

American Economic ReviewDOI: 10.1257/aer.20181169

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.

Working Paper

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.

Quarterly Journal of EconomicsDOI: 10.1162/003355303322552775

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.

American Economic ReviewDOI: 10.1257/aer.104.9.2593

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.

Strategic Management JournalDOI: 10.1002/smj.524

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.

Quarterly Journal of EconomicsDOI: 10.1162/qjec.122.2.647

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.

EconometricaDOI: 10.2307/1911408

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.

Princeton University PressDOI: 10.1515/9781400829828

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.

Cambridge University Press

Chapter 21 covers panel data methods comprehensively, including fixed effects, random effects, and dynamic panel models. A standard graduate-level reference for microeconometric methods.

Tags

panelcontinuous-outcometime-invariant-confounders