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

global program "FigureB2"

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. FIGURE 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 FIGURE *****
****************************

coefplot (cs_binary_con_se_baseline, mcolor(navy) ciopts(recast(rcap)  color(navy))) cs_binary_con_se_d*, keep(speech) vertical levels(95) /// 
	mcolor(gs8) ciopts(recast(rcap) lc(gs10)) leg(off) 	graphregion(color(white)) msymbol(S) msize(small)  mfcolor(white) 	pstyle(p3) ///
	yline(0, lpattern(dash)) yline(0, lpattern(dash))  yline(1, lpattern(dash) lcolor(gs15)) yline(2, lpattern(dash) lcolor(gs15))  /// 
	ylabel(-0.5(0.5)2, angle(0)  format(%9.1f))  /// 		
	xlabel(.6  `" "85" "(Baseline)"' .7  `"20"'  0.8  `"40"'  0.9  `"60"' 1  `"80"' 1.1  `"100"' 1.2  `"120"' 1.3  `"140"' 1.4  `"160"' , labsize(small)) ///		
	ytitle("Coefficient (95% C.I.)") xtitle("Clustering radius (km)") 
graph export "$figures/$program.pdf", replace

cap log close
