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

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

**** REPLICATION OF FIGURE B6 ****

clear all
set mem 2g
set more off

/*	
	OUTLINE
	1. REGRESSIONS
	2. EXPORT GRAPH
*/

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

// Import survey trust questions data

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

drop if dep==57 | dep==67 | dep==68
drop if dep==6 & survey_year==2012 // Region Est

// 1965 Regression

mlogit vote_1965 nocare_gov age c.age#c.age male if vote_1965<7, cluster(dep)
matrix b=e(b)
matrix V=e(V)

cap g coef_1965=.
cap g se_1965=.

forvalues i=1(1)7{
	local j=1+(`i'-1)*5
	
	replace coef_1965=b[1,`j'] if vote_1965==`i'
	replace se_1965=sqrt(V[`j',`j']) if vote_1965==`i'
	
}

// 1969 Regression

mlogit vote_1969 nocare_gov age c.age#c.age male if vote_1969<6, cluster(dep) rrr
matrix b=e(b)
matrix V=e(V)

cap g coef_1969=.
cap g se_1969=.

forvalues i=1(1)8{
	local j=1+(`i'-1)*5
	
	replace coef_1969=b[1,`j'] if vote_1969==`i'
	replace se_1969=sqrt(V[`j',`j']) if vote_1969==`i'
	
}

// 1974 Regression

mlogit vote_1974 nocare_pol age c.age#c.age male if vote_1974<=10, cluster(dep) rrr baseoutcome(3)
matrix b=e(b)
matrix V=e(V)

cap g coef_1974=.
cap g se_1974=.

forvalues i=1(1)10{
	local j=1+(`i'-1)*5
	
	replace coef_1974=b[1,`j'] if vote_1974==`i'
	replace se_1974=sqrt(V[`j',`j']) if vote_1974==`i'
	
}

// 1995 Regression

mlogit vote_1995 nocare_pol age c.age#c.age male if vote_1995<10, cluster(dep) rrr baseoutcome(5)
matrix b=e(b)
matrix V=e(V)

cap g coef_1995=.
cap g se_1995=.

forvalues i=1(1)10{
	local j=1+(`i'-1)*5

	replace coef_1995=b[1,`j'] if vote_1995==`i'
	replace se_1995=sqrt(V[`j',`j']) if vote_1995==`i'
	
}

// 2002 Regression

mlogit vote_2002 nocare_pol age c.age#c.age male if vote_2002<17, cluster(dep) rrr
matrix b=e(b)
matrix V=e(V)

cap g coef_2002=.
cap g se_2002=.

forvalues i=1(1)17{
	local j=1+(`i'-1)*5

	replace coef_2002=b[1,`j'] if vote_2002==`i'
	replace se_2002=sqrt(V[`j',`j']) if vote_2002==`i'
	
}

// 2007 Regression

mlogit vote_2007 nocare_pol age c.age#c.age male if vote_2007<12, cluster(dep) rrr baseoutcome(9)
matrix b=e(b)
matrix V=e(V)

cap g coef_2007=.
cap g se_2007=.

forvalues i=1(1)13{
	local j=1+(`i'-1)*5

	replace coef_2007=b[1,`j'] if vote_2007==`i'
	replace se_2007=sqrt(V[`j',`j']) if vote_2007==`i'
	
}

// 2012 Regression

mlogit vote_2012 nocare_pol age_cat c.age_cat#c.age_cat male if vote_2012<10, cluster(dep) rrr
matrix b=e(b)
matrix V=e(V)

cap g coef_2012=.
cap g se_2012=.

forvalues i=1(1)11{
	local j=1+(`i'-1)*5

	replace coef_2012=b[1,`j'] if vote_2012==`i'
	replace se_2012=sqrt(V[`j',`j']) if vote_2012==`i'
	
}

// 2017 Regression

mlogit vote_2017 nocare_pol age c.age#c.age male if vote_2017<10, cluster(dep) rrr
matrix b=e(b)
matrix V=e(V)

cap g coef_2017=.
cap g se_2017=.

forvalues i=0(1)11{
	local j=1+(`i'-1)*5

	replace coef_2017=b[1,`j'] if vote_2017==`i'
	replace se_2017=sqrt(V[`j',`j']) if vote_2017==`i'
	
}

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

// Get candidates and obs per candidate

foreach y in 1965 1969 1974 1995 2002 2007 2012 2017{
	decode vote_`y', g(candidate_`y')
}

// Collapse to get obs

collapse (count) vote_*, by(survey_year candidate_* coef_* se_*)

// Reshape

g annee=.
foreach y in 1965 1969 1974 1995 2002 2007 2012 2017{
	replace annee=`y' if candidate_`y'!=""
}

drop if annee==.

g id=_n
reshape long candidate_ coef_ se_ vote_, i(id) j(year)
rename *_ *

drop if year!=annee
drop year id

g lratio=exp(coef)
format lratio %9.3f

// Bounds

disp invnormal(.95)
g z=invnormal(.95)
g up=coef+z*se
g down=coef-z*se

replace up=exp(up)
replace down=exp(down)

replace up=107 if up>107 & up!=.

replace candidate="        Bové" if candidate=="Bove"
replace candidate="   Chaban-D." if candidate=="ChabanDelmas"
replace candidate=" Chevènement" if candidate=="Chevenement"
replace candidate="   de Gaulle" if candidate=="DeGaulle"
replace candidate=" de Villiers" if candidate=="DeVilliers"
replace candidate="   Dupont-A." if candidate=="DupontAignan"
replace candidate="Giscard d E." if candidate=="GiscarddEstaing"
replace candidate="      Le Pen" if candidate=="LePen"
replace candidate="      Mamère" if candidate=="Mamere"
replace candidate="      Mégret" if candidate=="Megret"
replace candidate="   Mélenchon" if candidate=="Melenchon"
replace candidate=" Saint-Josse" if candidate=="SaintJosse"
replace candidate="Tixier-Vign." if candidate=="TixierVignancour"

compress

foreach year in 1965 1969 1974 1995 2002 2007 2012 2017{

	preserve
	
	replace candidate=candidate+" (no data)" if lratio==.

	drop if coef==.
	keep if annee==`year'
	egen rank=rank(lratio), unique
	labmask rank, values(candidate)
	recode lratio (999999=1)

	qui sum rank
	local min=r(min)
	local max=r(max)
	
	format lratio %9.1f

	twoway ///
		(rcap up down rank, color(navy*.5) horizontal) ///
		(scatter rank lratio, mcolor(navy) msize(tiny) mlabsize(tiny) mlabel(lratio) mlabposition(12) mlabcolor(navy) mlabgap(*.5)) ///
		if annee==`year', ///
		ylabel(`min'(1)`max', format(%5.0f) angle(45) valuelabel labsize(tiny)) xlabel(1 25(25)100, format(%5.0f) labsize(vsmall)) yscale(reverse) /// 
		xline(1, lpattern(dash)) ytitle("") subtitle("`year' Election") ///
		graphregion(color(white)) legend(off) name(m`year', replace)
		
	restore

}

graph combine m1965 m1969 m1974 m1995 m2002 m2007 m2012 m2017, ///
	c(4) graphregion(color(white))
graph export "$results\\$program.png", replace width(7200) height(4500)

cap log close