**************************
*
* Table D5: Quantification
*
**************************

global program "TableD5"

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. TABLE RESULTS *****
****************************

// 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)
keep if sample==1
drop s sample

// Difference-in-Differences

egen year_state=group(year state)

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

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

la var x "1892"

g dep_var=speech_1896

reghdfe dempop_pres_ttl speech_1884 speech_1888 x dep_var speech_1900 $X	railroad_dum $geog $demo $econ_p if year>1880 & year<=1900, cluster(county) a(county year_state) // keepsing
g sample=1*(e(sample))
predict dem_real, xb
replace dep_var=dep_var*(1/40)
predict dem_nospeech, xb

g dif_dd=dem_real-dem_nospeech
replace dif_dd=. if sample==0
g ttl_dd=((100-dif_dd)/100)*ttl_pres-ttl_pres

drop dep_var sample dem_real dem_nospeech

// Within-state cross-section

g dep_var=speech

reghdfe dempop_pres_ttl dep_var 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
g sample=1*(e(sample))
predict dem_real, xb
replace dep_var=dep_var*(1/40)
predict dem_nospeech, xb

g dif_cs=dem_real-dem_nospeech
replace dif_cs=. if sample==0
g ttl_cs=((100-dif_cs)/100)*ttl_pres-ttl_pres

drop dep_var sample dem_real dem_nospeech

tempfile  tmp
save `tmp.dta', replace

// Aggregates

use "$data/Data_State", clear

keep if year==1896

format margin_vote dem_vote  %9.1f

// Total electoral college

estpost tabstat college, s(sum) // 
matrix col=e(sum) // e(Sum) // For Stata 17
matrix list col

// Total vote and margin

replace margin_vote=margin_vote/1000
replace dem_vote=dem_vote/1000

estpost tabstat dem_vote margin_vote, s(sum)
matrix tot_vote=e(dem_vote)', e(margin_vote)'
matrix list tot_vote

// Total vote percentage and margin percentage

g x=1
estpost tabstat dempop_pres_ttl margin_perc [w=tot_vote], s(mean) by(x) nototal
matrix perc_vote=e(dempop_pres_ttl)', e(margin_perc)'
matrix list perc_vote

// Counterfactual total

merge 1:m state_abb year using `tmp.dta'
drop if _merge==1 // States with no speech
drop _merge

tempfile  tmp
save `tmp.dta', replace

drop if year!=1896

foreach i in dd cs{
	replace dif_`i'=-dif_`i'
	recode dif_`i' (.=0)
	format dif_`i' %9.1f
}

// Total speeches

estpost tabstat speeches, s(sum) by(state) nototal 
matrix counter=e(sum)' // e(Sum)' // For Stata 17
matrix list counter

// State level stats

estpost tabstat college dempop_pres_ttl margin_perc dif_dd dif_cs [w=ttl_pres], s(mean) by(state) nototal 
matrix m=e(college)', e(dempop_pres_ttl)', e(margin_perc)', e(dif_dd)', e(dif_cs)' 
matrix list m
matrix counter=counter, m
matrix list counter

// Totals

estpost tabstat speeches, s(sum)
matrix spe=e(sum) // e(Sum) // For Stata 17
matrix list spe

estpost tabstat ttl_dd ttl_cs, s(sum)

foreach i in dd cs{
	replace ttl_`i'=ttl_`i'/1000
}

estpost tabstat ttl_dd ttl_cs, s(sum)
matrix ttl=e(ttl_dd)', e(ttl_cs)'

estpost tabstat dif_dd dif_cs [w=ttl_pres], s(mean) by(x) nototal
matrix dif=e(dif_dd)', e(dif_cs)'

matrix ttl=spe, col, tot_vote, ttl
matrix list ttl

matrix dif=spe, col, perc_vote, dif
matrix list dif

// Counterfactual electoral college

use `tmp.dta', clear

drop if year!=1896

bysort state: egen speech_sum=sum(speeches)

collapse (mean) dif_dd dif_cs [w=ttl_pres], by(state_abb state college speech_sum year) 

tempfile  tmp
save `tmp.dta', replace

