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.

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.

• I found that code, but I need a code specific for panel frameworks (in other words, in the field of xtreg command) Oct 18 '16 at 13:42
• @user98139 please see the edit. Oct 18 '16 at 14:39
• thank you for the edit! Calculating the three matrices and add the two "single" ones while subtracting the "interaction" one is a solution that I also found surfing the web. The problem is that I am not an experienced Stata user and don't know how to "say to the software" to use this new matrix in order to calculate the standard errors. Does your code do this? If so, can you tell me precisely the lines that do so. So that I could try to replicate it in my panel and solve my problem. Oct 18 '16 at 18:35
• @user98139 I think so. You can modify the xtreg ... lines as you wish. Before the first xtreg ... line is the data generating part for this exercise, which you can ignore. Good luck. Oct 19 '16 at 3:39
• So let me see if I understand your process. In the first two xtreg you compute the two fixed effects clustering with respect to both id (first) and year (second) and you save the robust matrices as, respectively, V1 and V2. Whit b=e(b) what exactly do you want to get and why? In the tird xtreg you compute the "interaction" robust matrix and you save it as V12. Then you compute the robust matrix for the two-way clustering and you name it V. Finally, what do you do in this lines:mat l V ereturn post b V ereturn display set more on Oct 19 '16 at 7:40

It might be too late, but I think this user written command is what you need:

http://scorreia.com/software/reghdfe/

It supports multiple fixed effects and multi-way clustering.

ssc install reghdfe
reghdfe y x, absorb(fe1 fe2) vce(cluster cl1 cl2)