cd "${output_path}\tmp\"

eststo clear
use "Linked_data.dta", clear
keep if age>=17

*Generate variables for plots
gen age_int=round(age,1)
gen vage_int=round(vage,1)
gen mage_int=round(mage,1)

bys age_int: egen birth_age=mean(birth)


bys age_int: egen birth_age_daught=mean(birth) if sex==2
bys mage_int: egen birth_vage_daught=mean(birth) if sex==2
bys vage_int: egen birth_mage_daught=mean(birth) if sex==2

bys age_int: egen birth_age_son=mean(birth) if sex==1
bys mage_int: egen birth_vage_son=mean(birth) if sex==1
bys vage_int: egen birth_mage_son=mean(birth) if sex==1


gen vdisc60=cond((vage+0.001)>=(60+1/12),1,0)
replace vdisc60=cond((vage+0.001)>=(60+2/12),1,0) if vgebjahr==1952 & vgebmonat==1
replace vdisc60=cond((vage+0.001)>=(60+3/12),1,0) if vgebjahr==1952 & vgebmonat==2
replace vdisc60=cond((vage+0.001)>=(60+4/12),1,0) if vgebjahr==1952 & vgebmonat==3
replace vdisc60=cond((vage+0.001)>=(60+5/12),1,0) if vgebjahr==1952 & vgebmonat==4
replace vdisc60=cond((vage+0.001)>=(60+6/12),1,0) if vgebjahr==1952 & vgebmonat==5
replace vdisc60=cond((vage+0.001)>=(60+7/12),1,0) if vgebjahr==1952 & vgebmonat>=6 & vgebmonat<=12
replace vdisc60=cond((vage+0.001)>=(60+8/12),1,0) if vgebjahr==1953 
replace vdisc60=cond((vage+0.001)>=(60+9/12),1,0) if vgebjahr==1954
replace vdisc60=cond((vage+0.001)>=(60+10/12),1,0) if vgebjahr==1955
replace vdisc60=cond((vage+0.001)>=(60+11/12),1,0) if vgebjahr==1956
replace vdisc60=cond((vage+0.001)>=61,1,0) if vgebjahr==1957
 
gen vdisc65=cond((vage+0.001)>=(65+1/12),1,0)
replace vdisc65=cond((vage+0.001)>=(65+2/12),1,0) if vgebjahr==1947
replace vdisc65=cond((vage+0.001)>=(65+3/12),1,0) if vgebjahr==1948
replace vdisc65=cond((vage+0.001)>=(65+4/12),1,0) if vgebjahr==1949
replace vdisc65=cond((vage+0.001)>=(65+5/12),1,0) if vgebjahr==1950
replace vdisc65=cond((vage+0.001)>=(65+6/12),1,0) if vgebjahr==1951
replace vdisc65=cond((vage+0.001)>=(65+7/12),1,0) if vgebjahr==1952
replace vdisc65=cond((vage+0.001)>=(65+8/12),1,0) if vgebjahr==1953
replace vdisc65=cond((vage+0.001)>=(65+9/12),1,0) if vgebjahr==1954
replace vdisc65=cond((vage+0.001)>=(65+10/12),1,0) if vgebjahr==1955
replace vdisc65=cond((vage+0.001)>=(65+11/12),1,0) if vgebjahr==1956
replace vdisc65=cond((vage+0.001)>=66,1,0) if vgebjahr==1957


gen mdisc60=cond((mage+0.001)>=(60+1/12),1,0)
replace mdisc60=cond((mage+0.001)>=(60+2/12),1,0) if mgebjahr==1952 & mgebmonat==1
replace mdisc60=cond((mage+0.001)>=(60+3/12),1,0) if mgebjahr==1952 & mgebmonat==2
replace mdisc60=cond((mage+0.001)>=(60+4/12),1,0) if mgebjahr==1952 & mgebmonat==3
replace mdisc60=cond((mage+0.001)>=(60+5/12),1,0) if mgebjahr==1952 & mgebmonat==4
replace mdisc60=cond((mage+0.001)>=(60+6/12),1,0) if mgebjahr==1952 & mgebmonat==5
replace mdisc60=cond((mage+0.001)>=(60+7/12),1,0) if mgebjahr==1952 & mgebmonat>=6 & mgebmonat<=12
replace mdisc60=cond((mage+0.001)>=(60+8/12),1,0) if mgebjahr==1953 
replace mdisc60=cond((mage+0.001)>=(60+9/12),1,0) if mgebjahr==1954
replace mdisc60=cond((mage+0.001)>=(60+10/12),1,0) if mgebjahr==1955
replace mdisc60=cond((mage+0.001)>=(60+11/12),1,0) if mgebjahr==1956
replace mdisc60=cond((mage+0.001)>=61,1,0) if mgebjahr==1957
 
gen mdisc65=cond((mage+0.001)>=(65+1/12),1,0)
replace mdisc65=cond((mage+0.001)>=(65+2/12),1,0) if mgebjahr==1947
replace mdisc65=cond((mage+0.001)>=(65+3/12),1,0) if mgebjahr==1948
replace mdisc65=cond((mage+0.001)>=(65+4/12),1,0) if mgebjahr==1949
replace mdisc65=cond((mage+0.001)>=(65+5/12),1,0) if mgebjahr==1950
replace mdisc65=cond((mage+0.001)>=(65+6/12),1,0) if mgebjahr==1951
replace mdisc65=cond((mage+0.001)>=(65+7/12),1,0) if mgebjahr==1952
replace mdisc65=cond((mage+0.001)>=(65+8/12),1,0) if mgebjahr==1953
replace mdisc65=cond((mage+0.001)>=(65+9/12),1,0) if mgebjahr==1954
replace mdisc65=cond((mage+0.001)>=(65+10/12),1,0) if mgebjahr==1955
replace mdisc65=cond((mage+0.001)>=(65+11/12),1,0) if mgebjahr==1956
replace mdisc65=cond((mage+0.001)>=66,1,0) if mgebjahr==1957

bys age_int: egen mean_vage60=mean(vdisc60)
bys age_int: egen mean_vage65=mean(vdisc65)

bys age_int: egen mean_mage60=mean(mdisc60)
bys age_int: egen mean_mage65=mean(mdisc65)

sort age_int
graph drop _all
twoway (scatter mean_vage60 age_int if age<50 & age>=17, connect(l) msymbol("diamond")) (scatter mean_vage65 age_int if age<50, connect(l) msymbol("diamond")) ,  legend(label(1 "60") label(2 "65")) ytitle("%") xlabel(20 30 40 50) xtitle("Age of adult child") scheme(s1mono) title("b. Fathers above age threshold") name(fathers)
twoway (scatter mean_mage60 age_int if age<50 & age>=17, connect(l) msymbol("triangle")) (scatter mean_mage65 age_int if age<50, connect(l) msymbol("triangle")) ,  legend(label(1 "60") label(2 "65")) ytitle("%") xlabel(20 30 40 50) xtitle("Age of adult child") scheme(s1mono) title("c. Mothers above age threshold") name(mothers)
twoway (scatter birth_age_daught age_int if age<50 & age>=17, connect(l) msymbol("plus")) (scatter birth_age_son age_int if age<50, connect(l) msymbol("cross")) , ytitle("% of births") xlabel(20 30 40 50) xtitle("Age of adult child") scheme(s1mono) name(birth) title("a. Child birth by age") legend(label(1 "Women") label(2 "Men"))

graph combine birth fathers mothers, altshrink scheme(s1mono)
graph export "${output_path}\Results\Fig1.tif", replace width(2400)
graph export "${output_path}\Results\Fig1.eps", replace 