use "$data/Data_State", clear

merge 1:1 state_abb year using `tmp.dta'
drop if _merge==1
drop _merge

g margin_dem=college_dem-(college-college_dem)

foreach i in dd cs{
	g dem_`i'=dempop_pres_ttl-dif_`i'
	g rep_`i'=rep_pres_ttl+dif_`i'
	format rep_`i' dem_`i' %9.1f
	drop dif_`i'
}

format dempop_pres_ttl rep_pres_ttl %9.1f

foreach i in dd cs{
	g col_`i'=college*(dem_`i'>rep_`i')
}

foreach i in dd cs{
	replace col_`i'=col_`i'-college_dem
	replace col_`i'=0 if speech_sum==0
	replace col_`i'=0 if state_abb=="KY"
}

estpost tabstat speech_sum college college_dem margin_dem col_dd col_cs, s(sum) 
matrix college=e(speech_sum)', e(college)', e(college_dem)', e(margin_dem)', e(col_dd)', e(col_cs)' 
matrix list college

tempfile  tmp
save `tmp.dta', replace

// House representatives

use "$data/Data_Congress", clear

drop if year==1888

xtset county election

reghdfe dempop_pres_ttl speech l2.dem_pres_ttl l2.pop_pres_ttl	$demo $geog $econ if year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state) // keepsing
g s=1*(e(sample))
bysort county: egen sample=max(s)
keep if sample==1
drop s sample

// Difference-in-Differences

egen year_state=group(year state)

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

cap g x=1
forvalues y=1890(2)1896{
	g speech_`y'=speech*(year==`y')
	la var speech_`y' "`y'" // Speech # 
}

la var x "1892"

g dep_var=speech_1896

reghdfe dempop_cong_ttl speech_1890 x speech_1894 dep_var $X	railroad_dum $geog $demo $econ_p if state_speech==1 & year>=1890 & year<=1896, cluster(county) a(county year_state)
g sample=1*(e(sample))
predict dem_real, xb
replace dep_var=dep_var*(1/40)
predict dem_nospeech, xb

g dif_dd=dem_real-dem_nospeech
replace dif_dd=. if sample==0

drop dep_var sample dem_real dem_nospeech

// Within-state cross-section

g dep_var=speech

reghdfe dempop_cong_ttl dep_var lag_dem_cong_ttl lag_pop_cong_ttl lag2_dem_cong_ttl lag2_pop_cong_ttl	$demo $geog $econ if year==1896 & state_speech==1 & railroad_dum==1, cluster(district) a(state)
g sample=1*(e(sample))
predict dem_real, xb
replace dep_var=dep_var*(1/40)
predict dem_nospeech, xb

g dif_cs=dem_real-dem_nospeech
replace dif_cs=. if sample==0

drop if year!=1896

keep state_abb state district county year ttl_cong rep_cong_ttl dempop_cong_ttl dif_dd dif_cs speeches

foreach i in dd cs{
	g dem_`i'=dempop_cong_ttl-dif_`i'
	replace dem_`i'=dempop_cong_ttl if dif_`i'==.
	g rep_`i'=rep_cong_ttl+dif_`i'
	replace rep_`i'=rep_cong_ttl if dif_`i'==.
	format rep_`i' dem_`i' %9.1f
	drop dif_`i'
}

bysort district: egen speech_sum=sum(speeches)

rename dempop_cong_ttl dem_cong_ttl

collapse (mean) dem_* rep_* [w=ttl_cong], by(state_abb state district speech_sum)

// House representatvies

g hou_real=1*(dem_cong_ttl>rep_cong_ttl)

foreach i in dd cs{
	g hou_`i'=1*(dem_`i'>rep_`i')
}

foreach i in dd cs{
	replace hou_`i'=hou_`i'-hou_real
}

// Collapse by state

