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

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

**** REPLICATION OF FIGURE C2 ****

clear all
set mem 2g
set more off

/*	
	OUTLINE
	1. REGRESSIONS
	2. GRAPH
*/

**************************
***** 1. REGRESSIONS *****
**************************

// Import data

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

drop if dep==57 | dep==67 | dep==68

// Get empty sheet to export data

capture erase "$results\abst60.txt"
capture erase "$results\abst70.txt"
capture erase "$results\rrw70.txt"
capture erase "$results\abst80.txt"
capture erase "$results\rrw80.txt"

// Elections in the 60s

foreach var in vote_l1958 vote_r1958 vote_l1962r1 vote_l1962r2 vote_r1962{
	reghdfe `var' i.age i.sexe, a(revenu ppi_rec dep agglo) cluster(dep)
	estimates store v60`var'
	qui sum `var' if e(sample)
	scalar mean_y=r(mean)
	estadd scalar mean_y
	outreg2 using "$results\abst60", nocons dec(3) level(95)
}

// Elections in the 70s

foreach var in vote_l1973 vote_p1974r1 vote_p1974r2 vote_l1978r1 vote_l1978r2{
	reghdfe `var' i.age i.sexe, a(revenu profession dep agglo) cluster(dep)
	estimates store v70`var'
	qui sum `var' if e(sample)
	scalar mean_y=r(mean)
	estadd scalar mean_y
	outreg2 using "$results\abst70", nocons dec(3) level(95)
}

foreach var in rrw_p1974r1 rrw_l1978r1{
	reghdfe `var' i.age i.sexe, a(revenu profession dep agglo) cluster(dep)
	estimates store v70`var'
	qui sum `var' if e(sample)
	scalar mean_y=r(mean)
	estadd scalar mean_y
	outreg2 using "$results\rrw70", nocons dec(3) level(95)
}

// Elections in the 80s

foreach var in vote_l1986 vote_p1988r1 vote_l1993 vote_e1994 vote_p1995r1{
	reghdfe `var' i.age i.sexe, a(revenu profession dep) cluster(dep)
	estimates store v80`var'
	qui sum `var' if e(sample)
	scalar mean_y=r(mean)
	estadd scalar mean_y
	outreg2 using "$results\abst80", nocons dec(3) level(95)
}

foreach var in rrw_l1986 rrw_p1988r1 rrw_l1993 rrw_e1994 rrw_p1995r1{
	reghdfe `var' i.age i.sexe, a(revenu profession dep) cluster(dep)
	estimates store v80`var'
	qui sum `var' if e(sample)
	scalar mean_y=r(mean)
	estadd scalar mean_y
	outreg2 using "$results\rrw80", nocons dec(3) level(95)
}

********************
***** 2. GRAPH *****
********************

// Abstention graph

insheet using "$results\abst60.txt", clear
drop if _n==1
g id=_n
forvalues i=2(1)6{
	rename v`i' `= strtoname(v`i'[1])'_str
}
save $results\tmp, replace

insheet using "$results\abst70.txt", clear
drop if _n==1
g id=_n
forvalues i=2(1)6{
	rename v`i' `= strtoname(v`i'[1])'_str
}
merge 1:1 id v1 using $results\tmp
drop _merge
save $results\tmp, replace

insheet using "$results\abst80.txt", clear
drop if _n==1
g id=_n
forvalues i=2(1)6{
	rename v`i' `= strtoname(v`i'[1])'_str
}
merge 1:1 id v1 using $results\tmp
drop _merge
save $results\tmp, replace
save $results\tmp2, replace

drop if vote_l1958=="vote_l1958"
rename v1 margins
drop if margins=="VARIABLES"
drop if margins==""
drop if _n>4

rename *_str *
reshape long vote_, i(margins) j(annee) string

rename vote_ beta
replace beta = subinstr(beta, "*", "", .) 
destring beta, replace

save $results\tmp, replace

use $results\tmp2, clear

drop if vote_l1958=="vote_l1958"
rename v1 margins
drop if margins=="VARIABLES"
replace margins=margins[_n-1] if margins==""
drop if margins==""
drop if _n==1 | _n==3 | _n==5 | _n==7 | _n==9 | _n==11 | _n==13
drop if _n>4

