********************************************************************************
*
* Table 2: Cross-sectional evidence 
*
********************************************************************************

global program "Table2"

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

preserve
keep if year==1896 // 2840 counties
keep if state_speech==1 // 1632 in states Bryan gave a speech
keep if railroad_dum==1 // 1498 with railroad access
g dep_var=speech
sum dempop_pres_ttl dep_var lag_dem_pres_ttl lag_pop_pres_ttl	$demo $geog $econ // 1415: with voting info
reghdfe dempop_pres_ttl dep_var lag_dem_pres_ttl lag_pop_pres_ttl	$demo if year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state) 
g sample=1*(e(sample))
sum dempop_pres_ttl dep_var lag_dem_pres_ttl lag_pop_pres_ttl	$demo $geog $econ if sample==1 // from 1402 to 1392: info on perc_farm perc_manuf missing
tab speech sample
restore

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

// Speech binary (Column 1)

preserve

g dep_var=speech

reghdfe dempop_pres_ttl dep_var lag_dem_pres_ttl lag_pop_pres_ttl 	$demo if year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state)  
estimates store cs_binary_noc
qui sum dempop_pres_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
test lag_dem_pres_ttl=lag_pop_pres_ttl
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

keep if e(sample)

xi: reg dempop_pres_ttl dep_var lag_dem_pres_ttl lag_pop_pres_ttl  	$demo i.state if year==1896 & state_speech==1 & railroad_dum==1, cluster(district)  
psacalc delta dep_var,  mcontrol(_Istate_100 - _Istate_550)  rmax(1)
scalar delta=r(delta)
estimates restore cs_binary_noc
estadd scalar delta

acreg dempop_pres_ttl dep_var lag_dem_pres_ttl lag_pop_pres_ttl 		$demo if year==1896 & state_speech==1 & railroad_dum==1, ///
	spatial latitude(county_lat) longitude(county_lon) id(county) time(year) lag(100000) dist(85) lagdist(0) pfe1(state) 
estimates store cs_binary_noc_se

restore

// Speech binary, full controls (Column 2)

reghdfe rep_pres_ttl speech l.rep_pres_ttl 	$demo $geog $econ if year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state)  

preserve

g dep_var=speech

reghdfe dempop_pres_ttl dep_var lag_dem_pres_ttl lag_pop_pres_ttl	$demo $geog $econ if year==1896 & state_speech==1 & railroad_dum==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 dep_var if e(sample)
scalar mean_x=r(mean)
estadd scalar mean_x
test lag_dem_pres_ttl=lag_pop_pres_ttl
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

keep if e(sample)

sum lag_dem_pres_ttl lag_pop_pres_ttl

xi: reg dempop_pres_ttl dep_var lag_dem_pres_ttl lag_pop_pres_ttl  	$demo $geog $econ i.state if year==1896 & state_speech==1 & railroad_dum==1, cluster(district)  
psacalc delta dep_var,  mcontrol(_Istate_100 - _Istate_550)  rmax(1)
scalar delta=r(delta)
estimates restore cs_binary_con
estadd scalar delta

acreg dempop_pres_ttl dep_var lag_dem_pres_ttl lag_pop_pres_ttl 		$demo $geog $econ if year==1896 & state_speech==1 & railroad_dum==1, ///
	spatial latitude(county_lat) longitude(county_lon) id(county) time(year) lag(100000) dist(85) lagdist(0) pfe1(state) 
estimates store cs_binary_con_se

restore

// Speech/10k pop, full controls (Column 3)

preserve

g dep_var=speeches_10k

reghdfe dempop_pres_ttl dep_var lag_dem_pres_ttl lag_pop_pres_ttl	$demo $geog $econ if year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state)  
estimates store cs_count_con
qui sum dempop_pres_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
test lag_dem_pres_ttl=lag_pop_pres_ttl
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

keep if e(sample)

xi: reg dempop_pres_ttl dep_var lag_dem_pres_ttl lag_pop_pres_ttl  	$demo $geog $econ i.state if year==1896 & state_speech==1 & railroad_dum==1, cluster(district)  
psacalc delta dep_var,  mcontrol(_Istate_100 - _Istate_550)  rmax(1)
scalar delta=r(delta)
estimates restore cs_count_con
estadd scalar delta

