Skip to content

Commit

Permalink
Fix bug wtih degrees-of-freedom
Browse files Browse the repository at this point in the history
If the VCE option was specified as `vce(cluster var1#var2)`, reghdfe was
comparing whether the fixed effects were nested within `var1` instead of
within `var1#var2`.

This reduced SEs and overstated statistical confidence.
  • Loading branch information
sergiocorreia committed Sep 25, 2017
1 parent 027f31a commit 44fc646
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/reghdfe_class.mata
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ class FixedEffects
if (cluster_data == .) {
if (strpos(clustervar, "#")) {
clustervar = base_clustervars[i_cluster]
clustervar = subinstr(clustervars[i_cluster], "#", " ", .)
F = factor(clustervar, sample, ., "", 0, 0, ., 0)
cluster_data = F.levels
F = Factor() // clear
Expand Down
21 changes: 21 additions & 0 deletions test/bug_cluster.do
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
noi cscript "reghdfe: prevent bug regression with cluster VCE" adofile reghdfe

* SEE:
* https://www.statalist.org/forums/forum/general-stata-discussion/general/1409974-reghdfe-4-x-standard-errors-and-categorical-variables

* Dataset
sysuse auto, clear
egen int turn_foreign = group(turn foreign)

* [TEST] Cluster

* 1. Run benchmark
reghdfe price weight, a(turn#trunk foreign) vce(cluster turn_foreign)
storedresults save benchmark e()

* 2. Run reghdfe with #
reghdfe price weight, a(turn#trunk foreign) vce(cluster turn#foreign)
storedresults compare benchmark e(), tol(1e-12) exclude(macro: clustvar1 clustvar cmdline)
storedresults drop benchmark

exit
2 changes: 2 additions & 0 deletions test/testall.do
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
run inconsistent-r2
run singleton-and-fweights

run bug_cluster

* -prune- corner cases
* run prune

Expand Down

0 comments on commit 44fc646

Please sign in to comment.