***********************************
*
* Table 1: Determinants of speeches
*
***********************************
 
global program "TableB1"

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 *****
***************************

// 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"

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

// State level, baseline controls (Column 1)

use "$data/Data_State", clear

reghdfe speech lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $econ  if year==1896, vce(robust) noabsorb 
estimates store speech_state_base
sum speech if e(sample)
estadd scalar y_mean=r(mean)
test perc_white perc_male21 perc_below21 perc_native perc_literate newsp_circ_elig
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)
test lag_dem_pres_ttl lag_pop_pres_ttl
return list
estadd scalar F_vote=r(F)
estadd scalar p_F_vote=r(p)

// State level, vote controls (Column 2)

reghdfe speech lag_dem_pres_ttl lag_pop_pres_ttl lag_margin_pres_ttl lag_ttl_pres_tev $demo $geog $econ  if year==1896, vce(robust) noabsorb 
estimates store speech_state_level
sum speech if e(sample)
estadd scalar y_mean=r(mean)
test perc_white perc_male21 perc_below21 perc_native perc_literate newsp_circ_elig
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)
test lag_dem_pres_ttl lag_pop_pres_ttl lag_margin_pres_ttl lag_ttl_pres_tev
return list
estadd scalar F_vote=r(F)
estadd scalar p_F_vote=r(p)

// District level, baseline controls (Column 3)

use "$data/Data_District", clear

reghdfe speech lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $econ  if year==1896 & state_speech==1, cluster(state) a(state)
estimates store speech_district_base
sum speech if e(sample)
estadd scalar y_mean=r(mean)
test perc_white perc_male21 perc_below21 perc_native perc_literate newsp_circ_elig
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)
test lag_dem_pres_ttl lag_pop_pres_ttl
return list
estadd scalar F_vote=r(F)
estadd scalar p_F_vote=r(p)

// District level, vote controls (Column 4)

reghdfe speech lag_dem_pres_ttl lag_pop_pres_ttl lag_margin_pres_ttl lag_ttl_pres_tev $demo $geog $econ  if year==1896 & state_speech==1, cluster(state) a(state)
estimates store speech_district_sample
sum speech if e(sample)
estadd scalar y_mean=r(mean)
test perc_white perc_male21 perc_below21 perc_native perc_literate newsp_circ_elig
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)
test lag_dem_pres_ttl lag_pop_pres_ttl lag_margin_pres_ttl lag_ttl_pres_tev
return list
estadd scalar F_vote=r(F)
estadd scalar p_F_vote=r(p)

// County level, baseline controls (Column 4)

use "$data/Data_County", clear

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

reghdfe 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)  
estimates store speech_base
sum speech if e(sample)
estadd scalar y_mean=r(mean)
test perc_white perc_male21 perc_below21 perc_native perc_literate newsp_circ_elig
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)
test lag_dem_pres_ttl lag_pop_pres_ttl
return list
estadd scalar F_vote=r(F)
estadd scalar p_F_vote=r(p)

// County level, vote controls (Column 5)

reghdfe speech lag_dem_pres_ttl lag_pop_pres_ttl lag_margin_pres_ttl lag_ttl_pres_tev $demo $geog $econ  if year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state)  
estimates store speech_state
sum speech if e(sample)
estadd scalar y_mean=r(mean)
test perc_white perc_male21 perc_below21 perc_native perc_literate newsp_circ_elig
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)
test lag_dem_pres_ttl lag_pop_pres_ttl lag_margin_pres_ttl lag_ttl_pres_tev
return list
estadd scalar F_vote=r(F)
estadd scalar p_F_vote=r(p)

// County level, demo controls (Column 6)

reghdfe speech lag_dem_pres_ttl lag_pop_pres_ttl lag_margin_pres_ttl lag_ttl_pres_tev $geog $econ $demo_add if year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state)  
estimates store speech_state_add
sum speech if e(sample)
estadd scalar y_mean=r(mean)
test perc_white perc_below21 perc_native perc_literate pop_male2144 pop_male4564 pop_male6599 perc_catholic perc_black newsp_circ_elig
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)
test lag_dem_pres_ttl lag_pop_pres_ttl lag_margin_pres_ttl lag_ttl_pres_tev
return list
estadd scalar F_vote=r(F)
estadd scalar p_F_vote=r(p)

// Crowd, baseline controls (Column 7)

reghdfe log_crowd_avg lag_dem_pres_ttl lag_pop_pres_ttl  $demo $geog $econ  if year==1896 & state_speech==1 & railroad_dum==1 , cluster(district) noabsorb
estimates store crowd_nofe
sum log_crowd_avg if e(sample)
estadd scalar y_mean=r(mean)
test perc_white perc_male21 perc_below21 perc_native perc_literate newsp_circ_elig
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)
test lag_dem_pres_ttl lag_pop_pres_ttl
return list
estadd scalar F_vote=r(F)
estadd scalar p_F_vote=r(p)