acreg dempop_pres_ttl dep_var lag_dem_pres_ttl lag_pop_pres_ttl 		$demo $geog $econ if year==1896 & state_speech==1 & railroad_dum==1, ///
	spatial latitude(county_lat) longitude(county_lon) id(county) time(year) lag(100000) dist(85) lagdist(0) pfe1(state) 
estimates store cs_count_con_se

restore

// Speech/10k pop and squared, full controls (Column 4)

preserve

g dep_var=speeches_10k
g dep_var2=speeches_10k*speeches_10k

reghdfe dempop_pres_ttl dep_var dep_var2 lag_dem_pres_ttl lag_pop_pres_ttl	$demo $geog $econ if year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state) 
estimates store cs_countsq_con
qui sum dempop_pres_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
test lag_dem_pres_ttl=lag_pop_pres_ttl
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

keep if e(sample)

xi: reg dempop_pres_ttl dep_var dep_var2 lag_dem_pres_ttl lag_pop_pres_ttl  	$demo $geog $econ i.state if year==1896 & state_speech==1 & railroad_dum==1, cluster(district)  
 psacalc delta dep_var,  mcontrol(_Istate_100 - _Istate_550)  rmax(1)
scalar delta=r(delta)
estimates restore cs_countsq_con
estadd scalar delta

acreg dempop_pres_ttl dep_var dep_var2 lag_dem_pres_ttl lag_pop_pres_ttl 		$demo $geog $econ if year==1896 & state_speech==1 & railroad_dum==1, ///
	spatial latitude(county_lat) longitude(county_lon) id(county) time(year) lag(100000) dist(85) lagdist(0) pfe1(state) 
estimates store cs_countsq_con_se

restore

// Distance (log km), full controls (Column 5)

preserve

g dep_var=ldist_speech_rail

reghdfe dempop_pres_ttl dep_var lag_dem_pres_ttl lag_pop_pres_ttl	$demo $geog $econ if year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state)  
estimates store cs_dist_con
qui sum dempop_pres_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
test lag_dem_pres_ttl=lag_pop_pres_ttl
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

keep if e(sample)

xi: reg dempop_pres_ttl dep_var lag_dem_pres_ttl lag_pop_pres_ttl  	$demo $geog $econ i.state if year==1896 & state_speech==1 & railroad_dum==1, cluster(district)  
psacalc delta dep_var,  mcontrol(_Istate_100 - _Istate_550)  rmax(1)
scalar delta=r(delta)
estimates restore cs_dist_con
estadd scalar delta

acreg dempop_pres_ttl dep_var lag_dem_pres_ttl lag_pop_pres_ttl 		$demo $geog $econ if year==1896 & state_speech==1 & railroad_dum==1, ///
	spatial latitude(county_lat) longitude(county_lon) id(county) time(year) lag(100000) dist(85) lagdist(0) pfe1(state) 
estimates store cs_dist_con_se

restore

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

// Fixed effects

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

// Table 

estout cs_binary_noc cs_binary_con cs_count_con cs_countsq_con cs_dist_con ///  
	using "$tables/$program.tex", replace style(tex) type label ///
	cells(b(fmt(%9.3f)) se(par star fmt(%9.3f)))  ///
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	keep(dep_var) order(dep_var) varlabels(dep_var "Speech variable") /// 
	title(\textit{Cross-sectional evidence}) ///
	prehead("\begin{table}[t!]" ///
	"\begin{center}" ///
	"\begin{threeparttable}" ///
	"\begin{scriptsize}" ///
	"\caption{\textit{@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}{l}{\textit{Dep. Variable}} 				&\multicolumn{1}{c}{Speech} 	&\multicolumn{1}{c}{Speech}  	&\multicolumn{1}{c}{Speeches/} 	&\multicolumn{1}{c}{Speeches/} 	&\multicolumn{1}{c}{Distance}\\" ///
	"\multicolumn{1}{l}{\textit{Democratic vote (\%) in 1896}} 	&\multicolumn{1}{c}{(binary)} 	&\multicolumn{1}{c}{(binary)}	&\multicolumn{1}{c}{10k pop} 	&\multicolumn{1}{c}{10k pop} 	&\multicolumn{1}{c}{(log km)}\\" ///
	"\hline") ///
	posthead("\\")

estout cs_binary_noc_se cs_binary_con_se cs_count_con_se cs_countsq_con_se cs_dist_con_se ///  
	using "$tables/$program.tex", append style(tex) type label ///
	cells(se(star par([ ]) fmt(%9.3f)))  ///
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	keep(dep_var) order(dep_var) varlabels(dep_var " ") // 

