Tutorials › Econometrics (Graduate) › Two-Way Fixed Effects In Static Panels

Fixed and Random Effects · Tutorial 260 of 952

Two-Way Fixed Effects In Static Panels

See how double demeaning removes additive unit and time effects, and how the remaining variation determines whether two-way fixed-effects slopes can be estimated.

Intermediate 9 min read

What You'll Learn

  • Specify a static panel model with both unit and time effects.
  • Double demean outcomes and regressors in a balanced panel.
  • Calculate a two-way fixed-effects slope from transformed data.
  • Identify the variation that remains available for slope estimation.
  • Recognize when unit and time effects absorb a regressor completely.
  • Distinguish estimation from the additional assumptions needed for inference.

Why Use Two-Way Fixed Effects?

Panel data follow the same units over multiple periods. Outcomes can differ across units for persistent reasons, such as location or baseline health, and across periods because of shocks shared by all units. If either kind of factor is related to the regressors, a regression that leaves it out may attribute its association with the outcome to the regressors instead.

A two-way fixed-effects model represents these two sources of additive heterogeneity explicitly. Its slope is estimated from the part of the data that is not explained by unit-specific levels or time-specific levels. In a balanced panel, that part can be isolated by double demeaning: subtract each observation’s unit mean and time mean, then add back the grand mean.

Definition: For \(N\) units observed in each of \(T\) periods, a static two-way fixed-effects model is $$ y_{it}=x_{it}'\beta+\alpha_i+\lambda_t+u_{it}, \qquad i=1,\ldots,N,\quad t=1,\ldots,T. $$ Here \(y_{it}\) is the outcome, \(x_{it}\) is a vector of regressors, \(\beta\) is a common slope vector, \(\alpha_i\) is a unit effect, \(\lambda_t\) is a time effect, and \(u_{it}\) is the remaining error. “Static” means the model has no lagged outcome as a regressor.

The panel is balanced when every one of the \(N\) units is observed in all \(T\) periods. The effects are additive: the model allows each unit its own level and each period its own level, but does not include a separate effect for every unit-period combination. An intercept is redundant with the full sets of unit and time effects, so a dummy-variable regression must use a normalization or omit a reference category.

For the usual slope interpretation, a key condition is strict exogeneity: conditional on the regressors from all periods and the fixed effects, the error has conditional mean zero in every period. In shorthand, \(E(u_{it}\mid x_{i1},\ldots,x_{iT},\alpha_i,\lambda_1,\ldots,\lambda_T)=0\). This condition is stronger than requiring the error to be uncorrelated only with the contemporaneous regressor. Fixed effects allow \(x_{it}\) to be related to \(\alpha_i\) or \(\lambda_t\); they do not, by themselves, solve endogeneity from \(u_{it}\).

Double Demeaning

In a balanced panel, define the unit mean, time mean, and grand mean of any variable \(z_{it}\) by \(\bar z_i=T^{-1}\sum_t z_{it}\), \(\bar z_t=N^{-1}\sum_i z_{it}\), and \(\bar z=(NT)^{-1}\sum_i\sum_t z_{it}\). Its double-demeaned value is \(\ddot z_{it}=z_{it}-\bar z_i-\bar z_t+\bar z\). Apply this transformation separately to the outcome and to every regressor.

