***************************************
*
* Figure C5: Pre-treatment balancedness
*
***************************************
 
global program "FigureC5"

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 s=1*(e(sample))
bysort county: egen sample=max(s)
drop s

// State-year fixed effects

egen year_state=group(year state)

// Time varying controls

global X "c.perc_farm#i.year c.perc_manuf#i.year" 

// Set baseline to 1892

cap g x=1
la var x "1892"

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

// Baseline

preserve

forvalues y=1880(4)1900{
	g dep_var_`y'=speech*(year==`y')
}

reghdfe dempop_pres_ttl dep_var_1884 dep_var_1888 x dep_var_1896 dep_var_1900 $X railroad_dum $geog $demo $econ_p if sample==1 & state_speech==1 & year>1880 & year<=1900, cluster(county) a(county year_state)
keep if e(sample)

collapse (mean) dempop_pres_ttl, by(speech year election)
xtset speech year

xtline dempop_pres_ttl, overlay scheme(burd) ///
	xline(1884 1888 1892, lc(gs8)) xline(1896) xlabel(1884(4)1900, labsize(small)) xscale(r(1883.5 1900.5)) xtitle("Election year", size(small)) /// 
	ylabel(, labsize(small) format(%9.0f)) ytitle("Democratic (%)", size(small)) ///
	leg(lab(1 "No speech counties") lab(2 "Speech counties")) ///
	name("baseline", replace) title("Baseline", size(medsmall)) //

restore

// 1892 Covariates (no elections)

preserve

forvalues y=1880(4)1900{
	g dep_var_`y'=speech*(year==`y')
}

reghdfe dempop_pres_ttl dep_var_1884 dep_var_1888 x dep_var_1896 dep_var_1900 $X railroad_dum $geog $demo $econ_p if sample==1 & state_speech==1 & year>1880 & year<=1900 [w=w_novote_1892], cluster(county) a(county year_state)
keep if e(sample)

collapse (mean) dempop_pres_ttl [w=w_novote_1892], by(speech year election)

xtset speech year

xtline dempop_pres_ttl, overlay scheme(burd) ///
	xline(1884 1888 1892, lc(gs8)) xline(1896) xlabel(1884(4)1900, labsize(small)) xscale(r(1883.5 1900.5)) xtitle("Election year", size(small)) /// 
	ylabel(, labsize(small) format(%9.0f)) ytitle("Democratic (%)", size(small)) ///
	leg(lab(1 "No speech counties") lab(2 "Speech counties")) ///
	name("w_novote_1892", replace) title("1892 Covariates (no elections)", size(medsmall))

restore

// 1892 Election controls

preserve

forvalues y=1880(4)1900{
	g dep_var_`y'=speech*(year==`y')
}

reghdfe dempop_pres_ttl dep_var_1884 dep_var_1888 x dep_var_1896 dep_var_1900 $X railroad_dum $geog $demo $econ_p if sample==1 & state_speech==1 & year>1880 & year<=1900 [w=w_elect1892], cluster(county) a(county year_state)
keep if e(sample)

collapse (mean) dempop_pres_ttl [w=w_elect1892], by(speech year election)

xtset speech year

xtline dempop_pres_ttl, overlay scheme(burd) ///
	xline(1884 1888 1892, lc(gs8)) xline(1896) xlabel(1884(4)1900, labsize(small)) xscale(r(1883.5 1900.5)) xtitle("Election year", size(small)) /// 
	ylabel(, labsize(small) format(%9.0f)) ytitle("Democratic (%)", size(small)) ///
	leg(lab(1 "No speech counties") lab(2 "Speech counties")) ///
	name("w_elect1892", replace) title("1892 Election controls", size(medsmall))

restore

// 1892 Full covariates

preserve

forvalues y=1880(4)1900{
	g dep_var_`y'=speech*(year==`y')
}

reghdfe dempop_pres_ttl dep_var_1884 dep_var_1888 x dep_var_1896 dep_var_1900 $X railroad_dum $geog $demo $econ_p if sample==1 & state_speech==1 & year>1880 & year<=1900 [w=w_1892], cluster(county) a(county year_state)
keep if e(sample)

collapse (mean) dempop_pres_ttl [w=w_1892], by(speech year election)

xtset speech year

xtline dempop_pres_ttl, overlay scheme(burd) ///
	xline(1884 1888 1892, lc(gs8)) xline(1896) xlabel(1884(4)1900, labsize(small)) xscale(r(1883.5 1900.5)) xtitle("Election year", size(small)) /// 
	ylabel(, labsize(small) format(%9.0f)) ytitle("Democratic (%)", size(small)) ///
	leg(lab(1 "No speech counties") lab(2 "Speech counties")) ///
	name("w_1892", replace) title("1892 Full covariates", size(medsmall))

restore

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

grc1leg baseline w_novote_1892 w_elect1892 w_1892, legendfrom(baseline) ycommon xcommon ///
	c(2) graphregion(color(white)) imargin(vsmall)
graph export "$figures/$program.pdf", replace

cap log close
