Tutorials › Econometrics (Graduate) › Two-Way Clustered Inference Across Units and Time

Panel Dependence and Inference · Tutorial 324 of 952

Two-Way Clustered Inference Across Units and Time

Construct two-way clustered standard errors, understand their dependence assumptions, and assess whether the unit and time dimensions provide enough clusters for inference.

Advanced 12 min read

What You'll Learn

  • Define unit, time, and unit-time-cell score contributions for a panel estimator.
  • Construct the two-way cluster-robust meat by adding unit and time components and subtracting the cell component.
  • Explain which score dependence patterns two-way clustering permits and which it does not.
  • Work with unbalanced panels and observed unit-time intersections.
  • Identify why adequate cluster counts in both dimensions matter for inference.
  • Recognize negative covariance estimates and other limits of the inclusion-exclusion estimator.

Why Cluster Along Two Dimensions?

Panel observations can be dependent in two different directions. Measurements from the same unit may be related over time, while measurements from different units in the same period may respond to a shared shock. Clustering only by unit allows the first pattern but not the second; clustering only by time allows the second but not the first. Two-way clustered inference is designed to account for both patterns in a single covariance estimate.

The method is based on an inclusion-exclusion calculation. It adds the score contributions clustered by unit and those clustered by time, then subtracts the contribution clustered by unit-time cell. Subtraction is needed because observations in the same cell belong to both a unit cluster and a time cluster. This adjustment concerns the covariance estimate, not the coefficient estimate.

Model, Scores, and the Two-Way Covariance Estimate

