********************************************************************************
*
* Table 1: Summary Statistics
*
********************************************************************************
 
global program "Table1"

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

keep if year==1896 // 2840 counties // 2507
keep if state_speech==1 // 1632 in states Bryan gave a speech // 1536
keep if railroad_dum==1 // 1498 with railroad access // 1422
sum dempop_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $econ // 1421: with voting info
order year state_abb state county dempop_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl	$demo $geog $econ // 1415: with voting info
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) keepsing // $vote 
g sample=1*(e(sample))
tab speech sample

keep if sample==1 // 1410 obs

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

// Means (Column 1)

estpost tabstat ///
	speech speeches_10k ldist_speech_bird dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl lag_dempop_pres_ttl $demo $geog $econ /// 
	, s(mean sd) columns(statistics) 
matrix m=e(mean) // matrix m=e(Mean) // For Stata 17
matrix s=e(sd) // matrix s=e(SD) // For Stata 17
matrix mean=m'
matrix sd=s'

// Correlation with Dem 1892 vote (Columns 2 and 3)

estpost corr lag_dempop_pres_ttl ///
	speech speeches_10k ldist_speech_bird dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl lag_dempop_pres_ttl $demo $geog $econ  
matrix b=e(b) 
matrix p=e(p)  
matrix beta=b'
matrix pbeta=p'

// Means for Speech/No-speech (Columns 4 and 5)

forvalues i=0(1)1{
	estpost tabstat ///
		speech speeches_10k ldist_speech_bird dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl lag_dempop_pres_ttl $demo $geog $econ if speech==`i' ///  
		, s(mean N) columns(statistics)  
	matrix m`i'=e(mean) // matrix m`i'=e(Mean) // For Stata 17 
	matrix mp`i'=m`i''
}

cap recode speech (1=0) (0=1), g(nospeech)

// Difference in means (Columns 6 and 7)

estpost ttest ///
	speech speeches_10k ldist_speech_bird dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl lag_dempop_pres_ttl $demo $geog $econ, by(nospeech)  
matrix d=e(b)  
matrix delta=d'
matrix p=e(p)  
matrix pttest=p'
	
// Get summary stats matrix
	
matrix b = mean, beta, pbeta, mp1, mp0, delta, pttest   
matrix list b

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

// Labels

la var speech 				"\\ Speech (binary)"
la var speeches 			"[0.3em] Speeches (\#)"
la var speeches_10k			"[0.3em] Speeches per 10k pop"
la var ldist_speech_bird	"[0.3em] Distance to speech (log)"
la var dempop_pres_ttl		"[0.3em] Democratic 1896 (\%)"
la var lag_dempop_pres_ttl 	"[0.3em] Dem and Pop 1892 (\%)"
la var lag_dem_pres_ttl 	"[0.3em] Democratic 1892 (\%)"
la var lag_pop_pres_ttl 	"[0.3em] Populist 1892 (\%)"
la var lpop_total 			"[0.3em] Population (log)"
la var county_lat 			"[0.3em] Latitude"
la var county_lon 			"[0.3em] Longitude"
la var ldist_capital 		"[0.3em] Dist to state capital (log)"
la var ldist_largest 		"[0.3em] Dist to state largest city (log)"
la var perc_white 			"[0.3em] White 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] Manufacture workers (\%)"
la var dlog_avbasket		"[0.3em] Crop value change (log)"
la var newsp_circ_elig		"[0.3em] News circ (per elig voter)"
la var ldist_silver			"[0.3em] Dist to silver mine (log)"
la var ldist_gold 			"[0.3em] Dist to gold mine (log)"
la var perc_pop_dstr		"[0.3em] Population (\% of district)"
la var urban_dum			"[0.3em] Urban county (binary)"
la var railroad_dens		"[0.3em] Railroad density"

// Export Table

estout matrix(b, fmt(2)) using ///
	"$tables/$program.tex", replace style(tex) type label /// 	 
	unstack collabels(none) ///
	mlabels(, none) ///
	msign(--) ///
	title(\textit{Summary statistics}) ///
	prehead("\begin{table}[t!]" ///
	"\begin{center}" ///
	"\begin{threeparttable}" ///
	"\begin{scriptsize}" ///
	"\caption{\textit{@title}}" ///
	"\label{$program}"  /// 
	"\begin{tabular*}{\columnwidth}{@{\extracolsep{\stretch{1}}}l*{7}{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}{Mean} 	&\multicolumn{1}{c}{Corr 1892}		&\multicolumn{1}{c}{Correlation}		&\multicolumn{1}{c}{Speech}  	&\multicolumn{1}{c}{No speech}	&\multicolumn{1}{c}{Difference}	&\multicolumn{1}{c}{Difference}\\" ///  	
	"	&\multicolumn{1}{c}{}  		&\multicolumn{1}{c}{Dem and Pop}		&\multicolumn{1}{c}{\textit{p-value}}	&\multicolumn{1}{c}{mean}		&\multicolumn{1}{c}{mean}		&\multicolumn{1}{c}{in means}	&\multicolumn{1}{c}{\textit{p-value}}\\") ///  		
	posthead("\hline") ///
	prefoot("") ///
	postfoot("\\")

// Get obs

estpost tabstat speech, s(N) columns(statistics) listwise  
matrix N = e(count)  
estpost tabstat speech, s(N) columns(statistics) listwise  
matrix N = N, e(count)
estpost tabstat speech, s(N) columns(statistics) listwise  
matrix N = N, e(count)
estpost tabstat speech if speech==1, s(N) columns(statistics) listwise  
matrix N = N, e(count)
estpost tabstat speech if speech==0, s(N) columns(statistics) listwise  
matrix N = N, e(count)
estpost tabstat speech, s(N) columns(statistics) listwise  
matrix N = N, e(count)
estpost tabstat speech, s(N) columns(statistics) listwise  
matrix N = N, e(count)
matrix list N

estout matrix(N, fmt(%9.0fc)) using ///
	"$tables/$program.tex", append style(tex) type label /// 	 
	unstack collabels(none) ///
	mlabels(, none) ///
	msign(--) ///
	varlabels(count "Observations") ///
	prehead("\hline") ///
	posthead("") ///
	prefoot("") ///
	postfoot("\hline" "\hline" ///
	"\end{tabular*}" ///
	"\end{scriptsize}" ///
	"\begin{tablenotes}[normal,flushleft]" ///
	"\begin{scriptsize}" ///
	"\item \emph{Notes}:" /// 
	"All statistics at the county level. " ///
	"Campaign variables come from the University of Nebraska \textit{Railroads and the Making of Modern America - A Digital History Project}. " ///
	"Election data from ICPSR Study no. 1 \citep{ICPSR1999USHER}. " ///
	"Railroad data from \cite{atack2013use}. " ///
	"Population data from NHGIS and IPUMS \citep{manson2021ipums, ruggles2021ipums}. " ///
	"Descriptive statistics for all counties in Column (1). " ///
	"Correlation with the joint Democratic and Populist vote in 1892 in Columns (2) and (3). " ///
	"Characteristics of speech and no-speech counties in Columns (4) and (5), and test of means equality of these two groups in Columns (6) and (7)." ///
	"\end{scriptsize}" ///
	"\end{tablenotes}" ///
	"\end{threeparttable}" ///
	"\end{center}" ///
	"\end{table}")

cap log close
