Two-way clustering in Stata I have a panel dataset and I would like to estimate a linear equation in a fixed effects framework. My question is: how should I implement a two-way clustering? Stata syntax and/or .ado file necessary would be greatly appreciated.
Have you seen http://faculty.econ.ucdavis.edu/faculty/dlmiller/statafiles/ ?

I see some entries there such as Multi-way clustering with OLS and Code for “Robust inference with Multi-way Clustering”.

EDIT: At least we can calculate the two-way clustered covariance matrix (note the nonest option), I think, though I can't verify it for now. See the following.

set more off
clear all
local n 50
local T 50

set obs `=`n'*`T''

gen id = floor((_n-1)/`T')+1
by id, sort: gen year = 1970+_n
xtset id year

set seed 1

gen x = rnormal()
gen y = 1+0.5*x+rnormal()

xtreg y x, re vce(cl id)
mat b = e(b)
mat V1 = e(V)
xtreg y x, re vce(cl year) nonest
mat V2 = e(V)
xtreg y x, re vce(cl id year) nonest
mat V12 = e(V)
mat V = V1+V2-V12
mat l V
ereturn post b V
ereturn display

set more on


This code has yet to be verified manually.



EDIT 2: Sorry for keeping editing. Just found that Stata's reg (for pooled OLS) does not allow for clustering by multiple variables such as vce(cluster id year). We should use vce(r) or just r. However, it seems that xtreg does (usually requiring nonest), though I counldn't find documentation.