**********************************************
*
* Table C1: Difference-in-differences evidence
*
**********************************************

global program "TableC1"

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

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

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

// Speech (Binary) (Column 1)

preserve

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

// Speech/10k pop (Column 2)

preserve

forvalues y=1880(4)1900{
	g dep_var_`y'=speeches_10k*(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_count_con
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

// Speech/10k pop and squared (Column 3)

preserve

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

g x2=1

reghdfe dempop_pres_ttl dep_var_1884 dep_var_1888 x dep_var_1896 dep_var_1900 dep_var2_1884 dep_var2_1888 x2 dep_var2_1896 dep_var2_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_countsq_con
qui sum dempop_pres_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
test dep_var_1884 dep_var_1888 dep_var2_1884 dep_var2_1888
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

restore

// Distance (log km) (Column 4)

preserve

forvalues y=1880(4)1900{
	g dep_var_`y'=ldist_speech_bird*(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_dist_con
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

// Speech (Binary) Rep vote (Column 5)

preserve

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

reghdfe rep_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_rep
qui sum rep_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

// Speech (Binary) Turnout (Column 7)

preserve

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

reghdfe ttl_pres_tev 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_tev
qui sum ttl_pres_tev 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

// Speech (false) Dem vote (Column 6)

global geog "railroad_dens ldist_capital ldist_largest" // 

sort county year
cap g id=_n
cap g nospeech=1*(speech==0 & state_speech==1)

cap drop nn*
teffects nnmatch (dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $econ) (speech) if year==1896 & railroad_dum==1 & state_speech==1, ///
	metric(euclidean) biasadj(lag_dem_pres_ttl lag_pop_pres_ttl $demo_cont $geog $econ) vce(robust) nneighbor(1) generate(nn)

cap drop nn*
cap teffects psmatch (dempop_pres_ttl) (speech lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $eco) if year==1896 & railroad_dum==1 & state_speech==1, ///
	vce(robust) nneighbor(1) gen(nn) osample(problem1)
cap drop nn*
teffects psmatch (dempop_pres_ttl) (speech lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $eco) if year==1896 & railroad_dum==1 & state_speech==1 & problem1!=1, ///
	vce(robust) nneighbor(1) gen(nn)

recode state_speech (0=1) (1=0), g(nostate_speech)

sort id

cap drop nn*
cap drop problem*
teffects nnmatch (dempop_pres_ttl lag_dem_pres_ttl lag_pop_pres_ttl $demo $geog $econ) (nostate_speech) if year==1896 & railroad_dum==1, ///
	metric(euclidean) biasadj(lag_dem_pres_ttl lag_pop_pres_ttl $demo_cont $geog $econ) vce(robust) nneighbor(1) generate(nn) osample(problem1) // caliper(1)

cap predict dist, dist
sum dist
g _pdif=dist/100

tempfile  tmp
save `tmp.dta', replace

use `tmp.dta', clear

keep id nn _pdif
keep if nn!=.

rename id id1
rename nn id

merge m:1 id using `tmp.dta', keepusing(speech)
drop if _merge==2

rename id nn
rename id1 id
drop _merge

rename speech f_s

merge 1:1 id using `tmp.dta'
bysort county: egen max=max(_merge)
drop if max==2
keep if nostate_speech==1

bysort county: egen f_speech=min(f_s)
bysort county: egen p_simil=min(_pdif)
replace p_simil=1-p_simil
drop _merge max

cap g x=1
forvalues y=1880(4)1900{
	cap drop dep_var_`y'
	g dep_var_`y'=f_speech*(year==`y')
}

la var x "1892"

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 state_speech==0 & year>1880 & year<=1900, cluster(county) a(county year_state)
estimates store DD_binary_false
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)
test dep_var_1884 dep_var_1888 dep_var_1896 dep_var_1900
return list
estadd scalar F_joint_all=r(F)
estadd scalar p_F_joint_all=r(p)

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

// Fixed effects

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

// Table 

estout DD_binary_con DD_count_con DD_countsq_con DD_dist_con DD_binary_rep DD_binary_false DD_binary_tev /// 
	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 $ \times $ 1884" dep_var_1888 "[0.5em] Speech $ \times $ 1888") /// 
	title(Difference-in-differences evidence) ///
	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}{l}{\textit{Dep. Variable}} 	&\multicolumn{4}{c}{Democratic vote (\%)}	&\multicolumn{1}{c}{Rep (\%)}	&\multicolumn{1}{c}{Dem (\%)}	&\multicolumn{1}{c}{Turnout (\%)}\\" ///
	"												\cmidrule(lr){2-5} 	\cmidrule(lr){6-6} 	\cmidrule(lr){7-7} \cmidrule(lr){8-8}" ///
	"\multicolumn{1}{l}{\textit{Speech variable}} 	&\multicolumn{1}{c}{Speech}  	&\multicolumn{1}{c}{Speeches/} 	&\multicolumn{1}{c}{Speeches/} 	&\multicolumn{1}{c}{Distance}	&\multicolumn{1}{c}{Speech}  	&\multicolumn{1}{c}{Speech} 	&\multicolumn{1}{c}{Speech}  	\\" ///
	"												&\multicolumn{1}{c}{(binary)}	&\multicolumn{1}{c}{10k pop} 	&\multicolumn{1}{c}{10k pop} 	&\multicolumn{1}{c}{(log km)}	&\multicolumn{1}{c}{(binary)}	&\multicolumn{1}{c}{(false)}	&\multicolumn{1}{c}{(binary)}	\\" ///
	"\hline") ///
	posthead("\\") ///
	prefoot("\\ Speech $ \times $ 1892 (omitted) & 0.000 & 0.000 & 0.000 & 0.000 & 0.000 & 0.000 & 0.000 \\ ") ///
	postfoot("") //	

// Break table for ommited categories

estout DD_binary_con DD_count_con DD_countsq_con DD_dist_con DD_binary_rep DD_binary_false DD_binary_tev /// 
	using "$tables/$program.tex", append 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_1896 dep_var_1900 dep_var2_1884 dep_var2_1888) /// 
	order(dep_var_1896 dep_var_1900 dep_var2_1884 dep_var2_1888) /// 
	varlabels(dep_var_1896 "\\ Speech $ \times $ 1896" dep_var_1900 "[0.5em] Speech $ \times $ 1900" ///
	dep_var2_1884 "Speech squared $ \times $ 1884" dep_var2_1888 "[0.5em] Speech squared $ \times $ 1888") /// 
	prehead("") ///
	posthead("") ///
	prefoot("\\ Speech sq. $ \times $ 1892 (omitted) & 	 & 	 & 0.000 & 	 & 	 & 	 & 	 \\ ") ///
	postfoot("")

// Break table for ommited categories

estout DD_binary_con DD_count_con DD_countsq_con DD_dist_con DD_binary_rep DD_binary_false DD_binary_tev /// 
	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_var2_1896 dep_var2_1900) /// 
	order(dep_var2_1896 dep_var2_1900) /// 
	varlabels(dep_var2_1896 "\\ Speech squared $ \times $ 1896" dep_var2_1900 "[0.5em] Speech squared $ \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_con DD_count_con DD_countsq_con DD_dist_con DD_binary_rep DD_binary_false DD_binary_tev /// 
	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
