set more off
do "do files - REPLICATION/0_gen three year averages.do"
est clear

***************************
*ROBUSTNESS TABLE (TABLE 3)
***************************


*column1
eststo:reg restrictions_all_racial_max restrictions2_racial_max perc_black2 perc_landless2_new urb2 density2 proprietary charter yearcleanup $colonydum $colonydum_trend, robust
estadd ysumm

*column2
eststo:ivreg29 restrictions_all_racial_max restrictions2_racial_max (perc_black2=rice_wheat_40_low) perc_landless2_new urb2 density2 proprietary charter yearcleanup $colonydum $colonydum_trend, robust savefirst savefprefix (first) first
estadd ysumm
*save the first state results in the e()-data of the main equation, so that they are printed in the table    
est store tmp
est restore firstperc_black2
scalar fs_r2 = e(r2)
scalar fs_b = _b[rice_wheat_40_low]
scalar fs_se = _se[rice_wheat_40_low]
est restore tmp
estadd scalar fs_r2=fs_r2
estadd scalar fs_b=fs_b
estadd local fs_se = "("+string(fs_se,"%7.3f")+")"
est store est2

*column3
eststo:reg restrictions_all_racial_max restrictions2_racial_max perc_black2 perc_landless2_new urb2 density2 proprietary charter yearcleanup $colonydum north_trend middle_trend, robust
estadd ysumm

*column4
eststo:ivreg29 restrictions_all_racial_max restrictions2_racial_max (perc_black2=rice_wheat_40_low) perc_landless2_new urb2 density2 proprietary charter yearcleanup $colonydum north_trend middle_trend, robust savefirst savefprefix (first) first
estadd ysumm
*save the first state results in the e()-data of the main equation, so that they are printed in the table    
est store tmp
est restore firstperc_black2
scalar fs_r2 = e(r2)
scalar fs_b = _b[rice_wheat_40_low]
scalar fs_se = _se[rice_wheat_40_low]
est restore tmp
estadd scalar fs_r2=fs_r2
estadd scalar fs_b=fs_b
estadd local fs_se = "("+string(fs_se,"%7.3f")+")"
est store est4


*Read in the data again, need the entire dataset
do "do files/0_gen annual data.do"
drop if colony=="GA"
drop if colony=="ME"
drop if drop_observations==1

*column 5
eststo: cgmwildboot restrictions_all_racial_max restrictions2_racial_max perc_black2 perc_landless2_new urb2 density2 proprietary yearcleanup colony_d1  colony_d2 colony_d4  colony_d5 colony_d7 colony_d8  colony_d9  colony_d10  colony_d11  colony_d12 colony_d13, cluster(colony_num)bootcluster(colony_num) reps(400) null (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0   )

*column 6
xtabond restrictions_all_racial_max restrictions2_racial_max  perc_black2 perc_landless2_new   urb2 density2 proprietary charter yearcleanup , maxldep(1) maxlags(1) vce(robust) lags(1)
////////////////////////WARNING!!!!!////////////////// THE FOLLOWING LINES MAKE SURE THAT THE COEFFICIENT FOR L.restrictions_all_racial_max
////////////////////////////////////////////////////// IS WRITTEN INTO THE SAME LINE AS THE COEFFICIENTS FOR  restrictions2_racial_max
////////////////////////////////////////////////////// BUT IF THERE SHOULD BE ANY CHANGES MADE TO THE COMMAND above (XTABOND) THIS IS LIKELY TO
////////////////////////////////////////////////////// LEAD TO UNDESIRED (AND UNPREDICTABLE) CONFUSIONS OF ROWS AND COEFFICIENTS. IN THAT CASE, PLEASE
////////////////////////////////////////////////////// REMOVE THE NEX 6 LINES AND EDIT THE tex-FILE MANUALLY AFTER RUNNING THIS do-FILE, 
////////////////////////////////////////////////////// TO GET THE DESIRED RESULTS
matrix bb = e(b) //save coefficents
matrix VV = e(V) //and  variances in new matrices. Rename the columns:
matrix colnames bb = restrictions2_racial_max perc_black2 perc_landless2_new urb2 density2 proprietary charter yearcleanup _cons
matrix colnames VV = restrictions2_racial_max perc_black2 perc_landless2_new urb2 density2 proprietary charter yearcleanup _cons
matrix rownames VV = restrictions2_racial_max perc_black2 perc_landless2_new urb2 density2 proprietary charter yearcleanup _cons
sum restrictions_all_racial_max if e(sample)
scalar eymean=r(mean)
scalar eN=r(N)
eststo: ereturn post bb VV //store them as a new estimation result
//////////////////////////////////////////////////////  THATS THE END OF THE CRITICAL PART
estadd scalar ymean = eymean
estadd scalar N = eN



//export all the saved estimates to a TeX table. See the comments for table2.do for detailed explanations
esttab 	using "$table_folder/robust1.tex", label replace se star(* 0.1 ** 0.05 *** 0.01) booktabs  nonotes ///		
		mtitles("\mtitle{OLS\\Colony FEs and colony trends}" "\mtitle{2SLS\\Colony FEs and colony trends}" "\mtitle{OLS\\Colony FEs and reg. trends}" "\mtitle{2SLS\\Colony FEs and reg. trends}" "\mtitle{Wild bootstrap}" "\mtitle{Arellano Bond}" "\mtitle{OLS\\Unweighted Suffrage}" "\mtitle{2SLS\\Unweighted Suffrage}") ///
		end("\rowstyle{\relax}\\") ///
		alignment(D..{-1})   /// 	
		drop(_cons ) ///		
		indicate("Year control=yearcleanup middle_trend north_trend" "Colony fixed effects = colony_d*" "Additional controls = proprietary charter", labels("\checkmark"  "")) ///
		order(restrictions2_racial_max  perc_black2 perc_landless2_new urb2 density2 yearcleanup  $colonydum proprietary charter ) ///
		scalars("ymean Mean suffrage" "r2 \(R^2\)" "fs_b \midrule{\bfseries First-Stage Results}\\Instrument Coefficient" "fs_se ~" "fs_r2 1\(^{st}\) stage \(R^2\)") ///
		substitute("(7.67e-20)" "(0.000)")
		/// somehow these extremely small values are shown as exponents, I replace them with 0.000