set more off
cd "${output_path}\tmp\"


clear
gen depvar=""
gen mse=.
gen bw=.
gen fathers=.
save "bw_choice_mse.dta", replace

use "Linked_data.dta", replace
set more off


gen age2=age*age


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 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

sort pid syear
gen fbirth=f.birth

keep if age>=25 & age<=40


*Loop for fathers

foreach bw of numlist 10 7 5 3{
	foreach var of varlist fbirth vretired{ 

			keep if vage>=(60-`bw') & vage<=(60+`bw')
			
			set seed 292521
			bys pid: gen pobs=_n

*This section of the code generates a random variable to randomly assign individuals into 10 groups. We then conduct crossvalidation using 9 groups 
*for estimation and 1 group for prediction
			gen random_sort=runiform() if pobs==1
			sort random_sort
			gen nobs=_n if pobs==1
			egen gsort=cut(nobs), group(10)
			replace gsort=gsort+1

			bys pid: egen psort=max(gsort)

			gen mse=.
			
	
		foreach num of numlist 1/10{


			xi: xtreg `var' age age2 vage vage2 i.month i.syear if psort!=`num', fe cluster(pid)

			predict pred_prob if psort==`num' & age>=25 & age<=40 & (vage>=59) & (vage<=61), xb
		
			replace mse=(`var'-pred_prob)*(`var'-pred_prob) if psort==`num' & age>=25 & age<=40 & (vage>=59) & (vage<=61)
		
			drop pred_prob
						

				
			}

			
		summarize mse
			
		local c = r(mean)
			
			preserve
				use "bw_choice_mse.dta", clear
				local nplus = _N +1   
				set obs `nplus' 
	
				replace depvar="`var'" in l
				replace mse= `c' in l
				replace bw=`bw' in l
				replace fathers=1 in l
				save "bw_choice_mse.dta", replace
			restore
		drop mse pobs random_sort nobs gsort psort	
	}
}

use "Linked_data.dta", replace
set more off


gen age2=age*age
gen age3=age2*age

gen mage2=mage*mage
gen mage3=mage2*mage
gen mage4=mage2*mage
gen mage5=mage3*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 vage2=vage*vage
gen vage3=vage2*vage
gen vage4=vage2*vage
gen vage5=vage3*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

sort pid syear
gen fbirth=f.birth

keep if age>=25 & age<=40


*Loop for mothers
foreach bw of numlist 10 7 5 3{
	foreach var of varlist fbirth mretired{ 

	
			keep if mage>=(60-`bw') & mage<=(60+`bw')
			
			set seed 292521
			bys pid: gen pobs=_n

			gen random_sort=runiform() if pobs==1
			sort random_sort
			gen nobs=_n if pobs==1
			egen gsort=cut(nobs), group(10)
			replace gsort=gsort+1

			bys pid: egen psort=max(gsort)
			gen mse=.

	
		foreach num of numlist 1/50{


			qui xi: xtreg `var' age age2 mage mage2 i.month i.syear if psort!=`num', fe cluster(pid)

			predict pred_prob if psort==`num' & age>=25 & age<=40 & (mage>=59) & (mage<=61), xb
		
			replace mse=(`var'-pred_prob)*(`var'-pred_prob) if psort==`num' & age>=25 & age<=40 & (mage>=59) & (mage<=61)
			
		
			drop pred_prob
						

				
			}

			
		summarize mse
			
		local c = r(mean)
			
			preserve
				use "bw_choice_mse.dta", clear
				local nplus = _N +1   
				set obs `nplus' 
	
				replace depvar="`var'" in l
				replace mse= `c' in l
				replace bw=`bw' in l
				replace fathers=0 in l
				save "bw_choice_mse.dta", replace
			restore
		drop mse pobs random_sort nobs gsort psort	
	}
}


use "bw_choice_mse.dta", clear
set logtype text
log using "${output_path}\Results\TableA4.txt", replace

bys depvar bw fathers: sum mse

log close

