cd "${output_path}\tmp\"


eststo clear
cap graph drop _all
use "Linked_data.dta", clear
set more off
keep if age>=25 & age<=40

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 age2=age*age
gen age3=age2*age

gen vage2=vage*vage
gen vage3=vage2*vage

replace vretirement_expectation=. if vretired==1
replace mretirement_expectation=. if mretired==1



sort pid syear


preserve
clear
gen beta=.
gen lower=.
gen upper=.
gen depvar=""
gen model=""
save "Timing_expectations_fathers.dta", replace
restore



foreach var of varlist  l7.vretirement_expectation l6.vretirement_expectation l5.vretirement_expectation l4.vretirement_expectation l3.vretirement_expectation l2.vretirement_expectation l1.vretirement_expectation vretirement_expectation{

xtreg `var' age age2 vage vage2 i.month i.syear vretired  if vage>=50 & vage<=70 & age>=25 & age<=40 , fe cluster(pid) 

preserve
    use "Timing_expectations_fathers.dta", clear
    local nplus = _N +1   
    set obs `nplus' 
	
	replace beta=_b[vretired] in l
	replace  lower= _b[vretired]-invnormal(0.975)*_se[vretired] in l
	replace  upper= _b[vretired]+invnormal(0.975)*_se[vretired] in l
	replace depvar=e(depvar) in l
	replace model="retirement" in l
    save "Timing_expectations_fathers.dta", replace
restore

}


foreach var of varlist  l7.vretirement_expectation l6.vretirement_expectation l5.vretirement_expectation l4.vretirement_expectation l3.vretirement_expectation l2.vretirement_expectation l1.vretirement_expectation ///
vretirement_expectation f1.vretirement_expectation f2.vretirement_expectation f3.vretirement_expectation f4.vretirement_expectation f5.vretirement_expectation f6.vretirement_expectation f7.vretirement_expectation{

xtreg `var' age age2 vage vage2 i.month i.syear birth  if vage>=50 & vage<=70 & age>=25 & age<=40 , fe cluster(pid) 

preserve
    use "Timing_expectations_fathers.dta", clear
    local nplus = _N +1   
    set obs `nplus' 
	
	replace beta=_b[birth] in l
	replace  lower= _b[birth]-invnormal(0.975)*_se[birth] in l
	replace  upper= _b[birth]+invnormal(0.975)*_se[birth] in l
	replace depvar=e(depvar) in l
	replace model="birth" in l
    save "Timing_expectations_fathers.dta", replace
restore

}



use "Timing_expectations_fathers.dta", clear


preserve
keep if model=="retirement"
gen sortnr=_n
gen t=sortnr-8





sort t

twoway (rspike upper lower t , lpattern(solid) lcolor(black) lwidth(medium) yline(0))  (scatter beta t , msize(small)) , ytitle("Father's retirement expectations - Effect size") title("a. Fathers - Retirement and Expectations") ///
xtitle("Time to retirement") legend(label(1 "95% CI")  label(2 "Point estimate")) scheme(s1mono) xscale(range(-7 7)) xlabel(-7(1)7) xline(-0.1) name(father_retirement)

restore


preserve
keep if model=="birth"
gen sortnr=_n
gen t=sortnr-8





sort t

twoway (rspike upper lower t , lpattern(solid) lcolor(black) lwidth(medium) yline(0))  (scatter beta t , msize(small)) , ytitle("Father's retirement expectations - Effect size") title("b. Fathers - Child Birth and Retirement Expectations") ///
xtitle("Time to child birth") legend(label(1 "95% CI")  label(2 "Point estimate")) scheme(s1mono) xscale(range(-7 7)) xlabel(-7(1)7) xline(-0.1) name(father_childbirth)

restore




use "Linked_data.dta", clear
set more off
keep if age>=25 & age<=40

gen mage2=mage*mage
gen mage3=mage2*mage



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 age2=age*age
gen age3=age2*age


replace mretirement_expectation=. if mretired==1

sort pid syear


preserve
clear
gen beta=.
gen lower=.
gen upper=.
gen depvar=""
gen model=""
save "Timing_expectations_mothers.dta", replace
restore



foreach var of varlist  l7.mretirement_expectation l6.mretirement_expectation l5.mretirement_expectation l4.mretirement_expectation l3.mretirement_expectation l2.mretirement_expectation l1.mretirement_expectation mretirement_expectation{

xtreg `var' age age2 mage mage2 i.month i.syear mretired  if mage>=50 & mage<=70 & age>=25 & age<=40 , fe cluster(pid) 

preserve
    use "Timing_expectations_mothers.dta", clear
    local nplus = _N +1   
    set obs `nplus' 
	
	replace beta=_b[mretired] in l
	replace  lower= _b[mretired]-invnormal(0.975)*_se[mretired] in l
	replace  upper= _b[mretired]+invnormal(0.975)*_se[mretired] in l
	replace depvar=e(depvar) in l
	replace model="retirement" in l
    save "Timing_expectations_mothers.dta", replace
restore

}


foreach var of varlist  l7.mretirement_expectation l6.mretirement_expectation l5.mretirement_expectation l4.mretirement_expectation l3.mretirement_expectation l2.mretirement_expectation l1.mretirement_expectation ///
mretirement_expectation f1.mretirement_expectation f2.mretirement_expectation f3.mretirement_expectation f4.mretirement_expectation f5.mretirement_expectation f6.mretirement_expectation f7.mretirement_expectation{

xtreg `var' age age2 mage mage2 i.month i.syear birth  if mage>=50 & mage<=70 & age>=25 & age<=40 , fe cluster(pid) 

preserve
    use "Timing_expectations_mothers.dta", clear
    local nplus = _N +1   
    set obs `nplus' 
	
	replace beta=_b[birth] in l
	replace  lower= _b[birth]-invnormal(0.975)*_se[birth] in l
	replace  upper= _b[birth]+invnormal(0.975)*_se[birth] in l
	replace depvar=e(depvar) in l
	replace model="birth" in l
    save "Timing_expectations_mothers.dta", replace
restore

}



use "Timing_expectations_mothers.dta", clear


preserve
keep if model=="retirement"
gen sortnr=_n
gen t=sortnr-8





sort t

twoway (rspike upper lower t , lpattern(solid) lcolor(black) lwidth(medium) yline(0))  (scatter beta t , msize(small)) , ytitle("Mother's retirement expectations - Effect size") title("c. Mothers - Retirement and Expectations") ///
xtitle("Time to retirement") legend(label(1 "95% CI")  label(2 "Point estimate")) scheme(s1mono) xscale(range(-7 7)) xlabel(-7(1)7) xline(-0.1) name(mother_retirement)

restore


preserve
keep if model=="birth"
gen sortnr=_n
gen t=sortnr-8





sort t

twoway (rspike upper lower t , lpattern(solid) lcolor(black) lwidth(medium) yline(0))  (scatter beta t , msize(small)) , ytitle("Mother's retirement expectations - Effect size") title("d. Mothers - Child Birth and Retirement Expectations") ///
xtitle("Time to child birth") legend(label(1 "95% CI")  label(2 "Point estimate")) scheme(s1mono) xscale(range(-7 7)) xlabel(-7(1)7) xline(-0.1) name(mother_childbirth)

restore

grc1leg father_retirement father_childbirth mother_retirement mother_childbirth, altshrink scheme(s1mono)
graph export "${output_path}\Results\FigA12.tif", replace width(2400)
