clear
clear matrix
graph drop _all

cd "${rawdata_path}"
*Pull parental characteristics from biography data
use biojob.dta, clear
keep persnr agefjob fjblue isei

mvdecode agefjob fjblue isei, mv(-10/-1)

preserve

rename persnr vnr
rename agefjob vagefjob
rename fjblue vfjblue
rename isei visei
save "${output_path}\tmp\fathers_bio.dta", replace
restore


rename persnr mnr
rename agefjob magefjob
rename fjblue mfjblue
rename isei misei
save "${output_path}\tmp\mothers_bio.dta", replace

cd "${output_path}\tmp\"
eststo clear
use "Linked_data.dta", clear
merge m:1 vnr using "fathers_bio.dta", keep(match master) nogen
merge m:1 mnr using "mothers_bio.dta", keep(match master) nogen


xtset, clear
keep if age>=25 & age<=40


keep v* syear m*
drop vil* mil* mother_near 
drop merge*

drop month mother_known migback




duplicates drop

set more off



gen vhschool=cond(vpgpsbil==3 | vpgpsbil==4,1,cond(vpgpsbil==.,.,0))
gen mhschool=cond(mpgpsbil==3 | mpgpsbil==4,1,cond(mpgpsbil==.,.,0))







preserve
keep if vage>=50 & vage<=70



gen vdisc60=60+1/12
replace vdisc60=60+2/12 if vgebjahr==1952 & vgebmonat==1
replace vdisc60=60+3/12 if vgebjahr==1952 & vgebmonat==2
replace vdisc60=60+4/12 if vgebjahr==1952 & vgebmonat==3
replace vdisc60=60+5/12 if vgebjahr==1952 & vgebmonat==4
replace vdisc60=60+6/12 if vgebjahr==1952 & vgebmonat==5
replace vdisc60=60+7/12 if vgebjahr==1952 & vgebmonat>=6 & vgebmonat<=12
replace vdisc60=60+8/12 if vgebjahr==1953 
replace vdisc60=60+9/12 if vgebjahr==1954
replace vdisc60=60+10/12 if vgebjahr==1955
replace vdisc60=60+11/12 if vgebjahr==1956
replace vdisc60=61 if vgebjahr==1957
 



gen vage_run=vage-vdisc60



gen vage_int=round(vage,1)
gen vage_run_int=round(vage_run,1)
gen veast1989=cond(vloc1989==1,1,cond(vloc1989==2 | vloc1989==3,0,.))

sort vnr syear
bys vage_int: egen hschool_fmean=mean(vhschool)
bys vage_int: egen east_fmean=mean(veast1989)
bys vage_int: egen rel_fmean=mean(vchris_rel)
bys vage_int: egen pgbilzeit_fmean=mean(vpgbilzeit)
bys vage_int: egen vagefjob_fmean=mean(vagefjob)


twoway  (qfitci vhschool vage_run if vage_run<0, fintensity(inten10)) (qfitci vhschool vage_run if vage_run>=0, fintensity(inten10)) (scatter hschool_fmean vage_run_int, lwidth("thin")), ///
xtitle("Father's age centered around threshold") ytitle("% fathers with high school degree") scheme(s1mono) legend(label(5 "Mean") label(2 "Local polynomial fit")  order(5 2)) title("A. High school degree") name(highschool)


twoway (qfitci vchris_rel vage_run if vage_run<0, fintensity(inten10)) (qfitci vchris_rel vage_run if vage_run>=0, fintensity(inten10)) (scatter rel_fmean vage_run_int, lwidth("thin")) , ///
xtitle("Father's age centered around threshold") ytitle("% following Christian religion") scheme(s1mono) legend(label(5 "Mean") label(2 "Local polynomial fit")  order(5 2)) title("B. Christian religon") name(rel)

twoway (qfitci vpgbilzeit vage_run if vage_run<0, fintensity(inten10)) (qfitci vpgbilzeit vage_run if vage_run>=0, fintensity(inten10)) (scatter pgbilzeit_fmean vage_run_int, lwidth("thin")) , ///
xtitle("Father's age centered around threshold") ytitle("Years of education") scheme(s1mono) legend(label(5 "Mean") label(2 "Local polynomial fit")  order(5 2)) title("C. Years of education") name(bilzeit)

