<p>Have you seen <a href="http://faculty.econ.ucdavis.edu/faculty/dlmiller/statafiles/" rel="nofollow noreferrer">http://faculty.econ.ucdavis.edu/faculty/dlmiller/statafiles/</a> ?</p>

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

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

<pre><code>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
</code></pre>

<p>This code has yet to be verified manually.</p>

<p><a href="https://i.sstatic.net/SBlq0.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/SBlq0.png" alt="enter image description here"></a></p>

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

CC BY-SA 3.0
2016-10-18T13:42:06.200Z
I found that code, but I need a code specific for panel frameworks (in other words, in the field of xtreg command)
user98139
CC BY-SA 3.0
2016-10-18T14:39:15.540Z
@user98139 please see the edit.
chan1142
CC BY-SA 3.0
2016-10-18T18:35:51.617Z
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.
user98139
CC BY-SA 3.0
2016-10-19T03:39:09.690Z
@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.
chan1142
CC BY-SA 3.0
2016-10-19T07:40:47.300Z
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
user98139
CC BY-SA 3.0
2016-10-19T23:44:20.033Z
`ereturn ...` things just display the results in the standard Stata format. I made `b` only for that purpose.
chan1142
CC BY-SA 3.0
2016-10-18T13:26:19.240Z
2016-10-22T15:14:26.910Z
2016-10-22T15:14:26.910Z
chan1142
<p>It might be too late, but I think this user written command is what you need:</p>

<p><a href="http://scorreia.com/software/reghdfe/" rel="nofollow noreferrer">http://scorreia.com/software/reghdfe/</a></p>

<p>It supports multiple fixed effects and multi-way clustering.</p>

<pre><code>ssc install reghdfe
reghdfe y x, absorb(fe1 fe2) vce(cluster cl1 cl2)
</code></pre>

CC BY-SA 3.0
2016-12-17T13:29:02.843Z
2016-12-18T09:43:45.313Z
2016-12-18T09:43:45.313Z
cheesecake
<p>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.</p>

<econometrics><applied-econometrics><stata>
Two-way clustering in Stata
user98139
# Two-way clustering in Stata

(Asked by: username_0 on 2016-10-18)

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.

**Tags:** econometrics, applied-econometrics, stata


---

## 2 answers

### Answer by username_1 on 2016-10-18. Score: 2 | Accepted answer

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.

[![enter image description here](https://i.sstatic.net/SBlq0.png)](https://i.sstatic.net/SBlq0.png)

**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.

> **Comments:**
> **username_0**: I found that code, but I need a code specific for panel frameworks (in other words, in the field of xtreg command)  
> **username_1**: @username_0 please see the edit.  
> **username_0**: 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.  
> **username_1**: @username_0 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.  
> **username_0**: 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  
> **username_1**: `ereturn ...` things just display the results in the standard Stata format. I made `b` only for that purpose.  

---

### Answer by username_2 on 2016-12-17. Score: 1

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)

```


---