rename *_str *
reshape long vote_, i(margins) j(annee) string

rename vote_ se
replace se = subinstr(se, "(", "", .) 
replace se = subinstr(se, ")", "", .) 
destring se, replace

merge 1:1 annee margins using $results\tmp
drop _merge id
save $results\tmp, replace

use $results\tmp, clear

g year=substr(annee, 2, 4)
g election=substr(annee, 1, 1)
g round=substr(annee, strlen(annee)-1, 2)
g strlen=strlen(annee)

replace election="Ref." if election=="r"
replace election="Parl." if election=="l"
replace election="Pres." if election=="p"
replace election="Euro." if election=="e"

replace round=" ("+upper(round)+")"
replace round="" if strlen==5

g y=year+" "+election+round
replace year=election+round+" "+year

drop election round year strlen

gen up95 = beta + invnormal(0.975)*se // 
gen lo95 = beta - invnormal(0.975)*se

encode y, g(year_enc)

replace margins="Aged 35 to 49" if margins=="2bn.age"
replace margins="Aged 50 to 64" if margins=="3.age"
replace margins="Aged 65 and above" if margins=="4.age"
replace margins="Female" if margins=="2.sexe"
encode margins, g(var_enc)

xtset var_enc year_enc

tab var_enc

format beta %9.2f

twoway (scatter beta year_enc if var_enc==1, mcolor(navy) connect(line) lcolor(navy)) ///
	(line up95 year_enc if var_enc==1, lcolor(navy) lpattern("......-")) ///
	(line lo95 year_enc if var_enc==1, lcolor(navy) lpattern("......-")) ///
	(scatter beta year_enc if var_enc==2, mcolor(maroon) msymbol(S) connect(line) lcolor(maroon)) ///
	(line up95 year_enc if var_enc==2, lcolor(maroon) lpattern("......-")) ///
	(line lo95 year_enc if var_enc==2, lcolor(maroon) lpattern("......-")) ///
	(scatter beta year_enc if var_enc==3, mcolor(green) msymbol(T) connect(line) lcolor(green)) ///
	(line up95 year_enc if var_enc==3, lcolor(green) lpattern("......-")) ///
	(line lo95 year_enc if var_enc==3, lcolor(green) lpattern("......-")) ///
	(scatter beta year_enc if var_enc==4, mcolor(purple) msymbol(D) connect(line) lcolor(purple)) ///
	(line up95 year_enc if var_enc==4, lcolor(purple) lpattern("......-")) ///
	(line lo95 year_enc if var_enc==4, lcolor(purple) lpattern("......-")) 	///
	, ylabel(-.3(.15).15, labsize(vsmall) format(%3.2f) angle(0)) yline(0, lpattern(dash)) yscale(r(-.39 .195)) ///	
	xlabel(1(1)15, labsize(vsmall) angle(30) valuelabel) ///
	ytitle("OLS Coefficient (95% C.I.)", size(small)) xtitle("") ///
	leg(lab(1 "Aged 35 to 49") lab(4 "Aged 50 to 64") lab(7 "Aged 65 and above") lab(10 "Female") r(2) pos(7) ring(0) order(10 1 4 7) region(col(white)) symx(*0.5) size(vsmall)) ///
	graphregion(color(white)) subtitle("Abstention", ring(0))
graph export "$results\EEREV-D-21-00004_FigureC2_abst.png", replace width(3600) height(3600)

// Rad right graph

insheet using "$results\rrw70.txt", clear
drop if _n==1
g id=_n
forvalues i=2(1)3{
	rename v`i' `= strtoname(v`i'[1])'_str
}
save $results\tmp, replace

insheet using "$results\rrw80.txt", clear
drop if _n==1
g id=_n
forvalues i=2(1)6{
	rename v`i' `= strtoname(v`i'[1])'_str
}
merge 1:1 id v1 using $results\tmp
drop _merge
save $results\tmp, replace
save $results\tmp2, replace

drop if rrw_l1986_str=="rrw_l1986"
rename v1 margins
drop if margins=="VARIABLES"
drop if margins==""
drop if _n>4

rename *_str *
reshape long rrw_, i(margins) j(annee) string