// Crowd, state FE (Column 8)

reghdfe log_crowd_avg 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 crowd_fe
sum log_crowd_avg if e(sample)
estadd scalar y_mean=r(mean)
test perc_white perc_male21 perc_below21 perc_native perc_literate newsp_circ_elig
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)
test lag_dem_pres_ttl lag_pop_pres_ttl
return list
estadd scalar F_vote=r(F)
estadd scalar p_F_vote=r(p)

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

// Fixed effects

estfe . speech_* crowd_*, labels(state "State FE" nbr_fe "Neighbor-pair FE") 
return list

// Labels

la var lag_dem_pres_ttl 	"\\ Democratic 1892 (\%)"
la var lag_pop_pres_ttl 	"[0.3em] Populist 1892 (\%)"
la var lag_rep_pres_ttl 	"[0.3em] Republican 1892 (\%)"
la var lag_margin_pres_ttl	"[0.3em] Margin 1892 (\%)"
la var lag_ttl_pres_tev 	"[0.3em] Turnout 1892 (\%)"
la var lpop_total 			"[0.3em] Population (log)"
la var larea	 			"[0.3em] Area (log)"
la var county_lat 			"[0.3em] Latitude"
la var county_lon 			"[0.3em] Longitude"
la var ldist_capital 		"[0.3em] State capital (log dist)"
la var ldist_largest 		"[0.3em] Largest city (log dist)"
la var perc_white 			"[0.3em] White population (\%)"
la var perc_black			"[0.3em] Black population (\%)"
la var perc_native 			"[0.3em] Native population (\%)"
la var perc_male21 			"[0.3em] Male above 21 (\%)"
la var perc_below21			"[0.3em] Aged below 21 (\%)"
la var perc_literate 		"[0.3em] Literacy (\%)" 
la var perc_farm 			"[0.3em] Farmer households (\%)"
la var perc_manuf 			"[0.3em] Manuf workers (\%)"
la var dlog_avbasket		"[0.3em] Crop value change (log)"
la var newsp_circ_elig		"[0.3em] News circ (per voter)"
la var ldist_silver			"[0.3em] Silver mine (log dist)"
la var ldist_gold 			"[0.3em] Gold mine (log dist)"
la var perc_catholic		"[0.3em] Catholic population (\%)"
la var pop_male2144 		"[0.3em] Male aged 21-44 (\%)" 
la var pop_male4564 		"[0.3em] Male aged 45-64 (\%)" 
la var pop_male6599			"[0.3em] Male age above 64 (\%)" 
la var perc_pop_dstr 		"[0.3em] Population (\% of district)"
la var lag_ttl_pres_tev		"[0.3em] Turnout 1892 (\%)"
la var urban_dum			"[0.3em] Urban counties"
la var perc_pop_dstr		"[0.3em] Pop (\% of c/s/d)"

// Export Table (part1)

global program "TableB1"

estout speech_state_base speech_state_level speech_district_base speech_district_sample speech_base speech_state speech_state_add crowd_nofe crowd_fe /// 
	using "$tables/$program.tex", replace style(tex) type label ///
	cells(b(star fmt(%9.3f)) se(par fmt(%9.3f)))  ///
	collabels(none) ///	
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	keep(lag_dem_pres_ttl lag_pop_pres_ttl lag_margin_pres_ttl lag_ttl_pres_tev perc_pop_dstr urban_dum lpop_total $geog $econ perc_white perc_male21 perc_below21 perc_native) /// 
	order(lag_dem_pres_ttl lag_pop_pres_ttl lag_margin_pres_ttl lag_ttl_pres_tev perc_pop_dstr urban_dum lpop_total $geog $econ perc_white perc_male21 perc_below21 perc_native)  ///  
	title(Determinants of speeches) ///
	prehead("\begin{table}[h!]" ///
	"\begin{center}" ///	
	"%\resizebox {1\textwidth}{!}{%" ///
	"\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{1}{l}{\textit{Dep. Variable}} &\multicolumn{7}{c}{Speech (Binary)} &\multicolumn{2}{c}{(log) Crowd} 	  \\" ///
	"\cmidrule(lr){2-8}    \cmidrule(lr){9-10}   " ///
	"\multicolumn{1}{l}{\textit{Observation unit}} &\multicolumn{2}{c}{State} &\multicolumn{2}{c}{District} &\multicolumn{3}{c}{County} &\multicolumn{2}{c}{County}\\" ///
	"\cmidrule(lr){2-3} \cmidrule(lr){4-5}  \cmidrule(lr){6-8}    \cmidrule(lr){9-10}   " ///
	"\multicolumn{1}{l}{\textit{Controls}} &\multicolumn{1}{c}{Baseline} &\multicolumn{1}{c}{Vote} &\multicolumn{1}{c}{Baseline} &\multicolumn{1}{c}{Vote} &\multicolumn{1}{c}{Baseline} &\multicolumn{1}{c}{Vote} &\multicolumn{1}{c}{Demo}  &\multicolumn{1}{c}{Baseline} &\multicolumn{1}{c}{State FE}\\" ///
	"\hline") ///
	prefoot("\hline") ///
	postfoot( ///
	"\end{tabular*}" /// 
	"\end{scriptsize}" ///
	"\begin{tablenotes}[normal,flushleft]" ///
	"\begin{scriptsize}" ///
	"\item \emph{Notes}: This table continues on the next page. " ///	
	"\end{scriptsize}" ///
	"\end{tablenotes}" ///
	"\end{threeparttable}%}" ///
	"\end{center}" ///
	"\end{table}") //	

