********************************************************************************
*
* Figure 2: Cross-sectional falsifications and alternative estimators
*
********************************************************************************

global program "Figure2"

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

// 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"

*****************************
***** 2. FIGURE RESULTS *****
*****************************

// Baseline for 1884-1892

use "$data/Data_County", clear

xtset county election
g lag2_dem_pres_ttl=l2.dem_pres_ttl
g lag2_pop_pres_ttl=l2.pop_pres_ttl

forvalues y=1884(4)1892{
	
	sum year if year==`y' 
	
	reghdfe dempop_pres_ttl speech lag_dem_pres_ttl 						$demo $geog $econ if year==`y' & state_speech==1 & railroad_dum==1, cluster(district) a(state) 
	estimates store false_binary_`y'

}

// Baseline for 1896

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)  
estimates store false_binary_1896

// Baseline for 1900 controlling for 1892

reghdfe dempop_pres_ttl speech lag2_dem_pres_ttl lag2_pop_pres_ttl			$demo $geog $econ if year==1900 & state_speech==1 & railroad_dum==1, cluster(district) a(state)  
estimates store false_binary_1900

// Within-district for 1884-1892

forvalues y=1884(4)1892{
	
	sum year if year==`y' 
	
	reghdfe dempop_pres_ttl speech lag_dem_pres_ttl 							$demo $geog $econ if year==`y' & state_speech==1 & railroad_dum==1, cluster(district) a(district)
	estimates store district_binary_`y'

}

// Within-district for 1896

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(district)
estimates store district_binary_1896

// Within-district for 1900 controlling for 1892

reghdfe dempop_pres_ttl speech lag2_dem_pres_ttl lag2_pop_pres_ttl			$demo $geog $econ if year==1900 & state_speech==1 & railroad_dum==1, cluster(district) a(district)
estimates store district_binary_1900

// Nearest-neighbor matching for 1884-1892

forvalues y=1884(4)1892{
	
	teffects nnmatch (dempop_pres_ttl lag_dem_pres_ttl $demo $geog $econ i.state) (speech) if year==`y' & state_speech==1 & railroad_dum==1, ///
		metric(euclidean) biasadj(lag_dem_pres_ttl $demo_cont $geog $econ_cont) vce(robust) nneighbor(1)
	estimates store nnd_binary_`y'
	
}

// Nearest-neighbor matching for 1896

teffects nnmatch (dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $econ i.state) (speech) if year==1896 & state_speech==1 & railroad_dum==1, ///
	metric(euclidean) biasadj(lag_dem_pres_ttl lag_pop_pres_ttl $demo_cont $geog $econ_cont) vce(robust) nneighbor(1)
estimates store nnd_binary_1896

// Nearest-neighbor matching for 1900 controlling for 1892

teffects nnmatch (dempop_pres_ttl lag2_dem_pres_ttl lag2_pop_pres_ttl $demo $geog $econ i.state) (speech) if year==1900 & state_speech==1 & railroad_dum==1, ///
	metric(euclidean) biasadj(lag2_dem_pres_ttl lag2_pop_pres_ttl $demo_cont $geog $econ_cont) vce(robust) nneighbor(1)
estimates store nnd_binary_1900

// Neighbor pairs for 1884, 1888, 1892

use "$data/Data_Neighbor_Pairs", clear

xtset county_nbr election
g lag2_dem_pres_ttl=l2.dem_pres_ttl
g lag2_pop_pres_ttl=l2.pop_pres_ttl

