AI Agents & Econometric Coding

Linear regression inference · OLS

Cluster-Robust Standard Errors

Clustered covariance estimation allows arbitrary error dependence inside declared groups.

Independence between groups

Repeated observations from one firm, school, or person can share shocks. Cluster-robust inference treats whole groups as independent units (Cameron and Miller, 2015).

For cluster $g$, collect its score as $s_g=X_g'\hat u_g$. The covariance meat is a sum of cluster score outer products:

$$ \widehat V=(X'X)^{-1}\left(\sum_{g=1}^G s_gs_g'\right)(X'X)^{-1}. $$

Why within-cluster cancellation matters

Consider one slope and two observations per cluster.

ClusterObservation scoresCluster sumSquared cluster sum
A$2,2$416
B$3,-3$00
C$-1,-2$-39

Observation-level squaring would miss the covariance between scores inside each group. Cluster B illustrates cancellation after scores are aggregated.

Assumptions and implementation

Clusters must be independent across groups under the standard large-$G$ argument. The clustering dimension should follow the treatment assignment or error dependence process. Few clusters require additional care.

Software differs in leverage corrections, degrees of freedom, and whether absorbed effects count toward the parameter total. Observations share dependence links inside groups, with no assumed links across groups.

What clustered uncertainty changes

Clustering leaves the OLS coefficient and residuals unchanged. It changes how score contributions enter the covariance matrix. Observations inside a cluster may move together, so the cluster score is summed before its outer product is formed. Cancellation within a cluster can lower a variance estimate, while aligned residuals can raise it. The number of independent clusters governs the effective information for inference.

Report the coefficient, clustered standard error, confidence interval, cluster variable, and cluster count. Name any finite-sample correction. A small standard error does not establish a causal effect. The regression design and identifying assumptions determine that interpretation.

Choosing clusters and checking the calculation

Choose the cluster level from the dependence or assignment process. If treatment is assigned to schools, clustering individual pupils only by classroom can omit school-level dependence. Panel regressions commonly cluster by unit because errors can correlate across time within each unit. Multiway clustering can address separate dependence along two dimensions when the sample supports it.

Verify that each observation has one valid cluster identifier. Compare the software output with cluster score sums and the declared correction factor. With few clusters, asymptotic normal approximations can be inaccurate. Use an appropriate small-sample reference distribution or a wild cluster bootstrap, and report the procedure. Highly unbalanced cluster sizes and high-leverage clusters deserve separate inspection. Removing a large cluster as a casual robustness check changes the target population, so explain any leave-one-cluster analysis. (Cameron and Miller, 2015)

Further reading

Cameron and Miller give an applied guide to cluster-robust inference (Cameron and Miller, 2015). Liang and Zeger develop the estimating-equation foundation (Liang and Zeger, 1986).

Source status

Sources and further reading

  1. A. Colin Cameron, Douglas L. Miller. 2015. “A Practitioner's Guide to Cluster-Robust Inference.” Journal of Human Resources 50(2): 317--372. doi:10.3368/jhr.50.2.317.
  2. Kung-Yee Liang, Scott L. Zeger. 1986. “Longitudinal Data Analysis Using Generalized Linear Models.” Biometrika 73(1): 13--22. doi:10.1093/biomet/73.1.13.

Related reading

About this benchmark task

Status
In the benchmark
Identifier
cluster_ols
Family
OLS
Software
Stata, R, Python
Source
Benchmark task set

Task statement

Estimate y_panel on x1 and x2 with an intercept. Use cluster-robust standard errors clustered on cluster. Report rows const, x1, and x2.

Required output

const, x1, x2

Open every recorded run for this task.