*************************************************************
*
* Table B2: Details on estimating methods: Spatial clustering
*
*************************************************************

global program "TableB2"

cap log close
cap log using "$log_files/log_$program$today", replace

clear all
set mem 2g
set more off
set matsize 11000
set scheme s1color

***************************
***** 1. PREPARE DATA *****
***************************

// Get data

use "$data/Data_County", clear

// Controls definition

global geog 		"railroad_dens ldist_capital ldist_largest county_lat county_lon" 
global demo 		"urban_dum perc_pop_dstr lpop_total perc_white perc_male21 perc_below21 perc_native perc_literate newsp_circ_elig" 
global demo_cont	"perc_pop_dstr lpop_total perc_white perc_male21 perc_below21 perc_native perc_literate newsp_circ_elig" 
global econ 		"perc_farm perc_manuf dlog_avbasket ldist_silver ldist_gold" 
global econ_cont	"perc_farm perc_manuf dlog_avbasket ldist_silver ldist_gold" 
global econ_p 		"perc_farm perc_manuf log_avbasket ldist_silver ldist_gold" 
global vote 		"lag_margin_pres_ttl" 			
global demo_add		"urban_dum lpop_total larea perc_white perc_below21 perc_native perc_literate newsp_circ_elig pop_male2144 pop_male4564 pop_male6599 perc_catholic perc_black"
  
// The sample

reghdfe dempop_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl	$demo $geog $econ if year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state) // keepsing
g sample=1*(e(sample))

keep if sample==1

****************************
***** 2. TABLE RESULTS *****
****************************

// Baseline (85km)

eststo clear
acreg dempop_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl 		$demo $geog $econ if year==1896 & state_speech==1 & railroad_dum==1, ///
	spatial latitude(county_lat) longitude(county_lon) id(county) time(year) lag(100000) dist(85) lagdist(0) pfe1(state) 
estimates store cs_binary_con_se_baseline
qui sum dempop_pres_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
qui sum speech if e(sample)
scalar mean_x=r(mean)
estadd scalar mean_x

// Spatial clustering 20 to 160km

forvalues i=20(20)160{
    
acreg dempop_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl 		$demo $geog $econ if year==1896 & state_speech==1 & railroad_dum==1, ///
	spatial latitude(county_lat) longitude(county_lon) id(county) time(year) lag(100000) dist(`i') lagdist(0) pfe1(state) 
	estimates store cs_binary_con_se_d`i'
	qui sum dempop_pres_ttl if e(sample)
	scalar mean_y=r(mean)
	estadd scalar mean_y
	qui sum speech if e(sample)
	scalar mean_x=r(mean)
	estadd scalar mean_x
	local se_dep_`i' = _se[speech]
}

***************************
***** 3. EXPORT TABLE *****
***************************

estout cs_binary_con_se_baseline cs_binary_con_se_d*  ///
	using "$tables/$program.tex", replace style(tex) type label ///
	cells(b(fmt(%9.3f) star) se(par fmt(%9.3f)))  ///
	stats(mean_y mean_x N, lab("Mean dep. variable" "Mean exp. variable" "Observations") fmt(2 2 %9.0fc)) ///
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	keep(speech lag_dem_pres_ttl lag_pop_pres_ttl) ///  
	order(speech lag_dem_pres_ttl lag_pop_pres_ttl) ///
	varlabels(speech "Speech (binary)" lag_dem_pres_ttl "[0.5em] Dem (\%) in 1892" lag_pop_pres_ttl "[0.5em] Pop (\%) in 1892") ///
	title(Details on estimating methods: Spatial clustering) ///
	indicate("\\ Demographic controls = lpop_total" ///	
	"Geographic controls = county_lat" ///	
	"Economic controls = perc_farm" ///		"State FE = 0.state" ///	
	, labels(Yes )) ///
	prehead("\begin{table}[h!]" ///
	"\begin{center}" ///
	"\begin{threeparttable}" ///
	"\begin{scriptsize}" ///
	"\caption{@title}\label{$program}" ///
	"\begin{tabular*}{\columnwidth}{@{\extracolsep{\stretch{1}}}l*{@M}{c}@{}}" ///
	"\hline" "\hline" ///
	"&\multicolumn{1}{c}{(1)} &\multicolumn{1}{c}{(2)}  		&\multicolumn{1}{c}{(3)} 		&\multicolumn{1}{c}{(4)} 		&\multicolumn{1}{c}{(5)} &\multicolumn{1}{c}{(6)} &\multicolumn{1}{c}{(7)} &\multicolumn{1}{c}{(8)}  &\multicolumn{1}{c}{(9)} \\" ///	  
	"\multicolumn{1}{l}{\textit{Distance Cutoff (in km's)}} 	&\multicolumn{1}{c}{85} 	&\multicolumn{1}{c}{20}  	&\multicolumn{1}{c}{40} 	&\multicolumn{1}{c}{60} 	&\multicolumn{1}{c}{80} &\multicolumn{1}{c}{100} &\multicolumn{1}{c}{120} &\multicolumn{1}{c}{140} &\multicolumn{1}{c}{160}\\" ///
	"\hline") ///
	posthead("\\") ///
	prefoot("\hline") ///
	postfoot("\hline" "\hline" ///
	"\end{tabular*}" /// 
	"\end{scriptsize}" ///
	"\begin{tablenotes}[normal,flushleft]" ///
	"\begin{scriptsize}" ///
	"\item \emph{Notes}: OLS regressions. The unit of observation is a county. " ///
	"Demographic controls: Urban (binary), District population (\%),  Log population, White (\%), Male above 21 (\%), Native (\%). " ///
	"Geographic controls: Railroad density (km per sq. km), Distance to state capital (log), Distance to state largest city (log), Latitude, Longitude. " ///
	"Economic controls: Farmers (\%), Manufacture workers (\%), Number of newspapers over eligible voters, Crop value change (log), (log) Distances to silver and gold mines. " ///
	"All regressions include state fixed-effects. " ///
	"Standard errors adjusted for spatial correlation in parentheses." /// 
	"* significant at 10\%; ** at 5\%; *** at 1\%. " ///	
	"\end{scriptsize}" ///
	"\end{tablenotes}" ///
	"\end{threeparttable} " ///
	"\end{center}" ///
	"\end{table}") //	

cap log close
