***************************************************************
*
* Figure B1: Details on estimating methods: Matching estimators
*
***************************************************************

global program "FigureB1"

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

sort year county // Order matters for psmatch
g id=_n

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

// NNMATCH: Euclidean distance, 2 neighbors, bias adjustment

teffects nnmatch (dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $econ) (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(2)
estimates store R_speech_nn1
qui sum dempop_pres_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y

// NNMATCH: Euclidean distance, 1 neighbor, bias adjustment

*sort id

teffects nnmatch (dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $econ) (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 R_speech_nn2
qui sum dempop_pres_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y

// NNMATCH: Euclidean distance, 1 neighbors, bias adjustment,  ematch(urban_dum)

*sort id

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) ematch(urban_dum) vce(robust) nneighbor(1)
estimates store R_speech_nn4
qui sum dempop_pres_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y

// NNMATCH: Euclidean distance, 1 neighbors, bias adjustment,  ematch(urban_dum)

sort id

cap teffects nnmatch (dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $econ) (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) ematch(state) vce(robust) nneighbor(1) osample(problem1)
cap teffects nnmatch (dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $econ) (speech) if year==1896 & state_speech==1 & railroad_dum==1 & problem1==0, ///
	metric(euclidean) biasadj(lag_dem_pres_ttl lag_pop_pres_ttl $demo_cont $geog $econ_cont) ematch(state) vce(robust) nneighbor(1) osample(problem2)
teffects nnmatch (dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $econ) (speech) if year==1896 & state_speech==1 & railroad_dum==1 & problem1==0 & problem2==0, ///
	metric(euclidean) biasadj(lag_dem_pres_ttl lag_pop_pres_ttl $demo_cont $geog $econ_cont) ematch(state) vce(robust) nneighbor(1) osample(problem3)
estimates store R_speech_nn5
qui sum dempop_pres_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y

// PSMATCH: 1 neighbor

sort id

teffects psmatch (dempop_pres_ttl) (speech lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $econ) if year==1896 & state_speech==1 & railroad_dum==1, ///
	vce(robust) nneighbor(1)
estimates store R_speech_nn3
qui sum dempop_pres_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y

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

// Figure for paper

coefplot (R_speech_nn1, offset(-.4)) (R_speech_nn2, offset(-.2)) (R_speech_nn4, offset(-.0)) (R_speech_nn5, offset(.2)) (R_speech_nn3, offset(.4)), ///
	vertical levels(95 90) ciopts(recast(rcap rcap) lc(gs13 gs13)) ///
	msymbol(S) msize(large) mfcolor(white) pstyle(p3) fcolor(white) ///
	mlabel mlabsize(small) format(%9.3f) mlabposition(3) mlabcolor(navy) mlabgap(*2) ///
	xlabel(.6 `" "NN Match" "2 Matches" "' .8 `" "NN Match" "1 Match" "(Baseline)" "' 1.0 `" "NN Match" "1 Match" "Exact Urban" "' 1.2 `" "NN Match" "1 Match" "Exact State" "' 1.4 `" "PS Match" "1 Match" "', labsize(small)) /// 
	yline(0, lpattern(dash)) ylabel(0(1)3, angle(0) format(%9.0f)) ytitle("Coefficient (95%, 90% C.I.)") ///
	yline(1, lpattern(dash) lcolor(gs15)) yline(2, lpattern(dash) lcolor(gs15)) yline(3, lpattern(dash) lcolor(gs15)) ///
	legend(off)
graph export "$figures/$program.pdf", replace
	
cap log close