// Export Table (part2)

global program "TableB1_pt2"
	
estout speech_state_base speech_state_level speech_district_base speech_district_sample speech_base speech_state speech_state_add crowd_nofe crowd_fe /// 
	using "$tables/$program.tex", replace style(tex) type label ///
	stats(y_mean N N_clust r2_a, lab("Mean dep. variable" "Observations" "Clusters" "Adjusted $ R^2 $") fmt(2 %9.0fc 0 2)) ///	"R-squared" 0 2
	cells(b(star fmt(%9.3f)) se(par fmt(%9.3f)))  ///
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	keep(perc_literate pop_male2144 pop_male4564 pop_male6599 perc_catholic perc_black) /// 
	order(perc_literate pop_male2144 pop_male4564 pop_male6599 perc_catholic perc_black)  /// 
	varlabels(perc_literate "\\ Literacy (\%)") ///
	indicate("\\ State FE = 0.state" ///
	, labels(Yes )) ///
	title("Determinants of speeches (cont'd)") ///
	prehead("\begin{table}[h!]" ///
	"\begin{center}" ///	
	"%\resizebox {1\textwidth}{!}{%" ///
	"\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{1}{l}{\textit{Dep. Variable}} &\multicolumn{7}{c}{Speech (Binary)} &\multicolumn{2}{c}{(log) Crowd} 	  \\" ///
	"\cmidrule(lr){2-8}    \cmidrule(lr){9-10}   " ///
	"\multicolumn{1}{l}{\textit{Observation unit}} &\multicolumn{2}{c}{State} &\multicolumn{2}{c}{District} &\multicolumn{3}{c}{County} &\multicolumn{2}{c}{County}\\" ///
	"\cmidrule(lr){2-3} \cmidrule(lr){4-5}  \cmidrule(lr){6-8}    \cmidrule(lr){9-10}   " ///
	"\multicolumn{1}{l}{\textit{Controls}} &\multicolumn{1}{c}{Baseline} &\multicolumn{1}{c}{Vote} &\multicolumn{1}{c}{Baseline} &\multicolumn{1}{c}{Vote} &\multicolumn{1}{c}{Baseline} &\multicolumn{1}{c}{Vote} &\multicolumn{1}{c}{Demo}  &\multicolumn{1}{c}{Baseline} &\multicolumn{1}{c}{State FE}\\" ///
	"\hline") ///
	posthead("") ///
	prefoot("\hline") ///
	postfoot("") //	

// Joint significance test
	
estout speech_state_base speech_state_level speech_district_base speech_district_sample speech_base speech_state speech_state_add crowd_nofe crowd_fe /// 
	using "$tables/$program.tex", append style(tex) type label ///
	cells(b(star fmt(%9.3f)) se(par fmt(%9.3f)))  ///
	stats(F_vote p_F_vote, 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("Past vote significance \\") ///
	postfoot("") //	

estout speech_state_base speech_state_level speech_district_base speech_district_sample speech_base speech_state speech_state_add crowd_nofe crowd_fe /// 
	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("") ///
	prefoot("Demographics significance \\") ///
	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. " ///
	"Past vote significance is a joint test for the coefficients of the 1892 Democratic vote, the 1892 Populist vote, and when present, the 1892 joint Democrat-Populist and Republican margin and 1892 turnout. " ///
	"Demographics significance is a joint test for the coefficients of the white population, male population above 21, population aged below 21, native population, literacy, newspaper circulation, " /// 
	"and when present, male population aged 21-44, male population aged 45-64, male population above 65, catholic population, and black population. " ///
	"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
