********************************************************
*
* Figure B3: Details on estimating methods: Balancedness
*
********************************************************

global program "FigureB3"

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)
g sample=1*(e(sample))

keep if sample==1

// Standardize vars

center dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl lag_dempop_pres_ttl $demo $geog $econ, inplace standardize

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

// Within state, no controls

foreach var in lag_dem_pres_ttl lag_pop_pres_ttl lag_dempop_pres_ttl $demo $geog $econ{
	rename `var' outcome
	rename speech `var'
	reghdfe outcome `var' if year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state)
	estimates store R_`var'_s
	rename `var' speech 
	rename outcome `var'
}

global OLS_estimates_s "R_lag_dem_pres_ttl_s \ R_lag_pop_pres_ttl_s \ R_lag_dempop_pres_ttl_s \ R_lpop_total_s \ R_urban_dum_s \ R_county_lat_s \ R_county_lon_s \ R_ldist_capital_s \ R_ldist_largest_s \ R_perc_white_s \ R_perc_native_s \ R_perc_male21_s \ R_perc_below21_s \ R_perc_literate_s \ R_perc_farm_s \ R_perc_manuf_s \ R_dlog_avbasket_s \ R_newsp_circ_elig_s \ R_ldist_silver_s \ R_ldist_gold_s" //

// Within state, urban and population controls

g u=urban_dum
g p=lpop_total

foreach var in lag_dem_pres_ttl lag_pop_pres_ttl lag_dempop_pres_ttl $demo $geog $econ{
	rename `var' outcome
	rename speech `var'
	reghdfe outcome `var' u p if year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state)
	estimates store R_`var'_sp
	rename `var' speech 
	rename outcome `var'
}

global OLS_estimates_sp "R_lag_dem_pres_ttl_sp \ R_lag_pop_pres_ttl_sp \ R_lag_dempop_pres_ttl_sp \ R_lpop_total_sp \ R_urban_dum_sp \ R_county_lat_sp \ R_county_lon_sp \ R_ldist_capital_sp \ R_ldist_largest_sp \ R_perc_white_sp \ R_perc_native_sp \ R_perc_male21_sp \ R_perc_below21_sp \ R_perc_literate_sp \ R_perc_farm_sp \ R_perc_manuf_sp \ R_dlog_avbasket_sp \ R_newsp_circ_elig_sp \ R_ldist_silver_sp \ R_ldist_gold_sp" //

// Within district, urban and population controls

foreach var in lag_dem_pres_ttl lag_pop_pres_ttl lag_dempop_pres_ttl $demo $geog $econ{
	rename `var' outcome
	rename speech `var'
	reghdfe outcome `var' u p if year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(district)
	estimates store R_`var'_dp
	rename `var' speech 
	rename outcome `var'
}

global OLS_estimates_dp "R_lag_dem_pres_ttl_dp \ R_lag_pop_pres_ttl_dp \ R_lag_dempop_pres_ttl_dp \ R_lpop_total_dp \ R_urban_dum_dp \ R_county_lat_dp \ R_county_lon_dp \ R_ldist_capital_dp \ R_ldist_largest_dp \ R_perc_white_dp \ R_perc_native_dp \ R_perc_male21_dp \ R_perc_below21_dp \ R_perc_literate_dp \ R_perc_farm_dp \ R_perc_manuf_dp \ R_dlog_avbasket_dp \ R_newsp_circ_elig_dp \ R_ldist_silver_dp \ R_ldist_gold_dp" // \ R_mine_dum_dp" //   

// Nearest neighbor matching, no controls

use "$data/Data_County", clear

sort year county
g id=_n

tempfile  tmp
save `tmp.dta', replace

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) vce(robust) nneighbor(2) generate(nn)

keep if speech==1

keep id nn1 nn2
drop if nn1==. | nn2==.

g pair=id
rename id nn0

reshape long nn, i(pair) j(rank)

rename nn id
merge m:1 id using `tmp.dta'
drop if _merge!=3

center dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl lag_dempop_pres_ttl $demo $geog $econ, inplace standardize

foreach var in lag_dem_pres_ttl lag_pop_pres_ttl lag_dempop_pres_ttl $demo $geog $econ{
	rename `var' outcome
	rename speech `var'
	eststo: reghdfe outcome `var', cluster(pair) a(pair)
	estimates store R_`var'_nn
	rename `var' speech 
	rename outcome `var'
}

global OLS_estimates_nn "R_lag_dem_pres_ttl_nn \ R_lag_pop_pres_ttl_nn \ R_lag_dempop_pres_ttl_nn \ R_lpop_total_nn \ R_urban_dum_nn \ R_county_lat_nn \ R_county_lon_nn \ R_ldist_capital_nn \ R_ldist_largest_nn \ R_perc_white_nn \ R_perc_native_nn \ R_perc_male21_nn \ R_perc_below21_nn \ R_perc_literate_nn \ R_perc_farm_nn \ R_perc_manuf_nn \ R_dlog_avbasket_nn \ R_newsp_circ_elig_nn \ R_ldist_silver_nn \ R_ldist_gold_nn" 

// Neighbor pairs, urban and population controls

use "$data/Data_Neighbor_Pairs", clear

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)
keep if e(sample)

center dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl lag_dempop_pres_ttl $demo $geog $econ, inplace standardize

g u=urban_dum
g p=lpop_total

foreach var in lag_dem_pres_ttl lag_pop_pres_ttl lag_dempop_pres_ttl $demo $geog $econ{
	rename `var' outcome
	rename speech `var'
	eststo: reghdfe outcome `var' u p if year==1896 & state_speech==1 & railroad_dum==1, cluster(nbr_fe) a(nbr_fe)
	estimates store R_`var'_np
	rename `var' speech 
	rename outcome `var'
}

