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

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

**** REPLICATION OF FIGURE B2 ****

clear all
set mem 2g
set more off

/*	
	OUTLINE
	1. MERGE DATA
	2. GRAPH
*/

*************************
***** 1. MERGE DATA *****
*************************

// Import conscription data

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

drop if com==. | birthyear==.

collapse (count) m_all=id, by(dep com birthyear)

tempfile tmp
save `tmp', replace

// Keep municipalities with casualty data

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

keep com
duplicates drop

merge 1:m com using `tmp'
drop if _merge!=3
drop _merge

tempfile tmp
save `tmp', replace

// Merge with casualty data

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

collapse (count) d_all=id, by(dep com birthyear)

merge 1:1 com birthyear using `tmp'
drop _merge

// Xtset

xtset com birthyear
tsfill, full

recode d_all m_all (.=0)

foreach case in all{
	g i_`case'=m_`case'*(134000/44034)
	format i_`case' %9.0f
}

save `tmp', replace

// Get 1936 population

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

keep dep com pop_1936
duplicates drop

merge 1:m com using `tmp'
drop if _merge!=3
drop _merge

// Group 1890-1905 

g group=1905 if birthyear<=1905
replace group=1930 if birthyear>=1930
replace group=birthyear if group==.

collapse (sum) d_all m_all i_all, by(com group pop)

collapse (sum) d_all m_all i_all pop, by(group)

// Get rates

foreach case in d m i{
	g p_`case'=(`case'_all*100)/pop
	format p_`case' %9.2f
}

g p_c=(d_all*100)/i_all
format p_c %9.2f

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

// Conscription and casualties

twoway (bar p_i group, bcolor(navy)) ///
	(bar p_d group, bcolor(maroon)), ///
	ytitle("Percent of 1936 population") /// 
	xtitle("Year of birth") /// 
	ylabel(0(.2).8, angle(0) format(%5.1f)) ///
	xlabel(1905 `" "Before" "1906""' 1908 1914 1927 1930 `" "After" "1929""') ///
	yscale(r(0 .94)) ///
	legend(label(1 "Conscripted") label(2 "Casualties") r(7) pos(11) ring(0) symx(*0.5) region(col(white))) /// 
	graphregion(color(white))  name(perc, replace)

// Casualty rate

twoway (bar p_c group if group>1907 & group<1914, bcolor(maroon)) ///
	(bar p_c group if group>1913 & group<1928, bcolor(maroon) fi(120)) ///
	(bar p_c group if group>1927 | group<1908, bcolor(maroon) fi(30)), ///
	ytitle("Casualty rate (%)") /// 
	xtitle("Year of birth") /// 
	ylabel(0(8)32, angle(0) format(%5.0f)) ///
	xlabel(1905 `" "Before" "1906""' 1908 1914 1927 1930 `" "After" "1929""') ///
	yscale(r(0 37.6)) ///
	legend(label(1 "Born 1908-1913") label(2 "Born 1914-1927") label(3 "Other years") r(7) pos(11) ring(0) symx(*0.5) region(col(white))) ///
	graphregion(color(white))  name(rates, replace)

graph combine perc rates ///
	, c(2) graphregion(color(white)) imargin(vsmall) //ycommon
graph export "$results\\$program.png", replace width(7200) height(3000)

cap log close
