*******************************************
*
* Table C4: Sensitivity to sample selection
*
*******************************************

global program "TableC4"

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"

// Interactions

egen year_state=group(year state)

// Control vectors

global X "c.perc_farm#i.year c.perc_manuf#i.year" 

// Figure for paper

cap g x=1
forvalues y=1880(4)1900{
	g speech_`y'=speech*(year==`y')
	la var speech_`y' "`y'" 
}

la var x "1892"

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

// Baseline sample (Column 1)

preserve

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)
g s=1*(e(sample))
bysort county: egen sample=max(s)
drop s

forvalues y=1880(4)1900{
	g dep_var_`y'=speech*(year==`y')
}

reghdfe dempop_pres_ttl dep_var_1884 dep_var_1888 x dep_var_1896 dep_var_1900 $X railroad_dum $geog $demo $econ_p if sample==1 & state_speech==1 & year>1880 & year<=1900, cluster(county) a(county year_state)
estimates store DD_binary_baseline
qui sum dempop_pres_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
test dep_var_1884 dep_var_1888
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

restore

// Excluding planned (Column 2)

preserve

cap drop sample
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)
g s=1*(e(sample))
bysort county: egen sample=max(s)
drop s

forvalues y=1880(4)1900{
	g dep_var_`y'=speech*(year==`y')
}

reghdfe dempop_pres_ttl dep_var_1884 dep_var_1888 x dep_var_1896 dep_var_1900 $X railroad_dum $geog $demo $econ_p if sample==1 & state_speech==1 & year>1880 & year<=1900, cluster(county) a(county year_state)
estimates store DD_binary_noplan
qui sum dempop_pres_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
test dep_var_1884 dep_var_1888
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

restore

// Excluding not planned (Column 3)

preserve

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

cap drop sample
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) //
g s=1*(e(sample))
bysort county: egen sample=max(s)
drop s

forvalues y=1880(4)1900{
	g dep_var_`y'=speech*(year==`y')
}

reghdfe dempop_pres_ttl dep_var_1884 dep_var_1888 x dep_var_1896 dep_var_1900 $X railroad_dum $geog $demo $econ_p if sample==1 & state_speech==1 & year>1880 & year<=1900, cluster(county) a(county year_state)
estimates store DD_binary_plan
qui sum dempop_pres_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
test dep_var_1884 dep_var_1888
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

restore

// Excluding neighbors (Column 4)

preserve

cap drop sample
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) //
g s=1*(e(sample))
bysort county: egen sample=max(s)
drop s

forvalues y=1880(4)1900{
	g dep_var_`y'=speech*(year==`y')
}

reghdfe dempop_pres_ttl dep_var_1884 dep_var_1888 x dep_var_1896 dep_var_1900 $X railroad_dum $geog $demo $econ_p if sample==1 & state_speech==1 & year>1880 & year<=1900, cluster(county) a(county year_state) // keepsing
estimates store DD_binary_nonbr
qui sum dempop_pres_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
test dep_var_1884 dep_var_1888
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

restore

// Including no-railroad (Column 5)

preserve

cap drop sample
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
g s=1*(e(sample))
bysort county: egen sample=max(s)
drop s

forvalues y=1880(4)1900{
	g dep_var_`y'=speech*(year==`y')
}

reghdfe dempop_pres_ttl dep_var_1884 dep_var_1888 x dep_var_1896 dep_var_1900 $X railroad_dum $geog $demo $econ_p if sample==1 & state_speech==1 & year>1880 & year<=1900, cluster(county) a(county year_state)
estimates store DD_binary_rr
qui sum dempop_pres_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
test dep_var_1884 dep_var_1888
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

restore

// Full sample (Column 6)

preserve

cap drop sample
reghdfe dempop_pres_ttl speech lag_dem_pres_ttl lag_pop_pres_ttl railroad_dum $demo $geog $econ  if year==1896, cluster(district) noabsorb
g s=1*(e(sample))
bysort county: egen sample=max(s)
drop s

forvalues y=1880(4)1900{
	g dep_var_`y'=speech*(year==`y')
}

reghdfe dempop_pres_ttl dep_var_1884 dep_var_1888 x dep_var_1896 dep_var_1900 $X railroad_dum $geog $demo $econ_p if sample==1 & year>1880 & year<=1900, cluster(county) a(county year_state) // keepsing
estimates store DD_binary_full
qui sum dempop_pres_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
test dep_var_1884 dep_var_1888
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