forvalues y=1884(4)1892{
	
	sum year if year==`y'
	
	reghdfe dempop_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl	$demo $geog $econ if year==`y' & sample_speech==1 & railroad_dum==1, cluster(nbr_fe) a(nbr_fe)  
	estimates store np_binary_`y'

}

// Neighbor pairs for 1896

reghdfe dempop_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl	$demo $geog $econ if year==1896 & sample_speech==1 & railroad_dum==1, cluster(nbr_fe) a(nbr_fe)  
estimates store np_binary_1896

// Neighbor pairs for 1900 controlling for 1892

reghdfe dempop_pres_ttl speech lag2_dem_pres_ttl lag2_pop_pres_ttl	$demo $geog $econ if year==1900 & sample_speech==1 & railroad_dum==1, cluster(nbr_fe) a(nbr_fe)  
estimates store np_binary_1900

// Entropy balancing for 1884, 1888, 1892

use "$data/Data_County", clear

xtset county election
g lag2_dem_pres_ttl=l2.dem_pres_ttl
g lag2_pop_pres_ttl=l2.pop_pres_ttl

forvalues y=1884(4)1892{
	
	sum year if year==`y' 
	
	reghdfe dempop_pres_ttl speech lag_dem_pres_ttl 							$demo $geog $econ [w=w_full`y'] if year==`y' & state_speech==1 & railroad_dum==1, cluster(district) a(state)  
	estimates store entropy_binary_`y'

}

// Entropy balancing for 1896

reghdfe dempop_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl			$demo $geog $econ [w=w_full1896] if year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state)  
estimates store entropy_binary_1896

// Entropy balancing for 1900

reghdfe dempop_pres_ttl speech lag2_dem_pres_ttl lag2_pop_pres_ttl			$demo $geog $econ [w=w_full1900] if year==1900 & state_speech==1 & railroad_dum==1, cluster(district) a(state)  
estimates store entropy_binary_1900

****************************
***** 3. EXPORT FIGURE *****
****************************

// Figure for paper

coefplot ////
	(false_binary_1884,  		mcolor(navy) msymbol(S) offset(-.42))	///
	(district_binary_1884, 		mcolor(green) msymbol(D) offset(-.36)) ///
	(nnd_binary_1884, 			mcolor(orange) msymbol(T) offset(-1.3)) ///
	(np_binary_1884, 			mcolor(maroon) msymbol(O) offset(-.24)) ///
	(entropy_binary_1884, 		mcolor(purple) msymbol(S) offset(-.18)) /// 
	(false_binary_1888,  		mcolor(navy) msymbol(S) offset(-.02))	///
	(district_binary_1888, 		mcolor(green) msymbol(D) offset(.04)) ///
	(nnd_binary_1888, 			mcolor(orange) msymbol(T) offset(-.9)) ///
	(np_binary_1888, 			mcolor(maroon) msymbol(O) offset(.16)) ///
	(entropy_binary_1888, 		mcolor(purple) msymbol(S) offset(.22)) /// 
	(false_binary_1892,  		mcolor(navy) msymbol(S) offset(.38)) ///
	(district_binary_1892, 		mcolor(green) msymbol(D) offset(.44)) ///
	(nnd_binary_1892, 			mcolor(orange) msymbol(T) offset(-.5)) ///
	(np_binary_1892, 			mcolor(maroon) msymbol(O) offset(.56)) ///
	(entropy_binary_1892, 		mcolor(purple) msymbol(S) offset(.62)) /// 
	(false_binary_1896,  		mcolor(navy) msymbol(S) offset(.78)) ///
	(district_binary_1896, 		mcolor(green) msymbol(D) offset(.84)) ///
	(nnd_binary_1896, 			mcolor(orange) msymbol(T) offset(-.1)) ///
	(np_binary_1896, 			mcolor(maroon) msymbol(O) offset(.96)) ///
	(entropy_binary_1896, 		mcolor(purple) msymbol(S) offset(1.02)) /// 
	(false_binary_1900,  		mcolor(navy) msymbol(S) offset(1.18)) ///
	(district_binary_1900, 		mcolor(green) msymbol(D) offset(1.24)) ///
	(nnd_binary_1900, 			mcolor(orange) msymbol(T) offset(.3)) ///
	(np_binary_1900, 			mcolor(maroon) msymbol(O) offset(1.36)) ///
	(entropy_binary_1900, 		mcolor(purple) msymbol(S) offset(1.42)), /// 
	keep(*speech) vertical levels(95) ciopts(recast(rcap) lc(gs13)) graphregion(color(white)) mfcolor(white) pstyle(p3) ///
	xlabel(.7 `" "1884" "(None)" "' 1.1 `" "1888" "(None)" "' 1.5 `" "1892" "(None)" "' 1.9 `" "1896" "(Bryan)" "' 2.3 `" "1900" "(Both D & R)" "') ///  
	mlabel mlabsize(vsmall) format(%9.2f) mlabposition(6) mlabcolor(navy) mlabgap(*2) /// 
	xline(1.7, lcolor(gs12) lpattern(dash)) xline(2.1, lcolor(gs12) lpattern(dash)) ///	 
	yline(0, lpattern(dash)) yline(1, lpattern(dash) lcolor(gs15)) yline(2, lpattern(dash) lcolor(gs15)) yline(-1, lpattern(dash) lcolor(gs15)) yline(-2, lpattern(dash) lcolor(gs15)) ///
	ylabel(-2(1)2, angle(0) format(%9.0f)) yscale(r(-2.2 2.2)) ///	
	ytitle("Coefficient (95% C.I.)", margin(normal)) xtitle("") leg(lab(2 "Baseline") lab(8 "Neighbour Pairs") lab(10 "Entropy balancing") lab(4 "Within district") lab(6 "NN Matching") c(5) order(2 4 6 8 10) symx(*0.25) region(col(white)) size(small))
graph export "$figures/$program.pdf", replace
 
cap log close
