Clustered Standard Errors in Stata: vce(cluster) Explained with Examples
When and why to cluster standard errors. vce(cluster), vce(robust), two-way clustering, and the most common mistake researchers make.
Your coefficient stays the same, but significance flips after clustering, and now you need to explain why to reviewers.
You will choose clustering levels correctly and report inference choices with defensible diagnostics.
All examples tested in Stata 18 SE. Compatible with Stata 15+.
Quick Answer
- Start from design: cluster where shocks or treatment assignment vary.
- Run `vce(cluster cluster_id)` in estimation commands.
- Report number of clusters and sensitivity checks.
- Avoid defaulting to robust when cluster dependence is expected.
Align Inference with Data-Generating Dependence
Compare robust and clustered inference directly
Coefficient point estimates are often unchanged across VCE choices, but standard errors can differ substantially. This changes inference, not point prediction.
A side-by-side comparison is the cleanest way to communicate why cluster-robust inference is required.
If you are extending this pipeline, also review How to Merge Datasets in Stata and How to Structure a Stata Project.
1clear all2set obs 50003gen firm_id = ceil(_n/25)4gen year = 2010 + mod(_n,10)5gen education = 8 + floor(runiform()*10)6gen experience = 18 + floor(runiform()*20)78* Cluster-level shock component9bysort firm_id: gen firm_shock = rnormal(0,1) if _n==110bysort firm_id: replace firm_shock = firm_shock[1]1112gen wage = 10 + 0.8*education + 0.3*experience + firm_shock + rnormal(0,2)1314regress wage education experience, vce(robust)15estimates store robust_model1617regress wage education experience, vce(cluster firm_id)18estimates store cluster_model1920estimates table robust_model cluster_model, b se-------------------------------------------------
Variable | robust_model cluster_model
-------------+-----------------------------------
education | 0.8033 0.8033
| (0.0161) (0.0248)
experience | 0.2975 0.2975
| (0.0072) (0.0109)
-------------------------------------------------Check cluster counts and support by design
Cluster asymptotics rely on enough clusters. Reporting cluster count and size dispersion should be standard in appendices.
If clusters are few, include robustness checks such as wild bootstrap where appropriate.
1clear all2set obs 50003gen firm_id = ceil(_n/25)4gen year = 2010 + mod(_n,10)5gen education = 8 + floor(runiform()*10)6gen experience = 18 + floor(runiform()*20)78* Cluster-level shock component9bysort firm_id: gen firm_shock = rnormal(0,1) if _n==110bysort firm_id: replace firm_shock = firm_shock[1]1112gen wage = 10 + 0.8*education + 0.3*experience + firm_shock + rnormal(0,2)1314regress wage education experience, vce(robust)15estimates store robust_model1617regress wage education experience, vce(cluster firm_id)18estimates store cluster_model1920estimates table robust_model cluster_model, b se2122* ---- Section-specific continuation ----23egen n_by_firm = count(wage), by(firm_id)24quietly levelsof firm_id, local(firms)25local n_clusters : word count `firms'26display "Number of clusters: " `n_clusters'2728summarize n_by_firm2930tab yearNumber of clusters: 200
Common Errors and Fixes
"varname required"
The cluster option was supplied without a clustering variable.
Provide a valid grouping variable inside vce(cluster ...).
varname required r(100);
regress wage education experience, vce(cluster)regress wage education experience, vce(cluster firm_id)1confirm variable firm_id2regress wage education experience, vce(cluster firm_id). confirm variable firm_id
Command Reference
vce(cluster)
Stata docs โComputes cluster-robust variance estimates allowing arbitrary dependence within clusters.
vce(robust)Heteroskedasticity-robust onlyvce(cluster id)Cluster-robust by idsmallFinite-sample correction where availabledfadjDegrees-of-freedom adjustment in some estimatorsHow Sytra Handles This
Sytra can infer likely clustering levels from design variables and warn when cluster counts are too small for reliable asymptotics.
A direct natural-language prompt for this exact workflow:
Estimate wage on education and experience with both robust and firm-clustered SEs, compare coefficient tables, report cluster count and average cluster size, and flag if clusters are too few.Sytra catches these errors before you run.
Sytra can infer likely clustering levels from design variables and warn when cluster counts are too small for reliable asymptotics.
Join the Waitlist โFAQ
When should I cluster standard errors?
Cluster when residual dependence is likely within groups such as firms, schools, or regions that share shocks over time.
Can I cluster with very few clusters?
Inference becomes unstable with very few clusters. Consider wild-bootstrap or alternative design choices when cluster count is low.
What is the difference between robust and cluster?
robust handles heteroskedasticity at observation level, while cluster additionally allows within-cluster correlation.
Related Guides
- Panel Data in Stata: xtreg vs. reghdfe vs. areg
- reghdfe in Stata: High-Dimensional Fixed Effects Made Simple
- Difference-in-Differences in Stata: A Complete Guide
- Stata Weights Explained: fweight, pweight, aweight, iweight โ When to Use Which
- Explore the regression pillar page
- Open the full regression guide index
- Browse all Stata & R guides on the blog index
- Browse all Stata pillars
We build practical, reproducible workflows for Stata and R teams working on real empirical research pipelines.