Tutorials › Causal Inference › Cluster-Robust Inference for DiD

Difference-in-Differences · Tutorial 599 of 972

Cluster-Robust Inference for DiD

Work through the cluster sandwich variance for DiD, its assumptions and finite-sample adjustment, and the distinction between clustering and weighting.

Intermediate 12 min read

What You'll Learn

  • Identify the independent clusters relevant to DiD inference
  • Construct the cluster-robust sandwich variance from cluster-level score contributions
  • Apply and interpret a common CR1 finite-sample adjustment
  • Explain why the cluster count matters more than the observation count for this inference
  • Distinguish a clustering choice from the weighting that defines a DiD estimand
  • Separate causal identification assumptions from standard-error assumptions

Why Cluster-Robust Inference Matters in DiD

A DiD data set can contain many observations without containing many independent pieces of information. For example, thousands of patients may be observed in a small number of hospitals, while treatment is assigned at the hospital level. Outcomes for patients in the same hospital may share shocks, policies, or measurement practices. Treating all patients as independent can therefore make a DiD estimate appear more precise than the design supports.

Cluster-robust inference addresses this dependence by allowing observations within a cluster to be correlated. It does not change the DiD regression coefficient, determine which population is being averaged over, or establish a causal effect. Those are separate questions: the coefficient depends on the regression and its weighting, causal interpretation depends on identifying assumptions such as parallel trends, and the cluster-robust variance estimates uncertainty under a specified dependence structure.

Definition: A cluster is a group of observations whose errors may be dependent, while different clusters are treated as independent for the variance calculation. A cluster-robust standard error is the square root of a sandwich variance estimator that aggregates regression score contributions within clusters before calculating their variation across clusters.

In DiD, a useful default is to cluster at the level at which treatment is assigned, or at a broader level at which relevant shocks may be shared. If policy adoption is determined by state, for example, individuals or counties in the same state are not automatically independent sources of treatment variation. The exact choice still requires a design-based argument: clustering cannot protect against dependence that crosses the chosen cluster boundaries.

The DiD Regression and Its Clustered Variance

For a two-period DiD, a familiar regression is

$$ Y_{it}=\alpha+\lambda Post_t+\gamma Treat_i+\tau(Treat_i\times Post_t)+\varepsilon_{it}. $$

Here \(Y_{it}\) is the outcome for unit \(i\) at time \(t\); \(Post_t\) indicates the post-intervention period; and \(Treat_i\) indicates membership in the treated group. In this specification, the coefficient \(\hat{\tau}\) on the interaction is the regression DiD estimate. In repeated-period or covariate-adjusted specifications, the design matrix \(X\) contains the corresponding terms, and the same cluster-robust construction applies to the fitted regression.

Let \(g=1,\ldots,G\) index clusters. Write \(X_g\) for the rows of the regression design matrix belonging to cluster \(g\), and \(\hat{u}_g\) for the vector of OLS residuals in that cluster. The unadjusted cluster-robust, or CR0, covariance estimator is

$$ \widehat{V}_{CR0} =(X'X)^{-1} \left(\sum_{g=1}^{G}X_g'\hat{u}_g\hat{u}_g'X_g\right) (X'X)^{-1}. $$

The middle term is often called the “meat” of the sandwich. Each cluster contributes its whole score product \(X_g'\hat{u}_g\hat{u}_g'X_g\), including cross-products between observations in that cluster. Those cross-products are precisely what would be lost by treating each row as independent. The estimated variance of \(\hat{\tau}\) is the diagonal entry of \(\widehat{V}_{CR0}\) corresponding to the interaction coefficient; its standard error is the square root of that entry.

Assumptions for the cluster sandwich: The regression is fit by OLS with a correctly specified design matrix; cluster score contributions have mean zero conditional on the regressors; clusters are independent of one another for the variance calculation; and the number of independent clusters is sufficiently large for the sandwich approximation to be reliable. Within a cluster, the estimator permits heteroskedasticity and arbitrary correlation across its observations. It does not permit arbitrary correlation across different clusters.

A common finite-sample adjustment is CR1:

$$ \widehat{V}_{CR1} = \frac{G}{G-1}\frac{N-1}{N-K}\widehat{V}_{CR0}, $$