merge m:1 state_abb using `tmp.dta'
drop if _merge==1
drop _merge

g house=357/286
g margin_hou=-60/286
drop hou_real
g hou_real=146/286
drop speech_sum
g speech_sum=744/286

estpost tabstat speech_sum house margin_hou hou_*, s(sum)
matrix house=e(speech_sum)', e(house)', e(hou_real)', e(margin_hou)', e(hou_dd)', e(hou_cs)'
matrix list house

***************************
***** 2. EXPORT TABLE *****
***************************

estout matrix(counter, fmt(0 0 1 1 2 2 2 2)) using ///
	"$tables/$program.tex", replace style(tex) type label /// 	 
	unstack collabels(none) ///
	mlabels(, none) ///
	msign(--) ///
	title(Quantification) ///
	prehead("\begin{table}[h!]" ///
	"\begin{center}" ///
	"\begin{threeparttable}" ///
	"\begin{scriptsize}" ///
	"\caption{\small{@title}}" ///
	"\label{$program}"  /// 
	"\begin{tabular*}{\columnwidth}{@{\extracolsep{\stretch{1}}}l*{6}{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{4}{c}{Actual outcomes}		&\multicolumn{2}{c}{Counterfactual vote if no campaign}  		\\" ///  		
	"	\cmidrule(lr){2-5}	\cmidrule(lr){6-7}" ///
	"	&\multicolumn{1}{c}{Speeches}	&\multicolumn{1}{c}{Electoral}	&\multicolumn{1}{c}{Democratic} &\multicolumn{1}{c}{Democratic} &\multicolumn{1}{c}{Difference-in-}	&\multicolumn{1}{c}{Within-state}\\" /// 
	"	&\multicolumn{1}{c}{}			&\multicolumn{1}{c}{votes}		&\multicolumn{1}{c}{vote}		&\multicolumn{1}{c}{margin}		&\multicolumn{1}{c}{differences}			&\multicolumn{1}{c}{cross-section}	\\") ///			
	posthead("\hline") ///
	prefoot("") ///
	postfoot("\hline \hline")

estout matrix(ttl, fmt(0 0 1 1 1 1 1 1)) using ///
	"$tables/$program.tex", append style(tex) type label /// 	 
	unstack collabels(none) ///
	mlabels(, none) ///
	msign(--) ///
	varlabels(sum "Popular vote (1,000)") //

estout matrix(dif, fmt(0 0 1 1 2 2 2 2)) using ///
	"$tables/$program.tex", append style(tex) type label /// 	 
	unstack collabels(none) ///
	mlabels(, none) ///
	msign(--) ///
	varlabels(sum "Popular vote (\%)") //

estout matrix(college, fmt(0)) using ///
	"$tables/$program.tex", append style(tex) type label /// 	 
	unstack collabels(none) ///
	mlabels(, none) ///
	msign(--) ///
	varlabels(sum "Electoral college") //

estout matrix(house, fmt(0)) using ///
	"$tables/$program.tex", append style(tex) type label /// 	 
	unstack collabels(none) ///
	mlabels(, none) ///
	msign(--) ///
	title(Summary Statistics) ///
	varlabels(sum "House representatives") ///
	prehead("") ///
	posthead("") ///
	prefoot("") ///
	postfoot("\hline" "\hline" ///
	"\end{tabular*}" ///
	"\end{scriptsize}" ///
	"\begin{tablenotes}[normal,flushleft]" ///
	"\begin{scriptsize}" ///
	"\item \emph{Notes}: Counterfactual vote in the absence of campaign speeches, based on the results of Tables \ref{Table2} and \ref{TableC1} (Presidential) and Tables \ref{TableB7} and \ref{TableC2} (Congress). " /// 
	"Columns (1) to (4): actual outcomes; " ///
	"Column (5) presents the counterfactual vote in the absence of the campaign using the difference-in-differences point estimates; " ///
	"Column (6) using the within-state cross-sectional point estimates. " ///
	"\end{scriptsize}" ///
	"\end{tablenotes}" ///
	"\end{threeparttable}" ///
	"\end{center}" ///
	"\end{table}")

cap log close