restore

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

// Fixed effects

estfe . DD*, labels(county "County FE" year_state "Year $ \times $ State FE")
return list

// Table 

estout DD_binary_baseline DD_binary_noplan DD_binary_plan DD_binary_nonbr DD_binary_rr DD_binary_full /// 
	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(dep_var_1884 dep_var_1888) ///
	order(dep_var_1884 dep_var_1888) /// 
	varlabels(dep_var_1884 "Speech (binary) $ \times $ 1884" dep_var_1888 "[0.5em] Speech (binary) $ \times $ 1888") /// 
	title(Sensitivity to sample selection) ///
	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}{l}{\textit{Dep. Variable}} &\multicolumn{6}{c}{Democratic vote (\%) in 1896} \\" ///
	"\cmidrule(lr){2-7}   " ///
	"\multicolumn{1}{l}{\textit{Sample}} &\multicolumn{1}{c}{Baseline}  &\multicolumn{1}{c}{Excluding} 	&\multicolumn{1}{c}{Excluding} 	&\multicolumn{1}{c}{Excluding}	&\multicolumn{1}{c}{Including}  &\multicolumn{1}{c}{Full}  \\" ///
	"&\multicolumn{1}{c}{sample} &\multicolumn{1}{c}{planned} &\multicolumn{1}{c}{not planned}  &\multicolumn{1}{c}{neighbors} &\multicolumn{1}{c}{no-railroad} &\multicolumn{1}{c}{sample}  \\" ///
	"\hline") ///
	posthead("\\") ///
	prefoot("\\ Speech $ \times $ 1892 (omitted) & 0.000 & 0.000 & 0.000 & 0.000 & 0.000 & 0.000 \\ ") ///
	postfoot("") //	

// Break table for ommited categories

estout DD_binary_baseline DD_binary_noplan DD_binary_plan DD_binary_nonbr DD_binary_rr DD_binary_full /// 
	using "$tables/$program.tex", append 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 %9.0fc 2)) ///
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	keep(dep_var_1896 dep_var_1900) ///
	order(dep_var_1896 dep_var_1900) /// 
	varlabels(dep_var_1896 "\\ Speech (binary) $ \times $ 1896" dep_var_1900 "[0.5em] Speech (binary) $ \times $ 1900") /// 
	indicate( ///
	"\\ Full controls = lpop_total" ///	
	"Year $ \times $ State FE = 0.year_state" ///	
	"Year $ \times $ Farmers (\%) = *.year#c.perc_farm" ///	
	"Year $ \times $ Workers (\%) = *.year#c.perc_manuf" ///
	"County FE = 0.county" ///	
	, labels(Yes )) ///
	prehead("") ///
	posthead("") ///
	prefoot("\hline") ///
	postfoot("") //	

// Joint significance test
	
estout DD_binary_baseline DD_binary_noplan DD_binary_plan DD_binary_nonbr DD_binary_rr DD_binary_full /// 
	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}_{1884}=\hat{\beta}_{1888}=0) $}" "\multicolumn{1}{l}{p-value $ (\hat{\beta}_{1884}=\hat{\beta}_{1888}=0) $}") fmt(2 2)) ///
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	drop(*) /// 
	prehead("") ///
	posthead("\hline") ///
	prefoot("Pre-trends joint 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 $ \times $ election. " ///
	"Time-varying controls: Year $ \times $ Farmers (\%), Year $ \times $ Manufacture workers (\%). " ///
	"All regressions include the full set of controls presented in Table \ref{Table1}, except time invariant geographical characteristics. " ///
	"All regressions include Year $ \times $ State fixed-effects and county fixed-effects. " ///
	"Standard errors clustered at the county level in parentheses." /// 
	"* significant at 10\%; ** at 5\%; *** at 1\%. " ///
	"\textit{Pre-trends joint significance} is a F-test that the 1884 and 1888 are jointly different from the 1892 coefficient. " ///
	"\end{scriptsize}" ///
	"\end{tablenotes}" ///
	"\end{threeparttable} " ///
	"\end{center}" ///
	"\end{table}") //	

cap log close
