//program * cluster2boot.ado ---- written by Mitchell Petersen -- March 2006 --- modified by Ian Gow, Gaizka Ormazabal and Dan Taylor --- July 2009 * Program calculates boostrapped clustered standard errors in both a firm and time dimension * as described by Cameron, Gelbach, and Miller, 2007, "Bootstrap-Based Improvements for Inference with * Clustered Errors" #delimit ; program define cluster2boot; syntax [varlist] [in] [if], fcluster(varname) tcluster(varname) nboot(integer); tokenize `varlist'; local depv `"`1'"'; local nreps `nboot'; * ---------------------------------------------------------------- ; * ----- Regression Clustering by First Variable (e.g. Firm) ------ ; * ---------------------------------------------------------------- ; quietly bootstrap "reg `varlist' `in' `if'" _b, reps(`nreps') cluster(`fcluster'); matrix vcf = e(V); local nfcluster=e(N_clust); * ---------------------------------------------------------------- ; * ----- Regression Clustering by Second Variable (e.g. Time) ----- ; * ---------------------------------------------------------------- ; quietly bootstrap "reg `varlist' `in' `if'" _b, reps(`nreps') cluster(`tcluster'); matrix vct = e(V); local ntcluster=e(N_clust); * ---------------------------------------------------------------- ; * --------------- Regression with No Clustering ---------------- ; * ---------------------------------------------------------------- ; capture confirm string variable `fcluster'; if !_rc {; gen bc1 = `fcluster'; /* string variable */ }; else {; gen bc1 = string(`fcluster'); /* numeric */ }; capture confirm string variable `tcluster'; if !_rc {; gen bc2 = `tcluster'; /* string variable */ }; else {; gen bc2 = string(`tcluster'); /* numeric */ }; gen bc3 = bc1 + "_" + bc2; quietly bootstrap "reg `varlist' `in' `if'" _b, reps(`nreps') cluster(bc3); drop bc1 bc2 bc3; matrix coef = e(b); matrix vcb = e(V); matrix vc = vcf+vct-vcb; * Get degrees of freedom and other basic statistics; quietly reg `varlist' `in' `if'; local nparm = e(df_m)+1; * ---------------------------------------------------------------- ; * ----------------- Print out Regression Results ----------------- ; * ---------------------------------------------------------------- ; tokenize `varlist'; /* this puts varlist in to the macros `1' `2' etc */ macro shift; /* drops first arguement (dep var) and shifts the rest up one */ dis " "; dis "Linear regression with clustered SEs" _column (52) "Number of obs = " %7.0f e(N); dis _column(52) "F(" %3.0f e(df_m) "," %6.0f e(df_r) ") =" %8.2f e(F); dis _column(52) "Prob > F =" %8.4f 1-F(e(df_m),e(df_r),e(F)); dis "Number of clusters (`fcluster') = " _column(31) %5.0f $_nfcluster _column(52) "R-squared =" %8.4f e(r2); dis "Number of clusters (`tcluster') = " _column(31) %5.0f $_ntcluster _column(52) "Root MSE =" %8.4f e(rmse); dis " "; dis _column(10) " Beta" _column(24) " Std Error" _column(38) " T-stat" _column(52) " p-value"; dis _column(10) " -----------" _column(24) " ------------" _column(38) " -----------" _column(52) " -----------"; local i = 1; * loop as long as macro `1' is not empty; while "`1'" ~= "" {; dis "`1'" _column(10) %9.5f coef[1,`i'] _column(24) %9.5f sqrt(vc[`i',`i']) _column(38) %9.4f coef[1,`i']/sqrt(vc[`i',`i']) _column(52) %9.4f 2*ttail(e(N)-1,abs(coef[1,`i']/sqrt(vc[`i',`i']))); macro shift; local i = `i' + 1; }; dis "Constant" _column(10) %9.5f coef[1,`nparm'] _column(24) %9.5f sqrt(vc[`nparm',`nparm']) _column(38) %9.4f coef[1,`nparm']/sqrt(vc[`nparm',`nparm']) _column(52) %9.4f 2*ttail(e(N)-1,abs(coef[1,`nparm']/sqrt(vc[`nparm',`nparm']))); dis " "; dis " SE clustered by " "`fcluster'" " and " "`tcluster'"; dis " "; end;