*******************************************
*
* Table B4: Sensitivity to sample selection
*
*******************************************
 
global program "TableB4"

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

// Import neighbors

use "$data/Data_Neighbor_Pairs", clear

bysort nbr_fe : egen total_speech = total(speech) if year == 1896
gen speech_nbr = (total_speech > 0  & speech == 0) if year == 1896

keep if year == 1896 & speech_nbr == 1

keep county speech_nbr
duplicates drop

tempfile speech_neighbors
save `speech_neighbors', replace

// Import data

use "$data/Data_County", clear

// Add neigbors

merge m:1 county using `speech_neighbors'

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

// Excluding planned (Column 1)

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 & planned!=1, cluster(district) a(state) // keepsing
estimates store cs_binary_noplan
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 lag_dem_pres_ttl=lag_pop_pres_ttl
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

// Excluding not planned (Column 2)

recode planned (0=.) if speech==1

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 & planned!=., cluster(district) a(state) //
estimates store cs_binary_plan
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 lag_dem_pres_ttl=lag_pop_pres_ttl
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

recode planned (.=0) if speech==1

// Excluding >1 speeches (Column 3)

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 & speeches<=1, cluster(district) a(state) // keepsing
estimates store cs_binary_onespeech
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 lag_dem_pres_ttl=lag_pop_pres_ttl
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

// Excluding neighbours (Column 4)

reghdfe dempop_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl  $demo $geog $econ  if speech_nbr!=1 & year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state) //
estimates store cs_binary_nonbr
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 lag_dem_pres_ttl=lag_pop_pres_ttl
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

// Including no-railroad (Column 5)

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

// Full sample (Column 6)

reghdfe dempop_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl railroad_dum $demo $geog $econ  if year==1896, cluster(district) noabsorb
estimates store cs_binary_fullnofe
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 lag_dem_pres_ttl=lag_pop_pres_ttl
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

// No-speech states (Column 7)

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

// Excluding populist strongholds (Column 8)


g pop_dem=ln(lag_pop_pres_ttl)-ln(lag_dem_pres_ttl) if year==1896
sum pop_dem if year==1896 & state_speech==1 & railroad_dum==1

sum pop_dem if year==1896 & state_speech==1 & railroad_dum==1, d
g limit=r(mean)+r(sd)

g pop_strong=1*(pop_dem>limit)

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 & pop_strong==0, cluster(district) a(state) // keepsing
estimates store cs_binary_pop
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 lag_dem_pres_ttl=lag_pop_pres_ttl
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") // year "Year FE")
return list

// Labels

la var lag_dem_pres_ttl 	"[0.3em] 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 (\%)" // male above 21 
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)"

// Table 

estout cs_binary_noplan cs_binary_plan cs_binary_onespeech cs_binary_nonbr cs_binary_rr cs_binary_fullnofe cs_binary_nospeech cs_binary_pop  ///
	using "$tables/$program.tex", replace style(tex) type label ///
	cells(b(star fmt(%9.3f)) 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 lpop_total urban_dum perc_farm perc_manuf railroad_dum) ///
	order(speech lag_dem_pres_ttl lag_pop_pres_ttl lpop_total urban_dum perc_farm perc_manuf railroad_dum) ///
	varlabels(speech "Speech (binary)" lag_dem_pres_ttl "[0.5em] Dem (\%) in 1892" lag_pop_pres_ttl "[0.5em] Pop (\%) in 1892" lpop_total "[0.5em] Population (log)" urban_dum	"[0.5em] Urban county" perc_farm "[0.5em] Farmers (\%)" perc_manuf "[0.5em] Workers (\%)" railroad_dum "[0.5em] Railroad (binary)") ///
	title(Sensitivity to sample selection) ///
	indicate("\\ Demo controls = perc_pop_dstr" ///	
	"Geographic controls = county_lat" ///	
	"Economic controls = dlog_avbasket" ///	
	"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}{l}{\textit{Dep. Variable}} &\multicolumn{8}{c}{Democratic vote (\%) in 1896} \\" ///
	"\cmidrule(lr){2-9}   " ///
	"\multicolumn{1}{l}{\textit{Sample}} &\multicolumn{1}{c}{Excl.} 	&\multicolumn{1}{c}{Excl. not} 	&\multicolumn{1}{c}{Excl. >1}  &\multicolumn{1}{c}{Excl.}	&\multicolumn{1}{c}{Incl. no}  &\multicolumn{1}{c}{Full}  		&\multicolumn{1}{c}{No-speech} &\multicolumn{1}{c}{Excl.}  \\" ///
	"&\multicolumn{1}{c}{planned} &\multicolumn{1}{c}{planned}  &\multicolumn{1}{c}{speeches} &\multicolumn{1}{c}{neighbors} &\multicolumn{1}{c}{railroad} &\multicolumn{1}{c}{sample}  &\multicolumn{1}{c}{states} &\multicolumn{1}{c}{Pop strong} \\" ///
	"\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