estout cs_binary_noc cs_binary_con cs_count_con cs_countsq_con cs_dist_con ///  
	using "$tables/$program.tex", append style(tex) type label ///
	cells(b(fmt(%9.3f)) se(par star fmt(%9.3f)))  ///
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	keep(dep_var2) order(dep_var2) varlabels(dep_var2 "[0.5em] Speech variable squared")
	

estout cs_binary_noc_se cs_binary_con_se cs_count_con_se cs_countsq_con_se cs_dist_con_se ///  
	using "$tables/$program.tex", append style(tex) type label ///
	cells(se(star par([ ]) fmt(%9.3f)))  ///
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	keep(dep_var2) order(dep_var2) varlabels(dep_var2 " ")

estout cs_binary_noc cs_binary_con cs_count_con cs_countsq_con cs_dist_con ///  
	using "$tables/$program.tex", append style(tex) type label ///
	cells(b(fmt(%9.3f)) se(par star fmt(%9.3f)))  ///
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	keep(lag_dem_pres_ttl) order(lag_dem_pres_ttl) varlabels(lag_dem_pres_ttl "[0.5em] Democratic vote (\%) in 1892") // 

estout cs_binary_noc_se cs_binary_con_se cs_count_con_se cs_countsq_con_se cs_dist_con_se ///  
	using "$tables/$program.tex", append style(tex) type label ///
	cells(se(star par([ ]) fmt(%9.3f)))  ///
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	keep(lag_dem_pres_ttl) order(lag_dem_pres_ttl) varlabels(lag_dem_pres_ttl " ") //

estout cs_binary_noc cs_binary_con cs_count_con cs_countsq_con cs_dist_con ///  
	using "$tables/$program.tex", append style(tex) type label ///
	cells(b(fmt(%9.3f)) se(par star fmt(%9.3f)))  ///
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	keep(lag_pop_pres_ttl) order(lag_pop_pres_ttl) varlabels(lag_pop_pres_ttl "[0.5em] Populist vote (\%) in 1892") // 

estout cs_binary_noc_se cs_binary_con_se cs_count_con_se cs_countsq_con_se cs_dist_con_se /// 
	using "$tables/$program.tex", append style(tex) type label ///
	cells(se(star par([ ]) fmt(%9.3f)))  ///
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	keep(lag_pop_pres_ttl) order(lag_pop_pres_ttl) varlabels(lag_pop_pres_ttl " ") //

estout cs_binary_noc cs_binary_con cs_count_con cs_countsq_con cs_dist_con ///  
	using "$tables/$program.tex", append style(tex) type label ///
	stats(mean_y mean_x N N_clust r2_a delta, lab("Mean dep. variable" "Mean exp. variable" "Observations" "Clusters" "Adjusted $ R^2 $" "Oster's $ \delta $") fmt(2 2 %9.0fc 0 2 1)) ///	"R-squared" 0 2
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	drop(*) /// 
	indicate("Demographic controls = lpop_total" "Geographic controls = county_lat" "Economic controls = perc_farm" "State FE = 0.state", labels(Yes )) ///
	prehead("\\") ///
	prefoot("\hline") ///
	postfoot("") //	

// Joint significance test
	
estout cs_binary_noc cs_binary_con cs_count_con cs_countsq_con cs_dist_con ///  
	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 $ (\hat{\beta}_{dem}-\hat{\beta}_{pop}=0) $}" "\multicolumn{1}{l}{p-value $ (\hat{\beta}_{dem}-\hat{\beta}_{pop}=0) $}") fmt(2 2)) ///	"R-squared" 0 2
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	drop(*) /// 
	prehead("") ///
	posthead("\hline") ///
	prefoot("Dem-Pop 1892 coefficient test \\") ///
	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. " ///
	"Demographic controls: Urban (binary), District population (\%),  Log population, White (\%), Male above 21 (\%), Native (\%). " ///
	"Geographic controls: Railroad density (km per sq. km), Distance to state capital (log), Distance to state largest city (log), Latitude, Longitude. " ///
	"Economic controls: Farmers (\%), Manufacture workers (\%), Number of newspapers over eligible voters, Crop value change (log), (log) Distances to silver and gold mines. " ///
	"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