twoway (qfitci vagefjob vage_run if vage_run<0, fintensity(inten10)) (qfitci vagefjob vage_run if vage_run>=0, fintensity(inten10)) (scatter vagefjob_fmean vage_run_int, lwidth("thin")) , ///
xtitle("Father's age centered around threshold") ytitle("Age at first job") scheme(s1mono) legend(label(5 "Mean") label(2 "Local polynomial fit")  order(5 2)) title("D. Age at first job") name(afj)


grc1leg highschool  rel bilzeit afj , altshrink title("Continuity of covariates - Fathers") scheme(s1mono)
graph export "${output_path}\Results\FigA8_A.tif", replace width(2400) 
graph drop _all

restore
keep if mage>=50 & mage<=70


gen mdisc60=60+1/12
replace mdisc60=60+2/12 if mgebjahr==1952 & mgebmonat==1
replace mdisc60=60+3/12 if mgebjahr==1952 & mgebmonat==2
replace mdisc60=60+4/12 if mgebjahr==1952 & mgebmonat==3
replace mdisc60=60+5/12 if mgebjahr==1952 & mgebmonat==4
replace mdisc60=60+6/12 if mgebjahr==1952 & mgebmonat==5
replace mdisc60=60+7/12 if mgebjahr==1952 & mgebmonat>=6 & mgebmonat<=12
replace mdisc60=60+8/12 if mgebjahr==1953 
replace mdisc60=60+9/12 if mgebjahr==1954
replace mdisc60=60+10/12 if mgebjahr==1955
replace mdisc60=60+11/12 if mgebjahr==1956
replace mdisc60=61 if mgebjahr==1957
 


gen mage_run=mage-mdisc60
gen meast1989=cond(mloc1989==1,1,cond(mloc1989==2 | mloc1989==3,0,.))

gen mage_run_int=round(mage_run,1)
gen mage_int=round(mage,1)

bys mage_int: egen hschool_mmean=mean(mhschool)
bys mage_int: egen east_mmean=mean(meast1989)
bys mage_int: egen rel_mmean=mean(mchris_rel)
bys mage_int: egen pgbilzeit_mmean=mean(mpgbilzeit)
bys mage_int: egen magefjob_mmean=mean(magefjob)


twoway  (qfitci mhschool mage_run if mage_run<0, fintensity(inten10)) (qfitci mhschool mage_run if mage_run>=0, fintensity(inten10)) (scatter hschool_mmean mage_run_int, lwidth("thin")), ///
xtitle("Mother's age centered around threshold") ytitle("% mothers with high school degree") scheme(s1mono) legend(label(5 "Mean") label(2 "Local polynomial fit")  order(5 2)) title("A. High school degree") name(highschool)


twoway (qfitci mchris_rel mage_run if mage_run<0, fintensity(inten10)) (qfitci mchris_rel mage_run if mage_run>=0, fintensity(inten10)) (scatter rel_mmean mage_run_int, lwidth("thin")) , ///
xtitle("Mother's age centered around threshold") ytitle("% following Christian religion") scheme(s1mono) legend(label(5 "Mean") label(2 "Local polynomial fit")  order(5 2)) title("B. Christian religion") name(rel)

twoway (qfitci mpgbilzeit mage_run if mage_run<0, fintensity(inten10)) (qfitci mpgbilzeit mage_run if mage_run>=0, fintensity(inten10)) (scatter pgbilzeit_mmean mage_run_int, lwidth("thin")) , ///
xtitle("Mother's age centered around threshold") ytitle("Years of education") scheme(s1mono) legend(label(5 "Mean") label(2 "Local polynomial fit")  order(5 2)) title("C. Years of education") name(bilzeit)

twoway (qfitci magefjob mage_run if mage_run<0, fintensity(inten10)) (qfitci magefjob mage_run if mage_run>=0, fintensity(inten10)) (scatter magefjob_mmean mage_run_int, lwidth("thin")) , ///
xtitle("Mother's age centered around threshold") ytitle("Age at first job") scheme(s1mono) legend(label(5 "Mean") label(2 "Local polynomial fit")  order(5 2)) title("D. Age at first job") name(afj)



grc1leg highschool  rel bilzeit afj , altshrink title("Continuity of covariates - Mothers") scheme(s1mono)
graph export "${output_path}\Results\FigA8_B.tif", replace width(2400) 

