clear
clear matrix

set more off
eststo clear
cd "${output_path}\tmp\"
use "Linked_data.dta", clear


gen age2=age*age


gen vage2=vage*vage



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

sort pid syear
gen fbirth=f.birth

*Information on distance is imputed using the nearest complete information
replace father_near=l.father_near if syear==1992 | syear==2002 | syear==2007 | syear==2012 | syear==2017
replace father_near=l2.father_near if syear==1993 | syear==2003 | syear==2008 | syear==2013
replace father_near=f.father_near if syear==1990 | syear==2000 | syear==2005 | syear==2010 | syear==2015
replace father_near=f2.father_near if syear==1989 | syear==1999 | syear==2004 | syear==2009 | syear==2014

preserve
sort pid syear
*To determine characteristics at the time of the retirement transition, the variable dretired records the change in retirement status
gen dretired=d.vretired
bys pid: egen total=total(abs(dretired))

*Number of grandchildren and income are captured a year before the retirement transition is observed, and then categorized.
gen pre_kids=children if f.dretired==1 & total==1
gen pre_inc=hghinc if f.dretired==1 & total==1


bys pid: egen kids_ind=mean(pre_kids) if total==1
bys pid: egen hinc_ind=mean(pre_inc) if total==1

gen vkids_cat=cond(kids_ind==0,0,cond(kids_ind==1,1,cond(kids_ind>=2 & kids_ind!=.,2,.)))
sum hinc_ind, detail
gen vinc_cat=cond(hinc_ind>r(p75) & hinc_ind!=.,1,cond(hinc_ind<=r(p25) & hinc_ind!=.,0,.))

qui eststo: xi: xtivreg2 fbirth age age2 vage vage2 i.month i.syear (vretired = vdisc60) if father_near==1 & vage>=50 & vage<=70 & age>=25 & age<=40, fe partial(i.month i.syear) cluster(pid)
qui eststo: xi: xtivreg2 fbirth age age2 vage vage2 i.month i.syear (vretired = vdisc60) if father_near==0 & vage>=50 & vage<=70 & age>=25 & age<=40, fe partial(i.month i.syear) cluster(pid)

qui eststo: xi: xtivreg2 fbirth age age2 vage vage2 i.month i.syear (vretired = vdisc60) if vinc_cat==0 & vage>=50 & vage<=70 & age>=25 & age<=40, fe partial(i.month i.syear) cluster(pid)
qui eststo: xi: xtivreg2 fbirth age age2 vage vage2 i.month i.syear (vretired = vdisc60) if vinc_cat==1 & vage>=50 & vage<=70 & age>=25 & age<=40, fe partial(i.month i.syear) cluster(pid)

qui eststo: xi: xtivreg2 fbirth age age2 vage vage2 i.month i.syear (vretired = vdisc60) if vkids_cat==0 & vage>=50 & vage<=70 & age>=25 & age<=40, fe partial(i.month i.syear) cluster(pid)
qui eststo: xi: xtivreg2 fbirth age age2 vage vage2 i.month i.syear (vretired = vdisc60) if vkids_cat==1 & vage>=50 & vage<=70 & age>=25 & age<=40, fe partial(i.month i.syear) cluster(pid)
qui eststo: xi: xtivreg2 fbirth age age2 vage vage2 i.month i.syear (vretired = vdisc60) if vkids_cat==2 & vage>=50 & vage<=70 & age>=25 & age<=40, fe partial(i.month i.syear) cluster(pid)


estout _all using "${output_path}\Results\Table3.csv" , keep(vretired) cells(b(star fmt(3)) se(par(`"="("' `")""'))) mlabels(,depvars) style(fixed)  stats(widstat N) ///
starlevels(* 0.10 ** 0.050 *** 0.010)  replace 
eststo clear


restore
*Estimation for mothers
gen mage2=mage*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 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





sort pid syear
replace mother_near=l.mother_near if syear==1992 | syear==2002 | syear==2007 | syear==2012 | syear==2017
replace mother_near=l2.mother_near if syear==1993 | syear==2003 | syear==2008 | syear==2013
replace mother_near=f.mother_near if syear==1990 | syear==2000 | syear==2005 | syear==2010 | syear==2015
replace mother_near=f2.mother_near if syear==1989 | syear==1999 | syear==2004 | syear==2009 | syear==2014


sort pid syear
gen dretired=d.mretired
bys pid: egen total=total(abs(dretired))


gen pre_kids=children if f.dretired==1 & total==1
gen pre_inc=hghinc if f.dretired==1 & total==1


bys pid: egen kids_ind=mean(pre_kids) if total==1
bys pid: egen hinc_ind=mean(pre_inc) if total==1

gen mkids_cat=cond(kids_ind==0,0,cond(kids_ind==1,1,cond(kids_ind>=2 & kids_ind!=.,2,.)))
sum hinc_ind, detail
gen minc_cat=cond(hinc_ind>r(p75) & hinc_ind!=.,1,cond(hinc_ind<=r(p25) & hinc_ind!=.,0,.))

qui eststo: xi: xtivreg2 fbirth age age2 mage mage2 i.month i.syear (mretired = mdisc60) if mother_near==1 & mage>=50 & mage<=70 & age>=25 & age<=40, fe partial(i.month i.syear) cluster(pid)
qui eststo: xi: xtivreg2 fbirth age age2 mage mage2 i.month i.syear (mretired = mdisc60) if mother_near==0 & mage>=50 & mage<=70 & age>=25 & age<=40, fe partial(i.month i.syear) cluster(pid)

qui eststo: xi: xtivreg2 fbirth age age2 mage mage2 i.month i.syear (mretired = mdisc60) if minc_cat==0 & mage>=50 & mage<=70 & age>=25 & age<=40, fe partial(i.month i.syear) cluster(pid)
qui eststo: xi: xtivreg2 fbirth age age2 mage mage2 i.month i.syear (mretired = mdisc60) if minc_cat==1 & mage>=50 & mage<=70 & age>=25 & age<=40, fe partial(i.month i.syear) cluster(pid)

qui eststo: xi: xtivreg2 fbirth age age2 mage mage2 i.month i.syear (mretired = mdisc60) if mkids_cat==0 & mage>=50 & mage<=70 & age>=25 & age<=40, fe partial(i.month i.syear) cluster(pid)
qui eststo: xi: xtivreg2 fbirth age age2 mage mage2 i.month i.syear (mretired = mdisc60) if mkids_cat==1 & mage>=50 & mage<=70 & age>=25 & age<=40, fe partial(i.month i.syear) cluster(pid)
qui eststo: xi: xtivreg2 fbirth age age2 mage mage2 i.month i.syear (mretired = mdisc60) if mkids_cat==2 & mage>=50 & mage<=70 & age>=25 & age<=40, fe partial(i.month i.syear) cluster(pid)


estout _all using "${output_path}\Results\Table3.csv" , keep(mretired) cells(b(star fmt(3)) se(par(`"="("' `")""'))) mlabels(,depvars) style(fixed)  stats(widstat N) ///
starlevels(* 0.10 ** 0.050 *** 0.010)  append
eststo clear



