***************************************
*
* Table D2: Accounting for unemployment
*
***************************************
 
global program "TableD2"

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

// Add unemployment ot controls

global econ 		"perc_farm perc_manuf dlog_avbasket ldist_silver ldist_gold perc_unempl" 

****************************
***** 2. TABLE RESULTS *****
****************************

// Populist (1892) (Column 1)

reghdfe pop_pres_ttl lag_dem_pres_ttl railroad_dum $demo $geog $econ if year==1892 & sample==1, cluster(district) a(state)
estimates store cs_binary_popnospeech
qui sum pop_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
test dlog_avbasket ldist_silver ldist_gold
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

// Populist (1892) (Column 2)

reghdfe pop_pres_ttl speech lag_dem_pres_ttl railroad_dum $demo $geog $econ if year==1892 & sample==1, cluster(district) a(state)
estimates store cs_binary_pop
qui sum pop_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
test dlog_avbasket ldist_silver ldist_gold
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

// Democratic (1896) (Column 3)

reghdfe dempop_pres_ttl speech lag_dem_pres_ttl 	$demo $geog $econ if year==1896 & sample==1, cluster(district) a(state)
estimates store cs_binary_nopop
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
test dlog_avbasket ldist_silver ldist_gold
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

// Democratic (1896) (Column 4)

reghdfe dempop_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl	$demo $geog $econ if year==1896 & sample==1, cluster(district) a(state)
estimates store cs_binary_con
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
test dlog_avbasket ldist_silver ldist_gold
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

// Pro-silver (1896), National Prohibition party (Column 5)

reghdfe natpro_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl	$demo $geog $econ if year==1896 & sample==1, cluster(district) a(state)
estimates store cs_binary_natpro
qui sum natpro_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
test dlog_avbasket ldist_silver ldist_gold
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

// Pro-silver (1896), Labor party (Column 6)

reghdfe soclabor_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl	$demo $geog $econ if year==1896 & sample==1, cluster(district) a(state)
estimates store cs_binary_soclabor
qui sum soclabor_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
test dlog_avbasket ldist_silver ldist_gold
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

// Pro-gold (1896), Republican party (Column 7)

reghdfe rep_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $econ if year==1896 & sample==1, cluster(district) a(state)
estimates store cs_binary_rep
qui sum rep_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
test dlog_avbasket ldist_silver ldist_gold
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

// Pro-gold (1896), National Democratic party (Column 8)

reghdfe demeq_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl	$demo $geog $econ if year==1896 & sample==1, cluster(district) a(state) 
estimates store cs_binary_demeq
qui sum demeq_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
test dlog_avbasket ldist_silver ldist_gold
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

// Pro-gold (1896), Prohibition party (Column 9)

reghdfe prohi_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl	$demo $geog $econ if year==1896 & sample==1, cluster(district) a(state)
estimates store cs_binary_prohi
qui sum prohi_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
test dlog_avbasket ldist_silver ldist_gold
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

***************************
***** 3. EXPORT TABLE *****
***************************

// Fixed effects

estfe . cs_binary_*, labels(state "State FE")
return list

// Table 

