*************************************
*
* Table C2: Populist vote in Congress
*
*************************************

global program "TableC2"

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_Congress", 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

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)
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=1890(2)1896{
	g speech_`y'=speech*(year==`y')
	la var speech_`y' "`y'" 
}

la var x "1892"

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

// Populist vote (%) 1890-1894 (Column 1)

reghdfe pop_cong_ttl speech_1890 x speech_1894 $X	railroad_dum $geog $demo $econ_p if sample==1 & state_speech==1 & year>=1890 & year<=1894, cluster(county) a(county year_state) // keepsing
estimates store DD_pop_perc
qui sum pop_cong_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
test speech_1890 speech_1894
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

// Populist vote (%) 1892-1894 (Column 2)

reghdfe pop_cong_ttl x speech_1894 $X	railroad_dum $geog $demo $econ_p if sample==1 & state_speech==1 & year>=1892 & year<=1894, cluster(county) a(county year_state) // keepsing
estimates store DD_pop_perc94
qui sum pop_cong_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y

// Populist candidate 1890-1894 (Column 3)

reghdfe pop_dum_cong speech_1890 x speech_1894 $X	railroad_dum $geog $demo $econ_p if sample==1 & state_speech==1 & year>=1890 & year<=1894, cluster(county) a(county year_state) // keepsing
estimates store DD_pop_dum
qui sum pop_dum_cong if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
test speech_1890 speech_1894
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

// Democrat and/or Populist vote (%) 1890-1894 (Column 4)

reghdfe dempop_cong_ttl speech_1890 x speech_1894 $X	railroad_dum $geog $demo $econ_p if sample==1 & state_speech==1 & year>=1890 & year<=1894, cluster(county) a(county year_state) // keepsing
estimates store DD_dempop_perc
qui sum dempop_cong_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
test speech_1890 speech_1894
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

// Democrat and/or Populist vote (%) 1890-1896 (Column 5)

reghdfe dempop_cong_ttl speech_1890 x speech_1894 speech_1896 $X	railroad_dum $geog $demo $econ_p if sample==1 & state_speech==1 & year>=1890 & year<=1896, cluster(county) a(county year_state) // keepsing
estimates store DD_dempop_perc96
qui sum dempop_cong_ttl if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
test speech_1890 speech_1894
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

// Democrat and/or Populist candidate 1890-1894 (Column 6)

reghdfe dempop_dum_cong speech_1890 x speech_1894 $X	railroad_dum $geog $demo $econ_p if sample==1 & state_speech==1 & year>=1890 & year<=1894, cluster(county) a(county year_state) // keepsing
estimates store DD_dempop_dum
qui sum dempop_dum_cong if e(sample)
scalar mean_y=r(mean)
estadd scalar mean_y
test speech_1890 speech_1894
return list
estadd scalar F_joint=r(F)
estadd scalar p_F_joint=r(p)

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

// Fixed effects

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

// Table 

estout DD_pop_perc DD_pop_perc94 DD_pop_dum DD_dempop_perc DD_dempop_perc96 DD_dempop_dum /// 
	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(speech_1890) ///
	order(speech_1890) /// 
	varlabels(speech_1890 "Speech (binary) $ \times $ 1890") /// 
	title(Populist vote in Congress) ///
	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{3}{c}{Populist} &\multicolumn{3}{c}{Democrat and/or Populist}\\" ///	
	"												\cmidrule(lr){2-4} \cmidrule(lr){5-7}" ///		
	"		&\multicolumn{2}{c}{Vote (\%)} &\multicolumn{1}{c}{Candidate} &\multicolumn{2}{c}{Vote (\%)} &\multicolumn{1}{c}{Candidate}\\" ///	
	"												\cmidrule(lr){2-3} \cmidrule(lr){4-4} \cmidrule(lr){5-6} \cmidrule(lr){7-7}" ///		
	"\multicolumn{1}{l}{\textit{Sample years}} &\multicolumn{1}{c}{1890-1894}	&\multicolumn{1}{c}{1892-1894} &\multicolumn{1}{c}{1890-1894} &\multicolumn{1}{c}{1890-1894} 	&\multicolumn{1}{c}{1890-1896} &\multicolumn{1}{c}{1890-1894} 	\\" /// 
	"\hline") ///
	posthead("\\") ///
	prefoot("\\ Speech $ \times $ 1892 (omitted) & 0.000 & 0.000 & 0.000 & 0.000 & 0.000 & 0.000 \\ ") ///
	postfoot("") //	

// Break table for omitted category

estout DD_pop_perc DD_pop_perc94 DD_pop_dum DD_dempop_perc DD_dempop_perc96 DD_dempop_dum /// 
	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(speech_1894 speech_1896) ///
	order(speech_1894 speech_1896) /// 
	varlabels(speech_1894 "\\ Speech (binary) $ \times $ 1894" speech_1896 "[0.5em] Speech (binary) $ \times $ 1896") /// 
	title(Populist vote in Congress) ///
	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_pop_perc DD_pop_perc94 DD_dempop_perc DD_dempop_perc96 DD_pop_dum DD_dempop_dum /// 
	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}_{1890}=\hat{\beta}_{1894}=0) $}" "\multicolumn{1}{l}{p-value $ (\hat{\beta}_{1890}=\hat{\beta}_{1894}=0) $}") fmt(2 2)) ///	
	collabels(none) ///
	mlabels(, span none prefix(\multicolumn{@span}{c}{) suffix(})) ///
	msign(--) starlevels(* .1 ** .05 *** .01) ///
	drop(*) /// 
	prehead("") ///
	posthead("\hline") ///
	prefoot("Joint significance test \\") ///
	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{Joint significance test} is a F-test that the 1890 and 1894 are jointly different from the 1892 coefficient. " ///
	"\end{scriptsize}" ///
	"\end{tablenotes}" ///
	"\end{threeparttable} " ///
	"\end{center}" ///
	"\end{table}") //	

cap log close