rename rrw_ beta
replace beta = subinstr(beta, "*", "", .) 
destring beta, replace

save $results\tmp, replace

use $results\tmp2, clear

drop if rrw_l1986_str=="rrw_l1986"
rename v1 margins
drop if margins=="VARIABLES"
replace margins=margins[_n-1] if margins==""
drop if margins==""
drop if _n==1 | _n==3 | _n==5 | _n==7 | _n==9 | _n==11 | _n==13
drop if _n>4

rename *_str *
reshape long rrw_, i(margins) j(annee) string

rename rrw_ se
replace se = subinstr(se, "(", "", .) 
replace se = subinstr(se, ")", "", .) 
destring se, replace

merge 1:1 annee margins using $results\tmp
drop _merge id
save $results\tmp, replace

use $results\tmp, clear

g year=substr(annee, 2, 4)
g election=substr(annee, 1, 1)
g round=substr(annee, strlen(annee)-1, 2)
g strlen=strlen(annee)

replace election="Ref." if election=="r"
replace election="Parl." if election=="l"
replace election="Pres." if election=="p"
replace election="Euro." if election=="e"

replace round=" ("+upper(round)+")"
replace round="" if strlen==5

g y=year+" "+election+round
replace year=election+round+" "+year

drop election round year strlen

gen up95 = beta + invnormal(0.975)*se // 
gen lo95 = beta - invnormal(0.975)*se

encode y, g(year_enc)

replace margins="Aged 35 to 49" if margins=="2bn.age"
replace margins="Aged 50 to 64" if margins=="3.age"
replace margins="Aged 65 and above" if margins=="4.age"
replace margins="Female" if margins=="2.sexe"
encode margins, g(var_enc)

xtset var_enc year_enc

tab var_enc

format beta %9.2f

twoway (scatter beta year_enc if var_enc==1, mcolor(navy) connect(line) lcolor(navy)) ///
	(line up95 year_enc if var_enc==1, lcolor(navy) lpattern("......-")) ///
	(line lo95 year_enc if var_enc==1, lcolor(navy) lpattern("......-")) ///
	(scatter beta year_enc if var_enc==2, mcolor(maroon) msymbol(S) connect(line) lcolor(maroon)) ///
	(line up95 year_enc if var_enc==2, lcolor(maroon) lpattern("......-")) ///
	(line lo95 year_enc if var_enc==2, lcolor(maroon) lpattern("......-")) ///
	(scatter beta year_enc if var_enc==3, mcolor(green) msymbol(T) connect(line) lcolor(green)) ///
	(line up95 year_enc if var_enc==3, lcolor(green) lpattern("......-")) ///
	(line lo95 year_enc if var_enc==3, lcolor(green) lpattern("......-")) ///
	(scatter beta year_enc if var_enc==4, mcolor(purple) msymbol(D) connect(line) lcolor(purple)) ///
	(line up95 year_enc if var_enc==4, lcolor(purple) lpattern("......-")) ///
	(line lo95 year_enc if var_enc==4, lcolor(purple) lpattern("......-")) 	///
	, ylabel(-.1(.05).05, labsize(vsmall) format(%3.2f) angle(0)) yline(0, lpattern(dash)) yscale(r(-.13 .065)) ///	
	xlabel(1(1)7, labsize(vsmall) angle(30) valuelabel) ///
	ytitle("OLS Coefficient (95% C.I.)", size(small)) xtitle("") ///
	leg(lab(1 "Aged 35 to 49") lab(4 "Aged 50 to 64") lab(7 "Aged 65 and above") lab(10 "Female") r(2) pos(7) ring(0) order(10 1 4 7) region(col(white)) symx(*0.5) size(vsmall)) ///
	graphregion(color(white)) subtitle("Radical right-wing vote", ring(0))
graph export "$results\EEREV-D-21-00004_FigureC2_vote.png", replace width(3600) height(3600)

cap log close

capture erase "$results\abst60.txt"
capture erase "$results\abst70.txt"
capture erase "$results\rrw70.txt"
capture erase "$results\abst80.txt"
capture erase "$results\rrw80.txt"
capture erase $results\tmp.dta
capture erase $results\tmp2.dta