Key result: In a balanced panel with additive unit and time effects, two-way fixed-effects least squares estimates \(\beta\) by regressing \(\ddot y_{it}\) on \(\ddot x_{it}\), with no separate intercept: $$ \hat\beta_{\mathrm{TWFE}} = \left(\sum_{i=1}^{N}\sum_{t=1}^{T}\ddot x_{it}\ddot x_{it}'\right)^{-1} \left(\sum_{i=1}^{N}\sum_{t=1}^{T}\ddot x_{it}\ddot y_{it}\right), $$ provided the matrix being inverted has full rank. For a single regressor, this is \(\hat\beta_{\mathrm{TWFE}}=\sum_{i,t}\ddot x_{it}\ddot y_{it}/\sum_{i,t}\ddot x_{it}^{\,2}\).

The transformation removes the effects because a unit-specific constant is unchanged by taking its unit mean, and a time-specific constant is unchanged by taking its time mean. For example, \(\ddot\alpha_i=\alpha_i-\alpha_i-\bar\alpha+\bar\alpha=0\); the same cancellation holds for \(\lambda_t\). Thus \(\ddot y_{it}=\ddot x_{it}'\beta+\ddot u_{it}\). Least squares on this transformed equation gives the displayed estimator.

Double-demeaned values also sum to zero within every unit and within every period. For instance, summing over periods gives \(\sum_t\ddot z_{it}=\sum_t z_{it}-T\bar z_i-\sum_t\bar z_t+T\bar z=0\), because in a balanced panel \(\sum_t\bar z_t=T\bar z\). The corresponding sum over units is zero as well. These properties describe the residual variation left after removing both sets of additive levels.

Worked Example: Double-Demeaning a Two-by-Two Panel

Consider a hypothetical balanced panel with one regressor. Rows are units and columns are periods:

Period 1Period 2
Outcome \(y\), unit 148
Outcome \(y\), unit 2713
Regressor \(x\), unit 112
Regressor \(x\), unit 235

For \(x\), the unit means are \(1.5\) and \(4\), the time means are \(2\) and \(3.5\), and the grand mean is \(2.75\). Therefore, \(\ddot x_{11}=1-1.5-2+2.75=0.25\). Applying the same calculation to all four observations gives \(\ddot x=\begin{pmatrix}0.25&-0.25\\-0.25&0.25\end{pmatrix}\).

For \(y\), the unit means are \(6\) and \(10\), the time means are \(5.5\) and \(10.5\), and the grand mean is \(8\). Thus \(\ddot y=\begin{pmatrix}0.5&-0.5\\-0.5&0.5\end{pmatrix}\). The estimated slope is $$ \hat\beta_{\mathrm{TWFE}} = \frac{(0.25)(0.5)+(-0.25)(-0.5)+(-0.25)(-0.5)+(0.25)(0.5)} {0.25^2+(-0.25)^2+(-0.25)^2+0.25^2} = \frac{0.5}{0.25} =2. $$

In this hypothetical panel, the outcome’s double-demeaned values are twice the regressor’s, so the estimated two-way fixed-effects slope is 2 outcome units per regressor unit. This calculation illustrates the estimator; with only four observations, it would not support reliable general inference by itself.

What Variation Identifies the Slope?

The slope is identified only if the double-demeaned regressors contain enough independent variation. With one regressor, the denominator \(\sum_{i,t}\ddot x_{it}^{\,2}\) must be positive. With several regressors, the matrix \(\sum_{i,t}\ddot x_{it}\ddot x_{it}'\) must have full rank. This is a condition about the transformed data, not simply about whether the original regressors vary.

In substantive terms, the estimator uses variation that remains after subtracting both a unit’s average level and a period’s average level. A variable that only differs between units but does not change over time is absorbed by unit effects. A variable that changes over time in exactly the same way for every unit is absorbed by time effects. More generally, any regressor expressible as the sum of a unit component and a time component has zero double-demeaned value.

Worked Example: Checking the Corrected Cross-Product

Consider a hypothetical balanced panel whose regressor and outcome already have zero row means and zero column means. They are therefore already double demeaned: $$ \ddot X= \begin{pmatrix} 1&0&-1\\ 0&1&-1\\ -1&-1&2 \end{pmatrix}, \qquad \ddot Y=\frac{14}{45} \begin{pmatrix} 1&0&-1\\ 0&1&-1\\ -1&-1&2 \end{pmatrix}. $$ Because both arrays have zero row and column means, applying the double-demeaning formula leaves them unchanged.

The sum of squared regressor values is \(\sum_{i,t}\ddot x_{it}^{\,2}=1+0+1+0+1+1+1+1+4=10\). The cross-product is $$ \sum_{i,t}\ddot x_{it}\ddot y_{it} = \frac{14}{45}+\frac{14}{45}+\frac{14}{45}+\frac{14}{45} +\frac{14}{45}+\frac{14}{45}+\frac{56}{45} = \frac{140}{45} = \frac{28}{9} \approx 3.111. $$

Consequently, \(\hat\beta_{\mathrm{TWFE}}=(28/9)/10=14/45\approx0.3111\). The estimated slope is about 0.3111 outcome units per regressor unit. The cross-product is \(28/9\), not \(10/3\); using an incorrect cross-product would also give an incorrect slope.

Worked Example: A Regressor Absorbed by Both Sets of Effects

Suppose a hypothetical regressor takes the values $$ X= \begin{pmatrix} 1&2&3\\ 2&3&4\\ 3&4&5 \end{pmatrix}, $$ with rows representing units and columns representing periods. Each entry is the sum of a unit component \((1,2,3)\) and a time component \((0,1,2)\). The row means are \(2,3,4\), the column means are \(2,3,4\), and the grand mean is \(3\).

For example, \(\ddot x_{11}=1-2-2+3=0\). The same calculation gives zero for every cell: each observation is fully explained by its unit and time components. Hence \(\sum_{i,t}\ddot x_{it}^{\,2}=0\), and the two-way fixed-effects slope cannot be estimated from this regressor. Having different raw values is not enough; there must be variation beyond additive unit and time patterns.

Interpretation, Assumptions, and Common Pitfalls

Two-way fixed effects control for all unit characteristics that are constant over the observed periods and all period shocks common to the units, provided these enter additively as specified. This can remove important sources of confounding, but it does not establish that a slope is causal. A causal interpretation requires an appropriate research design and assumptions about the remaining error, including the exogeneity condition stated above. Nor does double demeaning dictate a standard error formula: dependence in errors across periods within a unit or across units in a period can matter for inference.

  • Pitfall: using raw variation to claim identification. A regressor may vary substantially in the original data yet be entirely explained by unit and time effects. Check its double-demeaned values.
  • Pitfall: subtracting both means without adding back the grand mean. The correct formula is \(z_{it}-\bar z_i-\bar z_t+\bar z\). The grand-mean term prevents subtracting the overall level twice.
  • Pitfall: treating fixed effects as a cure for all endogeneity. They remove additive unit and time components, but they do not automatically remove time-varying omitted factors or feedback between regressors and errors.
  • Pitfall: interpreting a slope as a between-unit comparison. The two-way estimator uses residualized variation, not the raw differences in average regressor levels across units or periods.
Key takeaway: In a balanced static panel with additive unit and time effects, double demeaning removes both sets of effects. The slope is estimated from the remaining variation only, and it exists only when the transformed regressors have full rank.

Check Your Understanding

Try these questions before moving on.

  1. Write the double-demeaning formula for \(z_{it}\) and explain why the grand mean is added back.
  2. A regressor is constant over time within each unit but differs across units. What happens to its double-demeaned values, and can its slope be estimated with unit and time effects?
  3. State the rank condition required to estimate a vector of slopes from double-demeaned regressors.
  4. Why does removing unit and time effects not, by itself, justify a causal interpretation?
  5. In a balanced panel, what kind of regressor variation remains available for two-way fixed-effects slope estimation?
Clinical Biostats