global program "EEREV-D-21-00004_Figure4"

cap log close
cap log using "$log\log_$program$today", replace

**** REPLICATION OF FIGURE 4 ****

clear all
set mem 2g
set more off

/*	
	OUTLINE
	1. PREPARE DATA
	2. MARGINAL EFFECTS
*/

***************************
***** 1. PREPARE DATA *****
***************************

// Import Municipality data

use $data\EEREV-D-21-00004_Data_Municipalities.dta, clear

// Reshape

drop if sample==0
drop sample

reshape long voix_, i(com dep annee) j(candidate) string
drop if voix_==.
rename voix_ voix

// Merge with Candidate data

merge m:1 annee candidate using $data\EEREV-D-21-00004_Data_Candidates
tab candidate annee if _merge==1
rename _merge sample

recode party (.=-999) if sample==1
recode cand_year (.=-999) if sample==1

// Generate vote shares

g perc_exp=(voix*100)/exprimes 
g perc_ins=(voix*100)/inscrits
format perc_* %9.2f

// Get Inverse polar for abstention (time invariant)

foreach var in neg nat aut{
	bysort annee: egen min=min(inv_`var'_P)
	replace inv_`var'_P=min if sample==1
	drop min
}

// Fixed effects

egen com_year=group(com annee)
egen com_party=group(com party)
egen cand_year_border=group(cand_year borderfe)
egen party_year_border=group(party annee borderfe)

// Macros

global ctrl_neg_C 			"c.lpop_counter#c.neg_C c.perc_male_counter#c.neg_C c.perc_disp_1946#c.neg_C" 
global ctrl_inv_neg_P		"c.lpop_counter#c.inv_neg_P c.perc_male_counter#c.inv_neg_P c.perc_disp_1946#c.inv_neg_P" 

global pop 					"perc_b_1928_1945 perc_b_1946_1964 perc_b_1965_1980 perc_b_1981_1996 perc_male_counter"
global demog_chara 			"lpop_counter perc_fore $pop"
global socec_aspec 			"perc_bacplus perc_bluecollar perc_chom lrev_median lrev_sd"
global w 					"$demog_chara $socec_aspec"

*******************************
***** 2. MARGINAL EFFECTS *****
*******************************

cap drop trend
g trend=party_year_border

// Replicate Column (4) result using reghdfe (ivreghdfe does not work with margins)

reghdfe perc_ins ///
	c.perc_b_1903_1912#c.neg_C c.perc_b_1913_1922#c.neg_C c.perc_b_1923_1932#c.neg_C /// 
	c.perc_b_1903_1912#c.aut_C c.perc_b_1913_1922#c.aut_C c.perc_b_1923_1932#c.aut_C /// 
	c.perc_b_1903_1912#c.nat_C c.perc_b_1913_1922#c.nat_C c.perc_b_1923_1932#c.nat_C /// 
	c.perc_b_1903_1912#c.inv_neg_P c.perc_b_1913_1922#c.inv_neg_P c.perc_b_1923_1932#c.inv_neg_P ///
	c.perc_b_1903_1912#c.inv_aut_P c.perc_b_1913_1922#c.inv_aut_P c.perc_b_1923_1932#c.inv_aut_P ///
	c.perc_b_1903_1912#c.inv_nat_P c.perc_b_1913_1922#c.inv_nat_P c.perc_b_1923_1932#c.inv_nat_P ///
	$w /// 
	$ctrl_neg_C $ctrl_inv_neg_P ///
	(c.perc_i_full#c.neg_C c.perc_i_full#c.inv_neg_P ///
	c.perc_i_full#c.aut_C c.perc_i_full#c.inv_aut_P ///
	c.perc_i_full#c.nat_C c.perc_i_full#c.inv_nat_P ///
	= c.perc_e_1908_1927#c.neg_C c.perc_e_1908_1927#c.inv_neg_P ///
	c.perc_e_1908_1927#c.aut_C c.perc_e_1908_1927#c.inv_aut_P ///
	c.perc_e_1908_1927#c.nat_C c.perc_e_1908_1927#c.inv_nat_P) ///
	if sample==3, cluster(com_year) a(com_party trend) old

margins, dydx(perc_i_full) at(neg_C=(0(.2)0.6) inv_neg_P=(10.365172 47.374409)) atmeans
marginsplot, ///
	ciopts(lcolor(gs12)) plot1opts(mcolor(navy) lcolor(navy)) plot2opts(mcolor(navy*.5) lcolor(navy*.5) lpattern(dash)) ///
	xtitle("Candidate radicalism") xlabel(0(.2).6, format(%3.1f)) xscale(r(-.025 .625)) ///
	yline(0, lpattern(dash)) ytitle("Marginal effect of conscription (95% C.I.)") ylabel(-1.2(.4)0.8, format(%3.1f) angle(0)) yscale(r(-1.4 1)) ///	
	title("Candidate vote share", ring(0)) graphregion(color(white)) /// 
	legend(lab(3 "High polarization (2002)") lab(4 "Low polarization (1965)") lab(1 "95% C.I.") r(1) pos(6) ring(0) order(3 4) region(col(white)) symx(*0.5) size(small)) ///
	name(vote, replace)
 
 // Replicate Column (6) result using reghdfe (ivreghdfe does not work with margins)

 reghdfe perc_ins ///
	c.perc_b_1903_1912#c.inv_neg_P c.perc_b_1913_1922#c.inv_neg_P c.perc_b_1923_1932#c.inv_neg_P ///
	c.perc_b_1903_1912#c.inv_aut_P c.perc_b_1913_1922#c.inv_aut_P c.perc_b_1923_1932#c.inv_aut_P ///
	c.perc_b_1903_1912#c.inv_nat_P c.perc_b_1913_1922#c.inv_nat_P c.perc_b_1923_1932#c.inv_nat_P ///
	$w /// 
	$ctrl_neg_P $ctrl_inv_neg_P ///
	(c.perc_i_full#c.inv_neg_P ///
	c.perc_i_full#c.inv_aut_P ///
	c.perc_i_full#c.inv_nat_P ///
	= c.perc_e_1908_1927#c.inv_neg_P ///
	c.perc_e_1908_1927#c.inv_aut_P ///
	c.perc_e_1908_1927#c.inv_nat_P) ///
	if sample==1, cluster(com) a(com_party trend) old

// Recode years for more space

tostring annee, g(annee_str)
cap recode annee (1969=1971) (1974=1977) (1995=1991) (2002=1999) (2007=2005) (2012=2011), g(annee_tmp)
labmask annee_tmp, values(annee_str)

margins, dydx(perc_i_full) atmeans over(annee_tmp) // 
marginsplot, ///
	ciopts(lcolor(gs12)) plotopts(mcolor(navy) lcolor(navy)) ///
	xtitle("Election year") xlabel(, format(%3.0f)) xscale(r(1962.4 2019.6)) ///
	yline(0, lpattern(dash)) ytitle("Marginal effect of conscription (95% C.I.)") ///
	ylabel(0(1)5, format(%3.0f) angle(0)) yscale(r(-.5 5.5)) ///
	title("Abstention", ring(0)) graphregion(color(white)) name(abst, replace)

// Combine graphs
	
graph combine vote abst ///
	, c(2) graphregion(color(white)) imargin(vsmall) //
graph export "$results\\$program.png", replace width(7200) height(3000)

cap log close