*****************************
*
* Table B7: Congress election
*
*****************************

global program "TableB7"

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_Congress", 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

xtset county election 
reghdfe dempop_pres_ttl speech l2.dem_pres_ttl l2.pop_pres_ttl	$demo $geog $econ if year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state)
g sample=1*(e(sample))

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

// Speech (Binary) (Column 1)

preserve

g dep_var=speech

reghdfe dempop_cong_ttl dep_var lag_dem_cong_ttl lag_pop_cong_ttl lag2_dem_cong_ttl lag2_pop_cong_ttl	$demo $geog $econ if sample==1 & year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state)
estimates store cs_binary_con
qui sum dempop_cong_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
qui sum dep_var if e(sample)
scalar mean_x=r(mean)
estadd scalar mean_x

restore

// Speech/10k pop (Column 2)

preserve

g dep_var=speeches_10k

reghdfe dempop_cong_ttl dep_var lag_dem_cong_ttl lag_pop_cong_ttl lag2_dem_cong_ttl lag2_pop_cong_ttl	$demo $geog $econ if sample==1 & year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state)
estimates store cs_count_con
qui sum dempop_cong_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
qui sum dep_var if e(sample)
scalar mean_x=r(mean)
estadd scalar mean_x

restore

// Speech/10k pop and squared (Column 3)

preserve

g dep_var=speeches_10k
g dep_var2=speeches_10k*speeches_10k

reghdfe dempop_cong_ttl dep_var dep_var2 lag_dem_cong_ttl lag_pop_cong_ttl lag2_dem_cong_ttl lag2_pop_cong_ttl	$demo $geog $econ if sample==1 & year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state)
estimates store cs_countsq_con
qui sum dempop_cong_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
qui sum dep_var if e(sample)
scalar mean_x=r(mean)
estadd scalar mean_x

restore

// Distance (log km) (Column 4)

preserve

g dep_var=ldist_speech_rail

reghdfe dempop_cong_ttl dep_var lag_dem_cong_ttl lag_pop_cong_ttl lag2_dem_cong_ttl lag2_pop_cong_ttl	$demo $geog $econ if sample==1 & year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state)
estimates store cs_dist_con
qui sum dempop_cong_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
qui sum dep_var if e(sample)
scalar mean_x=r(mean)
estadd scalar mean_x

restore

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

// Fixed effects

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

// Table for paper

estout cs_binary_con cs_count_con cs_countsq_con cs_dist_con ///
	using "$tables/$program.tex", replace style(tex) type label ///
	cells(b(star fmt(%9.3f)) se(par fmt(%9.3f)))  ///
	stats(mean_y N N_clust r2_a, lab("Mean dep. variable" "Observations" "Clusters" "Adjusted $ R^2 $") fmt(2 %9.0fc 0 2)) ///
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	keep(dep_var dep_var2 lag_dem_cong_ttl lag_pop_cong_ttl lag2_dem_cong_ttl lag2_pop_cong_ttl) /// 
	order(dep_var dep_var2 lag_dem_cong_ttl lag_pop_cong_ttl lag2_dem_cong_ttl lag2_pop_cong_ttl) ///
	varlabels(dep_var "Speech variable" dep_var2 "[0.5em] Speech variable squared" lag_dem_cong_ttl "[0.5em] Democratic vote (\%) in 1894 (House)" lag_pop_cong_ttl "[0.5em] Populist vote (\%) in 1894 (House)" lag2_dem_cong_ttl "[0.5em] Democratic vote (\%) in 1892 (House)" lag2_pop_cong_ttl "[0.5em] Populist vote (\%) in 1892 (House)") ///
	title(Effects on Congress election) ///
	indicate("[0.5em] Demographic controls = urban_dum" ///	
	"Geography controls = railroad_dens" ///	
	"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}{l}{\textit{Dep. Variable}} 							&\multicolumn{1}{c}{Speech}  	&\multicolumn{1}{c}{Speeches/} 	&\multicolumn{1}{c}{Speeches/} 	&\multicolumn{1}{c}{Distance}\\" ///
	"\multicolumn{1}{l}{\textit{Dem or/and Pop vote (\%) in 1896 (House)}} 		&\multicolumn{1}{c}{(binary)}	&\multicolumn{1}{c}{10k pop} 	&\multicolumn{1}{c}{10k pop} 	&\multicolumn{1}{c}{(log km)}\\" ///
	"\hline") ///
	posthead("\\") ///
	prefoot("\hline") ///
	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." /// 
	"* significant at 10\%; ** at 5\%; *** at 1\%. " ///	
	"\end{scriptsize}" ///
	"\end{tablenotes}" ///
	"\end{threeparttable} " ///
	"\end{center}" ///
	"\end{table}") //	

cap log close