where \(N\) is the number of regression observations and \(K\) is the number of estimated regression coefficients, including the intercept. This is one common convention, not a universal definition: software may use different corrections or degrees-of-freedom rules. The adjustment scales the estimated covariance matrix; it does not alter the OLS coefficients or make a small number of clusters behave like a large one.

Why the Number of Clusters Matters

The sandwich is built from \(G\) cluster-level score contributions, not from \(N\) independent contributions. When there are many observations per cluster but few clusters, the apparent sample size \(N\) can be large while the information relevant to the cluster-robust approximation remains limited. A large number of rows does not, by itself, justify using a normal approximation for a cluster-robust test.

For large-cluster inference, a coefficient is commonly tested using a Wald statistic: subtract the null value from the estimate and divide by its cluster-robust standard error. Reference distributions and degrees-of-freedom conventions vary; a \(t\) reference with \(G-1\) degrees of freedom is one common choice. Whatever convention is used, the number of independent clusters should be reported and considered. A CR1 multiplier is a modest correction, not a remedy for a very small \(G\). In that setting, specialized small-cluster inference is needed.

Worked Example: Computing a DiD Interaction

Suppose a hypothetical program is introduced in one set of districts. The average outcome is 50 points before and 56 points after in treated districts, and 48 points before and 51 points after in comparison districts. The two-period, two-group regression includes the treatment indicator, the post indicator, and their interaction. What is the interaction estimate?

The treated group changes by \(56-50=6\) points. The comparison group changes by \(51-48=3\) points. Therefore,

$$ \hat{\tau}=(56-50)-(51-48)=6-3=3\text{ points}. $$

In this saturated two-by-two regression, the interaction coefficient equals this difference in changes. It is a causal effect only under the relevant DiD identification assumptions, including parallel trends in the absence of treatment and no anticipation over the periods used. Cluster-robust standard errors address the dependence structure used for inference; they do not establish those assumptions.

Clustering Does Not Choose the Estimand

The clustering level and the weighting in the regression answer different questions. Clustering specifies which observations may have correlated errors for the variance calculation. Weighting determines how observations or groups contribute to the point estimate. Changing the cluster variable while holding the regression and sample fixed ordinarily leaves its OLS coefficient unchanged, although its standard error may change. Changing weights or the regression specification can change the coefficient and thus the estimand.

For example, an unweighted regression on individual records generally gives more total influence to larger clusters than to smaller ones. A regression using one equally weighted observation per cluster instead targets a different average when cluster sizes differ. Merely declaring clusters for the variance calculation does not convert the first regression into the second.

Worked Example: Unequal Cluster Sizes and Different Averages

Consider hypothetical cluster-level outcome changes, measured in points. Three treated clusters have changes \(2,2,8\), and three comparison clusters each have a change of \(0\). Suppose the treated clusters contain \(10,10,\) and \(100\) individuals, respectively, while the comparison clusters together contain 40 individuals, all with change \(0\). Compare an equal-cluster contrast with an individual-weighted contrast.

With equal weight for each cluster, the treated mean change is \((2+2+8)/3=4\) points, and the comparison mean is \(0\). The equal-cluster contrast is therefore \(4-0=4\) points.

If instead the treated change is averaged over individuals, the weighted treated mean is

$$ \frac{10(2)+10(2)+100(8)}{10+10+100} =\frac{840}{120}=7\text{ points}. $$

The individual-weighted comparison mean remains \(0\), so this contrast is \(7\) points. The difference arises from weighting: the large treated cluster has more influence in the individual-weighted calculation. A cluster-robust variance calculation does not decide between these two targets. The small number of clusters here also means that this example should not be taken as support for conventional large-cluster inference.

A Short Derivation and a Numerical Variance Calculation