estout cs_binary_popnospeech cs_binary_pop cs_binary_nopop cs_binary_con cs_binary_natpro cs_binary_soclabor cs_binary_rep cs_binary_demeq cs_binary_prohi ///  
	using "$tables/$program.tex", replace style(tex) type label ///
	cells(b(fmt(%9.3f) star) se(par fmt(%9.3f)))  ///
	stats(mean_y mean_x N N_clust r2_a, lab("Mean dep. variable" "Mean exp. variable" "Observations" "Clusters" "Adjusted $ R^2 $") fmt(2 2 %9.0fc 0 2)) ///
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	keep(speech lag_dem_pres_ttl lag_pop_pres_ttl railroad_dum perc_unempl dlog_avbasket ldist_silver ldist_gold) ///
	order(speech lag_dem_pres_ttl lag_pop_pres_ttl railroad_dum perc_unempl dlog_avbasket ldist_silver ldist_gold) ///
	varlabels(speech "Speech (binary)" lag_dem_pres_ttl "[0.5em] Lag Democratic (\%)" lag_pop_pres_ttl "[0.5em] Lag Populist (\%)" lag_rep_pres_ttl "[0.5em] Lagged Republican (\%)" railroad_dum "[0.5em] Railroad (binary)" /// 
	perc_unempl "[0.5em] Unemployment rate" dlog_avbasket "[0.5em] Crop value change" ldist_silver "[0.5em] Dist to silver mine" ldist_gold "[0.5em] Dist to gold mine") ///
	title(Accounting for unemployment) ///
	indicate("\\ Demo controls = lpop_total" ///	
	"Geo controls = county_lat" ///	
	"Economic controls = perc_farm" ///	
	"State FE = 0.state" ///	
	, labels(Yes )) ///
	prehead("\begin{table}[h!]" ///
	"\begin{center}" ///
	"\begin{threeparttable}" ///
	"\begin{scriptsize}" ///
	"\caption{@title}\label{$program}" ///
	"\begin{tabular*}{\columnwidth}{@{\extracolsep{\stretch{1}}}l*{@M}{c}@{}}" ///
	"\hline" "\hline" ///
	"&\multicolumn{1}{c}{(1)} &\multicolumn{1}{c}{(2)} &\multicolumn{1}{c}{(3)} &\multicolumn{1}{c}{(4)} &\multicolumn{1}{c}{(5)} &\multicolumn{1}{c}{(6)} &\multicolumn{1}{c}{(7)} &\multicolumn{1}{c}{(8)} &\multicolumn{1}{c}{(9)}\\" ///
	"&\multicolumn{2}{c}{Populist (1892)} &\multicolumn{2}{c}{Democratic (1896)} &\multicolumn{2}{c}{Pro-silver (1896)}  &\multicolumn{3}{c}{Pro-gold (1896)} \\" ///
	" \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(lr){8-10}   " ///
	"\multicolumn{1}{l}{\textit{Dep. Variable}} &\multicolumn{1}{c}{Pop} &\multicolumn{1}{c}{Pop} &\multicolumn{1}{c}{Dem} &\multicolumn{1}{c}{Dem} &\multicolumn{1}{c}{Prohib} &\multicolumn{1}{c}{Labor} &\multicolumn{1}{c}{Rep}	&\multicolumn{1}{c}{Dem}  	&\multicolumn{1}{c}{Prohib} 	\\" /// : Vote (\%)
	"%\multicolumn{1}{l}{\textit{}} &\multicolumn{2}{c}{}	&\multicolumn{2}{c}{}	&\multicolumn{1}{c}{}  &\multicolumn{1}{c}{Labor} &\multicolumn{1}{c}{} 	&\multicolumn{1}{c}{Dem}  	&\multicolumn{1}{c}{Prohib} \\" ///
	"\hline") ///
	posthead("\\") ///
	prefoot("\hline") ///
	postfoot("") //	

// Joint significance test
	
estout cs_binary_popnospeech cs_binary_pop cs_binary_nopop cs_binary_con cs_binary_rep cs_binary_demeq cs_binary_natpro cs_binary_prohi cs_binary_soclabor  ///  
	using "$tables/$program.tex", append style(tex) type label ///
	cells(b(star fmt(%9.3f)) se(par fmt(%9.3f)))  ///
	stats(F_joint p_F_joint, lab("\multicolumn{1}{l}{$ F $-statistic}" "\multicolumn{1}{l}{$ p $-value}") fmt(2 2)) ///
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	drop(*) /// 
	prehead("") ///
	posthead("\hline") ///
	prefoot("Salience of silver \\") ///
	postfoot("\hline" "\hline" ///
	"\end{tabular*}" /// 
	"\end{scriptsize}" ///
	"\begin{tablenotes}[normal,flushleft]" ///
	"\begin{scriptsize}" ///
	"\item \emph{Notes}: OLS regressions. The unit of observation is a county. " ///
	"All regressions include the full set of controls presented in Table \ref{Table1}. " ///
	"All regressions include State fixed-effects. " ///
	"Standard errors clustered at the Congressional district level in parentheses. Standard errors adjusted for spatial correlation in square brackets." /// 
	"* significant at 10\%; ** at 5\%; *** at 1\%. " ///	
	"\end{scriptsize}" ///
	"\end{tablenotes}" ///
	"\end{threeparttable} " ///
	"\end{center}" ///
	"\end{table}") //	

cap log close
