********************************************************************************************************************************* ********************************************************************************************************************************* * ACREG: Program for computing corrected standard errors for Arbitrary Clustering * * Copyright: F. Colella, R. Lalive, S.O. Sakalli, M. Thoenig * * * * Beta Version, please do not circulate - This Version: June 2019 * * * * When using this program, please refer to our companion paper * * * * "Inference with Arbitrary Clustering", F. Colella, R. Lalive, S.O. Sakalli, M. Thoenig, 2018, mimeo University of Lausanne * * * ********************************************************************************************************************************* ********************************************************************************************************************************* ********************************************************************************************* * Examples * ********************************************************************************************* *** Additional Packages required to run this file *** - estout *** - ivreg2 *** - ranktest * Install this file required packages foreach package in estout ivreg2 ranktest { capture which `package' if _rc==111 ssc install `package' } net install acreg, from(https://acregstata.weebly.com/uploads/2/9/1/6/29167217) replace ********************************************************************************************* clear all set matsize 11000 * Install the acreg required packages acregpackcheck * Open Dataset use https://acregstata.weebly.com/uploads/2/9/1/6/29167217/acregfakedata.dta , clear scalar scope = scope ************************************************************************** ************************************************************************** * PART 1 - NO ARBITRARY CLUSTERING CORRECTION ************************************************************************** ************************************************************************** set seed 1 ************************************************************************** *Weights (comparison with IVREG2) xi i.time xtset id time * ADDITIONAL WEIGHTS gen popweights_temp = rnormal(100,20) gen double popweights =round(popweights_temp,1) drop popweights_temp sum popweights eststo: ivreg2 Y_it X1_it (Z_it=IV_it) , robust eststo: acreg Y_it X1_it (Z_it=IV_it) eststo: ivreg2 Y_it X1_it (Z_it=IV_it) [pw=popweights] , robust eststo: acreg Y_it X1_it (Z_it=IV_it) [pw=popweights] eststo: ivreg2 Y_it X1_it (Z_it=IV_it) [fw=popweights] , robust eststo: acreg Y_it X1_it (Z_it=IV_it) [fw=popweights] esttab, cells(b se) keep(Z_it X1_it) eststo clear ************************************************************************** *Multiway clutering (comparison with IVREG2) * IVREG2 CLUSTER TIME eststo: ivreg2 Y_it X1_it (Z_it=IV_it) , cluster(time) robust * ACREG CLUSTER TIME eststo: acreg Y_it X1_it (Z_it=IV_it) , cluster(time) * IVREG2 CLUSTER ID TIME eststo: ivreg2 Y_it X1_it (Z_it=IV_it) , cluster(id time) robust * ACREG CLUSTER TIME eststo: acreg Y_it X1_it (Z_it=IV_it) , cluster(id time) esttab, cells(b se) keep(_cons X1_it) eststo clear ************************************************************************** * Absorbing FE (comparison with IVREG2) * IVREG2 NO FE eststo: ivreg2 Y_it X1_it (Z_it=IV_it) , robust * ACREG NO FE eststo: acreg Y_it X1_it (Z_it=IV_it) * IVREG2 FEID FETIME MANUAL eststo: ivreg2 Y_it X1_it (Z_it=IV_it) i.id i.time , robust * ACREG FEID FETIME eststo: acreg Y_it X1_it (Z_it=IV_it) , pfe1(id) pfe2(time) *with the corrected r2 acreg Y_it X1_it (Z_it=IV_it) , pfe1(id) pfe2(time) correctr2 * IVREG2 FEID MANUAL eststo: ivreg2 Y_it X1_it (Z_it=IV_it) i.id , robust * ACREG FEID eststo: acreg Y_it X1_it (Z_it=IV_it) , pfe1(id) *with the corrected r2 acreg Y_it X1_it (Z_it=IV_it) , pfe1(id) correctr2 esttab, cells(b se) keep(Z_it X1_it) eststo clear ************************************************************************** *if (comparison with IVREG2) * IVREG2 if eststo: ivreg2 Y_it X1_it (Z_it=IV_it) if X1_it>5, robust * ACREG if local sco=scope eststo: acreg Y_it X1_it (Z_it=IV_it) if X1_it>5 esttab, cells(b se) keep(Z_it X1_it) eststo clear ************************************************************************** *factor variables (comparison with IVREG2) gen dumX=0 replace dumX=1 if X1_it>3 replace dumX=2 if X1_it>10 gen dumZ=0 replace dumZ=1 if Z_it>3 replace dumZ=2 if Z_it>20 gen dumIV=0 replace dumIV=1 if IV_it>3 replace dumIV=2 if IV_it>5 * IVREG2 factor variables eststo: ivreg2 Y_it i.dumX (i.dumZ=i.dumIV), robust * ACREG factor variables eststo: acreg Y_it i.dumX (i.dumZ=i.dumIV) esttab, cells(b se) eststo clear ************************************************************************** *Clustering ID and HAC * Cluster ID *** IVREG2 eststo: ivreg2 Y_it X1_it (Z_it=IV_it) , robust cluster(id) *** ACREG eststo: acreg Y_it X1_it (Z_it=IV_it), id(id) time(time) lag(100) * HAC correction only *** IVREG2 eststo: ivreg2 Y_it X1_it (Z_it=IV_it) , bw(9) robust *** ACREG eststo: acreg Y_it X1_it (Z_it=IV_it), id(id) time(time) lag(8) hac esttab, cells(b se) keep(Z_it X1_it) eststo clear ************************************************************************** ************************************************************************** * PART 2 - ARBITRARY CLUSTERING CORRECTION (spatial and network) ************************************************************************** ************************************************************************** ************************************************************************** * SPATIAL ENVIRONMENT * No correction *** IVREG2 eststo: ivreg2 Y_it X1_it (Z_it=IV_it) , robust *** ACREG eststo: acreg Y_it X1_it (Z_it=IV_it) * Using Longitude and Latitude as input cross section local sco=scope eststo: acreg Y_it X1_it (Z_it=IV_it), /// latitude(lat) longitude(lon) dist( `sco' ) spatial * Using Longitude and Latitude as input panel no hac local sco=scope eststo: acreg Y_it X1_it (Z_it=IV_it), id(id) time(time) /// latitude(lat) longitude(lon) lag(10) dist( `sco' ) spatial * Using Longitude and Latitude as input panel with hac local sco=scope eststo: acreg Y_it X1_it (Z_it=IV_it), id(id) time(time) /// latitude(lat) longitude(lon) lag(10) dist( `sco' ) spatial hac * Using Longitude and Latitude as input with ak0 correction and hac local sco=scope eststo: acreg Y_it X1_it (Z_it=IV_it), id(id) time(time) /// latitude(lat) longitude(lon) lag(10) dist( `sco' ) spatial hac ak0 * Using the N cluster variables as distance matrix in a Spatial Environment with hac eststo: acreg Y_it X1_it (Z_it=IV_it), id(id) time(time) /// dist_mat(clus*) lag(10) dist(2) spatial hac esttab, cells(b se) keep(Z_it X1_it) eststo clear //NOTE: point estimates should not change! ************************************************************************** * NETWORK ENVIRONMENT * No correction *** IVREG2 eststo: ivreg2 Y_it X1_it (Z_it=IV_it) , robust *** ACREG eststo: acreg Y_it X1_it (Z_it=IV_it), id(id) time(time) /// links_mat(clus*) network lag(0) dist(0) * Cluster correction - Using the N cluster variables as a 1/0 links input matrix in panel no hac eststo: acreg Y_it X1_it (Z_it=IV_it), id(id) time(time) /// links_mat(clus*) lag(10) dist(2) network * Cluster correction - Using the N cluster variables as a 1/0 links input matrix in panel hac eststo: acreg Y_it X1_it (Z_it=IV_it), id(id) time(time) /// links_mat(clus*) lag(10) dist(2) network hac * Cluster correction - Using the N cluster variables as distance matrix in a Network Environment in panel hac eststo: acreg Y_it X1_it (Z_it=IV_it), id(id) time(time) /// dist_mat(clus*) lag(10) dist(2) network storeweights hac // Note, here we store the weights in a matrix format esttab, cells(b se) keep(Z_it X1_it) eststo clear //NOTE: point estimates should not change! ************************************************************************** * GENERAL ARBITRARY CLUSTERING Environment * No correction *** IVREG2 eststo: ivreg2 Y_it X1_it (Z_it=IV_it) , robust *** ACREG eststo: acreg Y_it X1_it (Z_it=IV_it) * Correction using weights stored * Using the weights variables we computed before as weights input matrix * Step a) generate weights variables from the stored weights (if the previous code saves the weights) capt drop weightsmat* svmat weightsmat * Step b) run the code eststo: acreg Y_it X1_it (Z_it=IV_it), weights(weightsmat*) esttab, cells(b se) keep(Z_it X1_it) eststo clear //NOTE: point estimates should not change!