global OLS_estimates_np "R_lag_dem_pres_ttl_np \ R_lag_pop_pres_ttl_np \ R_lag_dempop_pres_ttl_np \ R_lpop_total_np \ R_urban_dum_np \ R_county_lat_np \ R_county_lon_np \ R_ldist_capital_np \ R_ldist_largest_np \ R_perc_white_np \ R_perc_native_np \ R_perc_male21_np \ R_perc_below21_np \ R_perc_literate_np \ R_perc_farm_np \ R_perc_manuf_np \ R_dlog_avbasket_np \ R_newsp_circ_elig_np \ R_ldist_silver_np \ R_ldist_gold_np" //  

// Entropy weights, no controls

use "$data/Data_County", clear

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)
keep if e(sample)

center dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl lag_dempop_pres_ttl $demo $geog $econ, inplace standardize

foreach var in lag_dem_pres_ttl lag_pop_pres_ttl lag_dempop_pres_ttl $demo $geog $econ{
	rename `var' outcome
	rename speech `var'
	reghdfe outcome `var' if year==1896 & state_speech==1 & railroad_dum==1 [pw=w_full1896], cluster(district) noabsorb
	estimates store R_`var'_ew
	rename `var' speech 
	rename outcome `var'
}

global OLS_estimates_ew "R_lag_dem_pres_ttl_ew \ R_lag_pop_pres_ttl_ew \ R_lag_dempop_pres_ttl_ew \ R_lpop_total_ew \ R_urban_dum_ew \ R_county_lat_ew \ R_county_lon_ew \ R_ldist_capital_ew \ R_ldist_largest_ew \ R_perc_white_ew \ R_perc_native_ew \ R_perc_male21_ew \ R_perc_below21_ew \ R_perc_literate_ew \ R_perc_farm_ew \ R_perc_manuf_ew \ R_dlog_avbasket_ew \ R_newsp_circ_elig_ew \ R_ldist_silver_ew \ R_ldist_gold_ew"

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

// Labels

la var lag_dem_pres_ttl 	"Democratic 1892 (%)"
la var lag_pop_pres_ttl 	"Populist 1892 (%)"
la var lag_dempop_pres_ttl 	"Dem+Pop 1892 (%)"
la var lag_rep_pres_ttl 	"Republican 1892 (%)"
la var lag_margin_pres_ttl	"Margin 1892 (%)"
la var lag_ttl_pres_tev 	"Turnout 1892 (%)"
la var lpop_total 			"Population (log)"
la var urban_dum			"Urban county (binary)"
la var larea	 			"Area (log)"
la var county_lat 			"Latitude"
la var county_lon 			"Longitude"
la var ldist_capital 		"Dist to state capital (log)"
la var ldist_largest 		"Dist to state largest city (log)"
la var perc_white 			"White population (%)"
la var perc_black			"Black population (%)"
la var perc_native 			"Native population (%)"
la var perc_male21 			"Male above 21 (%)"
la var perc_below21			"Aged below 21 (%)"
la var perc_literate 		"Literacy (%)" 
la var perc_farm 			"Farmer households (%)"
la var perc_manuf 			"Manufacture workers (%)"
la var dlog_avbasket		"Crop value change (log)"
la var newsp_circ_elig		"News circulation (per voter)"
la var ldist_silver			"Dist to silver mine (log)"
la var ldist_gold 			"Dist to gold mine (log)"
la var perc_catholic		"Catholic (%)"

// Figure

coefplot ($OLS_estimates_s), drop(_cons u p a $pop5) bylabel(`" "Within State" "No controls" "') ///
	|| ($OLS_estimates_sp), drop(_cons u p a $pop5) bylabel(`" "Within State" "Pop/Urban" "')  ///
	|| ($OLS_estimates_dp), drop(_cons u p a $pop5) bylabel(`" "Within District" "Pop/Urban" "')  ///
	|| ($OLS_estimates_nn), drop(_cons u p a $pop5) bylabel(`" "NN Matching" "No controls" "')  ///
	|| ($OLS_estimates_np), drop(_cons u p a $pop5) bylabel(`" "Neighbor Pairs" "Pop/Urban" "')  ///
	|| ($OLS_estimates_ew), drop(_cons u p a $pop5) bylabel(`" "Entropy" "No controls" "')  ///
	||, xlabel(-1 "-1" -0.5 0 "0" 0.5 1 "1", labsize(vsmall) format(%9.1f)) ylabel(, angle(0) labsize(vsmall)) yscale(r(0 20.5)) ///
	horizontal mcolor(navy) msymbol(S) msize(vsmall) mfcolor(white) pstyle(p3) fcolor(*.5) ///
	levels(90) ciopts(recast(rcap) lc(navy*.5) lwidth(thin)) citop ///
	msize(tiny) mlabel mlabposition(12) mlabcolor(navy) mlabgap(*1) mlabsize(tiny) format(%9.1f) /// 
	xline(0, lwidth(thin) lpattern(dash)) xtitle("Speech - No-Speech Difference (std.dev.)", margin(small) size(small)) /// 
	graphregion(color(white) lstyle(none)) subtitle(, size(small) bmargin(top) margin(vsmall) color(white) bcolor(navy)) ///
	byopts(cols(6) legend(off) bgcolor(white) graphregion(fcolor(white) lstyle(none))) //	
graph export "$figures/$program.pdf", replace

cap log close