The sandwich formula follows from the OLS score representation. Conditional on the design matrix, write \(A=X'X\). The OLS estimation error is

$$ \hat{\beta}-\beta=A^{-1}X'\varepsilon =A^{-1}\sum_{g=1}^{G}X_g'\varepsilon_g. $$

If cluster errors have mean zero conditional on \(X\), and distinct clusters are independent, the variance of the sum is the sum of its cluster variances. Thus

$$ \operatorname{Var}(\hat{\beta}\mid X) =A^{-1}\left[ \sum_{g=1}^{G}X_g'\operatorname{Var}(\varepsilon_g\mid X)X_g \right]A^{-1}. $$

This step does not require independent errors within a cluster: \(\operatorname{Var}(\varepsilon_g\mid X)\) may have nonzero off-diagonal entries. Replacing the unknown errors by OLS residuals gives the CR0 estimator. The large-\(G\) justification is that the aggregate variation in the independent cluster score contributions can be estimated from their observed counterparts, subject to regularity conditions and no cluster dominating the total information.

Worked Example: Cluster-Robust Standard Error for a DiD Contrast

For a transparent calculation, suppose each of eight independent hypothetical clusters contributes one outcome change, measured in points. Four treated-cluster changes are \(3,4,2,3\), and four comparison-cluster changes are \(1,0,2,1\). Regressing the cluster changes on an intercept and a treated-cluster indicator gives the difference in the two group means.

The treated mean is \((3+4+2+3)/4=3\) points; the comparison mean is \((1+0+2+1)/4=1\) point. Hence the estimated contrast is \(\hat{\tau}=3-1=2\) points. The residuals within each group are \(0,1,-1,0\), so the residual sum of squares in each group is \(2\).

For this intercept-and-indicator regression, the CR0 variance for the difference in means is the treated residual sum of squares divided by the square of the treated cluster count, plus the corresponding comparison term:

$$ \widehat{\operatorname{Var}}_{CR0}(\hat{\tau}) =\frac{2}{4^2}+\frac{2}{4^2} =\frac{1}{8}+\frac{1}{8} =\frac{1}{4}\text{ points}^2. $$

Thus \(SE_{CR0}(\hat{\tau})=\sqrt{1/4}=0.5000\) points. Here \(N=G=8\) and \(K=2\), so the stated CR1 convention multiplies the variance by

$$ \frac{8}{7}\frac{7}{6}=\frac{4}{3}. $$

The adjusted variance is \((4/3)(1/4)=1/3\text{ points}^2\), and the adjusted standard error is \(\sqrt{1/3}\approx0.5774\) points. This calculation illustrates the mechanics, not a reliable large-cluster test: eight clusters are few for standard cluster-asymptotic inference. It also shows why the CR1 adjustment must be applied to the unrounded variance, rather than by multiplying rounded intermediate values.

Practical Choices and a Common Pitfall

Before reporting a clustered DiD result, make the dependence argument explicit. Ask what units receive treatment, which units share shocks that could induce residual dependence, and whether the proposed clusters can reasonably be treated as independent. Then report the number of clusters, the clustering level, the coefficient, and the standard error. If treatment is assigned at the state level, for instance, the number of individual records is not a substitute for the number of states when describing the basis of the cluster-robust approximation.

Common mistake: “There are 5,000 individual observations, so the cluster-robust standard error is based on a large sample.” This reasoning counts rows as if they supplied independent cluster information. If those records are nested in 12 states and the relevant dependence is within state, the sandwich aggregates scores into only 12 contributions. The correction is to report and evaluate the cluster count itself, and not to interpret the number of rows as the number of independent units for clustered inference.

A second mistake is to say that clustering at a different level “changes the DiD effect.” Holding the fitted OLS regression and observations fixed, changing the variance estimator changes uncertainty estimates, not the OLS interaction coefficient. If the point estimate changes, then something else has changed too, such as the weights, sample, or regression specification. Neither a small standard error nor a large cluster count repairs a failure of parallel trends; those are distinct inferential and causal questions.

Key takeaway: Cluster-robust DiD inference aggregates regression scores within clusters and estimates uncertainty across independent clusters. The cluster level must reflect plausible dependence, the number of independent clusters governs the reliability of the approximation, and clustering itself does not define the causal estimand.

Check Your Understanding

Answer each question by distinguishing the regression estimate, the dependence assumptions, and the causal interpretation.

  1. In a DiD regression with 2,000 individuals nested in 10 states, why is the number 2,000 not the relevant count for the cluster-level approximation if errors may be dependent within state?
  2. What part of the cluster-robust sandwich allows arbitrary error correlation within a cluster?
  3. A researcher changes only the clustering variable while keeping the regression fixed. Which quantities may change, and which OLS quantity does not?
  4. In the unequal-size example, why did the individual-weighted contrast differ from the equal-cluster contrast?
  5. Does a cluster-robust standard error establish parallel trends? Explain why or why not.
Clinical Biostats