Consider a panel with units indexed by \(i=1,\ldots,N\) and periods indexed by \(t=1,\ldots,T\). The panel may be unbalanced: let \(\mathcal{O}\) denote the set of observed unit-time cells. For concreteness, suppose the coefficient vector \(\hat{\beta}\) is estimated by ordinary least squares in the linear model \(y_{it}=x_{it}'\beta+u_{it}\), for \((i,t)\in\mathcal{O}\). Here \(x_{it}\) is a column vector of regressors and \(u_{it}\) is the regression error. The same clustering construction applies to other estimators with suitable observation-level estimating scores and a corresponding bread matrix.

For OLS, define the fitted residual \(\hat{u}_{it}=y_{it}-x_{it}'\hat{\beta}\) and the observation-level score contribution \(\hat{\psi}_{it}=x_{it}\hat{u}_{it}\). Sum these scores within each unit, each time period, and each observed unit-time cell:

$$ \hat{S}_i=\sum_{t:(i,t)\in\mathcal{O}}\hat{\psi}_{it}, \qquad \hat{S}_t=\sum_{i:(i,t)\in\mathcal{O}}\hat{\psi}_{it}, \qquad \hat{S}_{it}=\hat{\psi}_{it}. $$

With one observation per cell, the intersection cluster is just that observation. If a cell contains several observations, \(\hat{S}_{it}\) is their summed score. The three meat matrices are formed from the outer products of these sums.

$$ \widehat{M}_{\mathrm{unit}}=\sum_{i=1}^{N}\hat{S}_i\hat{S}_i', \qquad \widehat{M}_{\mathrm{time}}=\sum_{t=1}^{T}\hat{S}_t\hat{S}_t', \qquad \widehat{M}_{\mathrm{cell}}=\sum_{(i,t)\in\mathcal{O}}\hat{S}_{it}\hat{S}_{it}'. $$
Definition: The raw two-way cluster-robust covariance estimate for OLS is
$$ \widehat{\operatorname{Var}}_{\mathrm{2way}}(\hat{\beta}) = \widehat{Q}^{-1} \left( \widehat{M}_{\mathrm{unit}} +\widehat{M}_{\mathrm{time}} -\widehat{M}_{\mathrm{cell}} \right) \widehat{Q}^{-1}, \qquad \widehat{Q}=\sum_{(i,t)\in\mathcal{O}}x_{it}x_{it}'. $$
The standard error for a coefficient is the square root of its estimated variance, provided that variance is nonnegative. The displayed formula is the unadjusted, or raw, version; software may apply finite-sample multipliers.

The “bread” matrices \(\widehat{Q}^{-1}\) convert the score-based meat into a covariance matrix on the coefficient scale. For a scalar coefficient, if the bread is \(0.1\) and the meat is \(80\), the estimated variance is \(0.1(80)0.1=0.80\) coefficient-units squared. The corresponding standard error is \(\sqrt{0.80}=0.8944\) coefficient units—not score units per coefficient unit.

Why Inclusion-Exclusion Works

The score-based variance can be understood by expanding the cluster sums into pairs of observation scores. A pair in the same unit contributes to the unit-cluster sum; a pair in the same period contributes to the time-cluster sum. A pair in the same unit-time cell is included in both sums, so it must be subtracted once. Pairs that share neither a unit nor a time period are not included.

More formally, consider two observation scores, \(\hat{\psi}_{it}\) and \(\hat{\psi}_{js}\). In the sum of outer products by unit, their cross-product appears when \(i=j\). In the sum by time, it appears when \(t=s\). If both conditions hold, it is counted twice; the cell sum subtracts one copy. Thus the net multiplicity is one when the pair shares a unit or a period, and zero when it shares neither. This pairwise counting is the inclusion-exclusion justification for the meat formula.

$$ \mathbf{1}\{i=j\}+\mathbf{1}\{t=s\} -\mathbf{1}\{i=j\text{ and }t=s\} = \begin{cases} 1,& i=j\text{ or }t=s,\\ 0,& i\ne j\text{ and }t\ne s. \end{cases} $$

This algebra explains what the estimator targets; it does not by itself guarantee valid inference. Consistency also relies on the relevant dependence being contained within the two clustering dimensions, along with regularity conditions for the estimator and sufficiently many informative clusters.

Dependence Assumptions and Cluster Counts

Two-way clustering allows broad dependence among score contributions from the same unit, even across distant periods, and broad dependence among contributions from different units in the same period. It does not generally allow arbitrary dependence between observations that share neither a unit nor a time period. For example, a common shock that induces correlation across different units and persists across different periods may violate that restriction. A different covariance method may be needed for such a structure.

For conventional two-way cluster asymptotics, both the number of units and the number of time periods must be large, with no single cluster dominating the relevant score variation and with appropriate moment and design conditions. The required amount of information is not determined by the number of observations alone. A panel can contain many unit-time observations but still have too few units, too few periods, or highly unbalanced clusters for dependable inference. If one dimension has only a small number of clusters, conventional large-cluster approximations in that dimension can be unreliable. Few-cluster corrections and bootstrap methods can be relevant, but they do not turn a very small cluster count into abundant independent information.

The two dimensions also need to match the dependence pattern in the application. Clustering by unit and time is not a universal correction for “panel dependence.” It is appropriate when the consequential dependence is plausibly organized along those two dimensions and the asymptotic information conditions are credible. It does not automatically account for dependence across neighboring locations, persistent common factors spanning periods, or other structures that do not fit the unit/time clustering scheme.

Worked Example: Computing the Meat in a Balanced Panel

Suppose a hypothetical scalar-coefficient estimation has four observed unit-time cells. The fitted score contributions, in the order shown, are as follows. Their total is zero, as expected for a scalar OLS score at the fitted coefficient in this setup.

UnitPeriod 1 scorePeriod 2 score
A11
B-1-1

Unit-cluster meat. The unit score sums are \(2\) for A and \(-2\) for B, so \( \widehat{M}_{\mathrm{unit}}=2^2+(-2)^2=8.\)

Time-cluster meat. Both time score sums are zero: \(1+(-1)=0\). Therefore \( \widehat{M}_{\mathrm{time}}=0^2+0^2=0.\)

Cell meat and two-way meat. The sum of the four squared cell scores is \(1^2+1^2+(-1)^2+(-1)^2=4\). Hence \( \widehat{M}_{\mathrm{2way}}=8+0-4=4.\) If the scalar bread is \(0.1\), the covariance estimate is \(0.1(4)0.1=0.04\) coefficient-units squared, and the standard error is \(\sqrt{0.04}=0.2000\) coefficient units.

Worked Example: An Unbalanced Panel and Observed Cells

In a hypothetical panel, three units have the following score contributions. Unit C is not observed in period 2.

UnitPeriod 1Period 2
A22
B-1-1
C-2Not observed

The total score is \(2+2-1-1-2=0\). Unit score sums are \(4,-2,-2\), giving \( \widehat{M}_{\mathrm{unit}}=4^2+(-2)^2+(-2)^2=24.\) The period sums are \(-1\) and \(1\), giving \( \widehat{M}_{\mathrm{time}}=(-1)^2+1^2=2.\) For the five observed cells, the cell meat is \( \widehat{M}_{\mathrm{cell}}=2^2+2^2+(-1)^2+(-1)^2+(-2)^2=14.\) Thus the two-way meat is \(24+2-14=12\). If the scalar bread is \(0.25\), then the estimated variance is \(0.25(12)0.25=0.75\) coefficient-units squared, and the standard error is \(\sqrt{0.75}=0.8660\) coefficient units. The unobserved C-period-2 cell contributes nothing: cluster sums are formed from observed scores, not from imputed zero-valued observations.

Worked Example: Interpreting a Two-Way Clustered Test

Suppose a hypothetical panel has \(N=40\) units and \(T=25\) periods, with observations and regressors suitable for estimation in both dimensions. A regression estimates an exposure coefficient of \(0.60\) outcome units per exposure unit, and its two-way cluster-robust standard error is \(0.20\) outcome units per exposure unit. Assume the large-cluster approximation is considered reasonable for this illustration.

To test \(H_0:\beta=0\) against \(H_a:\beta\ne 0\), the asymptotic statistic is \( z=0.60/0.20=3.00.\) Under the standard normal reference distribution, the two-sided p-value is approximately \(0.00270\). At significance level \(\alpha=0.05\), this is evidence against the null hypothesis of a zero coefficient, conditional on the model and inference assumptions. It does not establish that exposure causes the outcome.

A 95% large-sample confidence interval is \(0.60\pm1.96(0.20)=0.60\pm0.392,\) or \([0.208,0.992]\) outcome units per exposure unit. The interval procedure has 95% repeated-sampling coverage under its assumptions; it is not a statement that the fixed coefficient has a 95% probability of lying in this realized interval. The values \(N=40\) and \(T=25\) alone do not prove that the approximation is reliable: cluster sizes, leverage, score variation, and the dependence structure matter as well.

Practical Checks and Common Pitfalls

In practice, the covariance estimate is computed from residual scores after estimating the model. Report which dimensions were clustered and the numbers of clusters in each. State whether a finite-sample adjustment was used, since different software may implement different adjustments. Such multipliers change the estimate; they do not change the underlying dependence assumptions. The raw inclusion-exclusion covariance can also fail to be positive semidefinite in finite samples. A negative estimated variance for a particular contrast cannot be square-rooted to produce a standard error; it signals a limitation of the raw estimate in that sample, not a meaningful negative uncertainty.

  • Wrong: “The panel has thousands of observations, so two-way clustered inference must be reliable.” Correction: Check the number and quality of clusters in both dimensions; a large observation count does not replace enough independent cluster information.
  • Wrong: “Adding unit and time covariance estimates can only make uncertainty larger.” Correction: Inclusion-exclusion subtracts the intersection component, and finite-sample estimates can even yield a negative variance for some contrasts.
  • Wrong: “Two-way clustering covers any dependence in a panel.” Correction: It permits dependence among observations sharing a unit or period, but not unrestricted dependence between observations sharing neither.
  • Wrong: “The cell term can be omitted in an unbalanced panel.” Correction: Subtract the outer products for observed unit-time intersections; the overlap correction remains essential even when cells are missing.
Key takeaway: Two-way clustered inference uses the unit meat plus the time meat minus the observed cell meat. Its validity depends on a credible unit/time dependence structure and adequate cluster information in both dimensions.

Check Your Understanding

Answer each question using the definitions and conditions developed above.

  1. Why does the two-way meat subtract the unit-time-cell component rather than simply adding the unit and time components?
  2. For an unbalanced panel, which observations contribute to a time-cluster score sum?
  3. Describe one dependence pattern that unit-and-time clustering permits and one that it does not generally cover.
  4. Why can a large number of unit-time observations still be insufficient for reliable two-way clustered inference?
  5. A raw two-way covariance estimate gives a negative estimated variance for a coefficient contrast. What does that imply about calculating its standard error?
Clinical Biostats