clear
set more off

use "$temp/predictions_final", clear

gen epid_march_15 = nbre_hospit_corona_cum_pc * (date == mdy(03,15,2020))
gen epid_march_26 = nbre_hospit_corona_cum_pc * (date == mdy(03,26,2020))

collapse (max) turnout_* epid_march_*, by(id_dep)

* Graph 2014 and 2020 turnout by departement
********************************************************************************

reg turnout_2020 turnout_2014
local pos = 51
local x`pos' = `pos'/100
local y`pos' = _b[_cons] + _b[turnout_2014] * `x`pos''

local alpha : di %3.2fc abs(_b[_cons])
local beta : di %3.2fc _b[turnout_2014]
local r2 : di %3.2fc e(r2)

twoway scatter turnout_2020 turnout_2014, mcolor(gs6) ||  ///
	   scatteri 0.29 0.29 0.81 0.81, msymbol(none) recast(line) lcolor(black) lpattern(dash) || ///
	   || lfit turnout_2020 turnout_2014, lcolor(black) || ///
	   , ///
	   xlabel(0.3(0.1)0.8, format(%3.1f)) ylabel(0.3(0.1)0.8, angle(0) nogrid format(%3.1f)) ///
	   xtitle(" " "Turnout at 2014 municipal elections") ytitle("Turnout at 2020 municipal elections" " ") ///
	   legend(off) ///
	   text(0.7 0.7 "45° line", placement(e) justification(left) margin(medsmall) size(medsmall)) ///
	   text(`y`pos'' `x`pos'' "{it:y} = `beta' {it:x} - `alpha' [R{superscript:2} = `r2']", placement(e) justification(left) margin(medsmall) size(medsmall)) ///
	   bgcolor(white) graphregion(color(white)) plotregion(margin(zero)) 

graph export "$graphs/dep_turnout.eps", replace
graph export "$graphs/dep_turnout.png", replace

* Graph 2020 turnout and 2014-2020 turnout difference as a function of cumulated hospitalizations on March 15 and 26, 2020
********************************************************************************

gen diff = turnout_2020 - turnout_2014

foreach date in 15 26{
* local date = 26 // Test command

replace epid_march_`date' = log(1 + epid_march_`date')

local v1 =  log(1+150)
local text1 = "150"
local v2 =  log(1+50)
local text2 = "50"
local v3 =  log(1+20)
local text3 = "20"
local v4 =  log(1+10)
local text4 = "10"
local v5 =  log(1+5)
local text5 = "5"
local v6 =  log(1+4)
local text6 = "4"
local v7 =  log(1+3)
local text7 = "3"
local v8 =  log(1+2)
local text8 = "2"
local v9 =  log(1+1)
local text9 = "1"
local v10 =  log(1+0.5)
local text10 = "0.5"
local v11 =  log(1+500)
local text11 = "500"

*  2020 turnout
***************

local ypos = 0.70
loca xpos = log(1+20)

reg turnout_2020 epid_march_`date'
local alpha : di %3.2fc _b[_cons]
local beta : di %3.2fc _b[epid_march_`date']
local r2 : di %3.2fc e(r2)

twoway scatter turnout_2020 epid_march_`date', mcolor(gs6) ||  ///
scatteri 0.34 0.34 0.705 0.705, msymbol(none) || ///
lfit turnout_2020 epid_march_`date', lcolor(black) || ///
, ///
xlabel(0 "0" `v1' "`text1'" `v2' "`text2'" `v3' "`text3'" `v4' "`text4'" `v5' "`text5'" `v6' "`text6'" `v7' "`text7'" `v8' "`text8'" `v9' "`text9'" `v10' "`text10'" `v11' "`text11'") ///
ylabel(0.35(0.05)0.7, angle(0) format(%4.2f) nogrid) ///
ytitle("2020 turnout" " ") xtitle(" " "Cumulated number of hospitalizations for COVID-19 suspicion" "per 100,000 inhabitants on March `date', 2020") ///
text(`ypos' `xpos' "{it:y} = `beta' {it:x} + `alpha' [R{superscript:2} = `r2']", placement(e) justification(left) margin(medsmall) size(medsmall)) ///
bgcolor(white) graphregion(color(white)) ///
legend(off)

graph export "$graphs/dep_turnout_2020_march_`date'.eps", replace
graph export "$graphs/dep_turnout_2020_march_`date'.png", replace

* 2014 to 2020 turnout difference
*********************************

local ypos = -0.1
loca xpos = log(1+20)

reg diff epid_march_`date'
local alpha : di %3.2fc _b[_cons]
local beta : di %3.2fc _b[epid_march_`date']
local r2 : di %3.2fc e(r2)

twoway scatter diff epid_march_`date', mcolor(gs6) ||  ///
lfit diff epid_march_`date', lcolor(black) || ///
, ///
xlabel(0 "0" `v1' "`text1'" `v2' "`text2'" `v3' "`text3'" `v4' "`text4'" `v5' "`text5'" `v6' "`text6'" `v7' "`text7'" `v8' "`text8'" `v9' "`text9'" `v10' "`text10'" `v11' "`text11'") ///
ylabel(-0.10(0.05)-0.30, angle(0) format(%4.2f) nogrid) ///
ytitle("2014 to 2020 turnout difference" " ") xtitle(" " "Cumulated number of hospitalizations for COVID-19 suspicion" "per 100,000 inhabitants on March `date', 2020") ///
text(`ypos' `xpos' "{it:y} = `beta' {it:x} + `alpha' [R{superscript:2} = `r2']", placement(e) justification(left) margin(medsmall) size(medsmall)) ///
bgcolor(white) graphregion(color(white)) ///
legend(off)

graph export "$graphs/dep_turnout_diff_march_`date'.eps", replace
graph export "$graphs/dep_turnout_diff_march_`date'.png", replace

}
