** RECALL bias estimates
snapshot erase _all
snapshot  save
 /**/
 snapshot restore 1
 
 forvalue t=2020(-1)2000{
 	cap drop _merge
	cap drop help
	cap drop e_faelle_id
 	local n = `t'-1
	 merge 1:1 pnr using $dorg/bef12_`n', keep(1 3) keepus(e_faelle_id) 
	 gen cohab_`t' = e_faelle_id != ""
	 gen in_accord`t' = 1
	 replace in_accord`t'= 0 if (cohab_`t'==0 & year_start_0 < `t' )
	 gen help =0
	 forvalue v =1/8{
	 	replace in_accord`t' = 0 if ///
		(cohab_`t'==0 & year_start_`v' < `t' & year_end_`v' >= `t')
		replace help = help+1 if ///
		(cohab_`t'==1 & year_start_`v' >= `t' | year_end_`v' < `t')
	 }
	 replace in_accord`t' =0 if help == 8 & year_start_0 >= `t' 
	 replace in_accord`t'=. if _merge ==1
	 drop help _merge e_faelle_id
 }
 
 reshape long in_accord cohab_, i(pnr) j(a_year)
 gsort pnr -a_year
 by pnr: gen mark =_n==1
 keep if a_year-doby >= 18 | mark ==1
 
forvalue t = 2020(-1)2000{
	qui	sureg   (eq1: accordance i.b1971.doby  i.cohab_jan) ///
			(eq2: in_accord i.b1971.doby  i.cohab_) [aw=pervgt ] if a_year==`t'
	qui margins, at(cohab_jan_2020 =(0 1) cohab_ = (0 1)) atmeans
	matrix b = r(b)
	matrix V = r(V)
	if `t'==2020 matrix a =     (b[1,1],V[1,1]^.5,`t',1,1)
	if `t'< 2020 matrix a = a \ (b[1,1],V[1,1]^.5,`t',1,1)
	matrix a = a \ (b[1,3],V[3,3]^.5,`t',1,2)
	matrix a = a \ (b[1,5],V[5,5]^.5,`t',2,1)
	matrix a = a \ (b[1,6],V[6,6]^.5,`t',2,2)
	matrix a = a \ (b[1,5]-b[1,1],(V[1,1]+V[5,5]-2*V[5,1])^.5,`t',3,1)
	matrix a = a \ (b[1,6]-b[1,3],(V[3,3]+V[6,6]-2*V[6,3])^.5,`t',3,2)

}

svmat a
rename (a1 a2 a3 a4 a5) (b se year_est sample group)
tostring sample group, replace
replace group = "Single" if group =="1"
replace group = "Cohabiting" if group =="2"
replace sample = "Baseline" if sample == "1"
replace sample = "Recall" if sample == "2"
replace sample = "Difference" if sample == "3"

outsheet b se year_est sample group using "Y:\Data\Workdata\708085\pf\GGS\recall.txt" if b !=., replace

recode age2 	(37/44=1 "18-24") ///
				(45/50=2 "25-29") ///
			, gen(agegroup2001) 

recode age2 	(27/34=1 "18-24") ///
				(35/40=2 "25-29") ///
				(40/44=3 "30-34") ///
				(45/50=4 "35-39") ///
			, gen(agegroup2011) 			
			
tab agegroup2001 cohab_  [aw=pervgt], row nofr, if a_year==2001
bysort  cohab_: tab agegroup2001 in_accord [aw=pervgt], row nofr, if a_year==2001

tab agegroup2011 cohab_  [aw=pervgt], row nofr, if a_year==2011
bysort  cohab_: tab agegroup2011 in_accord [aw=pervgt], row nofr, if a_year==2011




 snapshot restore 1
 forvalue t=2020(-1)2000{
 	cap drop _merge
	cap drop help
	cap drop e_faelle_id
 	local n = `t'-1
	 merge 1:1 pnr using $dorg/bef12_`n', keep(1 3) keepus(e_faelle_id) 
	 gen cohab_`t' = e_faelle_id != ""
	 gen in_accord`t' = 1
	 replace in_accord`t'= 0 if (cohab_`t'==0 & year_start_0 < `t' )
	 gen help =0
	 forvalue v =1/8{
	 	replace in_accord`t' = 0 if ///
			cohab_`t'==0 & (year_start_`v' < `t' & year_end_`v' >= `t') & ///
			(year_start_`v' < `t'+1 & year_end_`v' >= `t'+1) &  ///
			(year_start_`v' < `t' -1& year_end_`v' >= `t'-1) 
		replace help = help+1 if ///
		cohab_`t'==1 & (year_start_`v' >= `t' | year_end_`v' < `t') & ///
		(year_start_`v' >= `t'+1 | year_end_`v' < `t'+1) & ///
		(year_start_`v' >= `t'-1 | year_end_`v' < `t'-1)
	 }
	 replace in_accord`t' =0 if help == 8 & year_start_0 >= `t' 
	 replace in_accord`t'=. if _merge ==1
	 drop help _merge e_faelle_id
 }
 
 reshape long in_accord cohab_, i(pnr) j(a_year)
 gsort pnr -a_year
 by pnr: gen mark =_n==1
 keep if a_year-doby >= 18 | mark ==1
 
 gen age_new = a_year-doby
 
reg accordance i.age_new i.cohab_jan [aweight=pervgt], vce(hc2), if mark ==1
reg in_accord i.age_new i.cohab_#i.b2020.a_year [aweight=pervgt], vce(hc2)


forvalue t = 2020(-1)2000{
	qui	sureg   (eq1: accordance i.b1971.doby  i.cohab_jan) ///
			(eq2: in_accord i.b1971.doby  i.cohab_) [aw=pervgt ] if a_year==`t'
	qui margins, at(cohab_jan_2020 =(0 1) cohab_ = (0 1)) atmeans
	matrix b = r(b)
	matrix V = r(V)
	if `t'==2020 matrix a =     (b[1,1],V[1,1]^.5,`t',1,1)
	if `t'< 2020 matrix a = a \ (b[1,1],V[1,1]^.5,`t',1,1)
	matrix a = a \ (b[1,3],V[3,3]^.5,`t',1,2)
	matrix a = a \ (b[1,5],V[5,5]^.5,`t',2,1)
	matrix a = a \ (b[1,6],V[6,6]^.5,`t',2,2)
	matrix a = a \ (b[1,5]-b[1,1],(V[1,1]+V[5,5]-2*V[5,1])^.5,`t',3,1)
	matrix a = a \ (b[1,6]-b[1,3],(V[3,3]+V[6,6]-2*V[6,3])^.5,`t',3,2)

}

svmat a
rename (a1 a2 a3 a4 a5) (b se year_est sample group)
tostring sample group, replace
replace group = "Single" if group =="1"
replace group = "Cohabiting" if group =="2"
replace sample = "Baseline" if sample == "1"
replace sample = "Recall" if sample == "2"
replace sample = "Difference" if sample == "3"

outsheet b se year_est sample group using "Y:\Data\Workdata\708085\pf\GGS\recall2.txt" if b !=., replace


snapshot restore 1
 forvalue t=2020(-1)2000{
 	cap drop _merge
	cap drop help
	cap drop e_faelle_id
 	local n = `t'-1
	 merge 1:1 pnr using $dorg/bef12_`n', keep(1 3) keepus(e_faelle_id) 
	 gen cohab_`t' = e_faelle_id != ""
	 gen in_accord`t' = 1
	 replace in_accord`t'= 0 if (cohab_`t'==0 & year_start_0 < `t' )
	 gen help =0
	 forvalue v =1/8{
	 	replace in_accord`t' = 0 if ///
		(cohab_`t'==0 & year_start_`v' < `t' & year_end_`v' >= `t')
		replace help = help+1 if ///
		(cohab_`t'==1 & year_start_`v' >= `t' | year_end_`v' < `t')
	 }
	 replace in_accord`t' =0 if help == 8 & year_start_0 >= `t' 
	 replace in_accord`t'=. if _merge ==1
	 drop help _merge e_faelle_id
 }
 
 gen robustness= in_accord2000
 
 reshape long in_accord cohab_, i(pnr) j(a_year)
 gsort pnr -a_year
 by pnr: gen mark =_n==1
 keep if a_year-doby >= 18 | mark ==1
 
 gen age_new = a_year-doby
 
reg accordance i.age_new i.cohab_jan [aweight=pervgt], vce(hc2), if mark ==1
reg in_accord i.age_new i.cohab_#i.b2020.a_year [aweight=pervgt], vce(hc2)


forvalue t = 2020(-1)2000{
	qui	sureg   (eq1: accordance i.b1971.doby  i.cohab_jan) ///
			(eq2: in_accord i.b1971.doby  i.cohab_) [aw=pervgt ] if a_year==`t' & robustness!=.
	qui margins, at(cohab_jan_2020 =(0 1) cohab_ = (0 1)) atmeans
	matrix b = r(b)
	matrix V = r(V)
	if `t'==2020 matrix a =     (b[1,1],V[1,1]^.5,`t',1,1)
	if `t'< 2020 matrix a = a \ (b[1,1],V[1,1]^.5,`t',1,1)
	matrix a = a \ (b[1,3],V[3,3]^.5,`t',1,2)
	matrix a = a \ (b[1,5],V[5,5]^.5,`t',2,1)
	matrix a = a \ (b[1,6],V[6,6]^.5,`t',2,2)
	matrix a = a \ (b[1,5]-b[1,1],(V[1,1]+V[5,5]-2*V[5,1])^.5,`t',3,1)
	matrix a = a \ (b[1,6]-b[1,3],(V[3,3]+V[6,6]-2*V[6,3])^.5,`t',3,2)

}

svmat a
rename (a1 a2 a3 a4 a5) (b se year_est sample group)
tostring sample group, replace
replace group = "Single" if group =="1"
replace group = "Cohabiting" if group =="2"
replace sample = "Baseline" if sample == "1"
replace sample = "Recall" if sample == "2"
replace sample = "Difference" if sample == "3"

outsheet b se year_est sample group using "Y:\Data\Workdata\708085\pf\GGS\recall3.txt" if b !=., replace


*/
** RECALL bias estimates allowing for age at period of recall to inform baseline
 
 snapshot restore 1

 forvalue t=2020(-1)2000{
 	cap drop _merge
	cap drop help
	cap drop e_faelle_id
 	local n = `t'-1
	 merge 1:1 pnr using $dorg/bef12_`n', keep(1 3) keepus(e_faelle_id) 
	 gen cohab_`t' = e_faelle_id != ""
	 gen in_accord`t' = 1
	 replace in_accord`t'= 0 if (cohab_`t'==0 & year_start_0 < `t' )
	 gen help =0
	 forvalue v =1/8{
	 	replace in_accord`t' = 0 if ///
		(cohab_`t'==0 & year_start_`v' < `t' & year_end_`v' >= `t')
		replace help = help+1 if ///
		(cohab_`t'==1 & year_start_`v' >= `t' | year_end_`v' < `t')
	 }
	 replace in_accord`t' =0 if help == 8 & year_start_0 >= `t' 
	 replace in_accord`t'=. if _merge ==1
	 drop help _merge e_faelle_id
 }
 
 reshape long in_accord cohab_, i(pnr) j(a_year)
 gsort pnr -a_year
 by pnr: gen mark =_n==1
 keep if a_year-doby >= 18 | mark ==1
 
 gen age_new = a_year-doby
 
snapshot save 

clear matrix 
forvalue w=1/1000{
	snapshot restore 2
	set seed `w'
	di in ye `w'
	gen help = age2
	bsample, strata(strata)
	forvalue t = 2020(-1)2000{
		
		qui{
			reg   accordance i.age2  i.cohab_jan [aw=pervgt ] if a_year ==2020
			**Correct age 
			replace age2= age_new
			sum age2 if a_year==`t'
			local max = r(max)
			margins if inrange(age2,18,`max'), at(cohab_jan = (0 1)) post atmeans
			matrix b1 = r(b)
			reg in_accord i.b1971.doby  i.cohab_ [aw=pervgt ] if a_year==`t'
			margins, at(cohab_ = (0 1)) post
			matrix b2 = r(b)
			matrix a =     (b1[1,1],`t',1,1,`w')
			matrix a = a \ (b1[1,2],`t',1,2,`w')
			matrix a = a \ (b2[1,1],`t',2,1,`w')
			matrix a = a \ (b2[1,2],`t',2,2,`w')
			matrix a = a \ (b2[1,1]-b1[1,1],`t',3,1,`w')
			matrix a = a \ (b2[1,2]-b1[1,2],`t',3,2,`w')
			mata{
				if (`t'==2020 & `w'==1) a1=st_matrix("a")
				else a1=a1 \ st_matrix("a")
			}
			
			
			replace age2=help
		}
	}
}


clear
getmata (a*) = a1, force

bysort a2 a3 a4: egen b = mean(a1)
bysort a2 a3 a4: egen se = sd(a1)
bysort a2 a3 a4: keep if _n ==1
drop a1 a5
rename (a2 a3 a4) (year_est sample group)
tostring sample group, replace 
replace group = "Single" if group =="1"
replace group = "Cohabiting" if group =="2"
replace sample = "Baseline" if sample == "1"
replace sample = "Recall" if sample == "2"
replace sample = "Difference" if sample =="3"

outsheet b se year_est sample group using "Y:\Data\Workdata\708085\pf\GGS\recall4.txt" if b !=., replace

snapshot restore 1
 forvalue t=2020(-1)2000{
 	cap drop _merge
	cap drop help
	cap drop e_faelle_id
 	local n = `t'-1
	 merge 1:1 pnr using $dorg/bef12_`n', keep(1 3) keepus(e_faelle_id) 
	 gen cohab_`t' = e_faelle_id != ""
	 gen in_accord`t' = 1
	 replace in_accord`t'= 0 if (cohab_`t'==0 & year_start_0 < `t' )
	 gen help =0
	 forvalue v =1/8{
	 	replace in_accord`t' = 0 if ///
		(cohab_`t'==0 & year_start_`v' < `t' & year_end_`v' >= `t')
		replace help = help+1 if ///
		(cohab_`t'==1 & year_start_`v' >= `t' | year_end_`v' < `t')
	 }
	 replace in_accord`t' =0 if help == 8 & year_start_0 >= `t' 
	 replace in_accord`t'=. if _merge ==1
	 drop help _merge e_faelle_id
 }
 
 reshape long in_accord cohab_, i(pnr) j(a_year)
 gsort pnr -a_year
 by pnr: gen mark =_n==1
 keep if a_year-doby >= 18 | mark ==1
 
forvalue t = 2020(-1)2000{
	qui	sureg   (eq1: accordance i.b1971.doby  i.cohab_jan) ///
			(eq2: in_accord i.b1971.doby  i.cohab_) [aw=pervgt ] if a_year==`t' & marker_no==0
	qui margins, at(cohab_jan_2020 =(0 1) cohab_ = (0 1)) atmeans
	matrix b = r(b)
	matrix V = r(V)
	if `t'==2020 matrix a =     (b[1,1],V[1,1]^.5,`t',1,1)
	if `t'< 2020 matrix a = a \ (b[1,1],V[1,1]^.5,`t',1,1)
	matrix a = a \ (b[1,3],V[3,3]^.5,`t',1,2)
	matrix a = a \ (b[1,5],V[5,5]^.5,`t',2,1)
	matrix a = a \ (b[1,6],V[6,6]^.5,`t',2,2)
	matrix a = a \ (b[1,5]-b[1,1],(V[1,1]+V[5,5]-2*V[5,1])^.5,`t',3,1)
	matrix a = a \ (b[1,6]-b[1,3],(V[3,3]+V[6,6]-2*V[6,3])^.5,`t',3,2)

}

svmat a
rename (a1 a2 a3 a4 a5) (b se year_est sample group)
tostring sample group, replace
replace group = "Single" if group =="1"
replace group = "Cohabiting" if group =="2"
replace sample = "Baseline" if sample == "1"
replace sample = "Recall" if sample == "2"
replace sample = "Difference" if sample == "3"

outsheet b se year_est sample group using "Y:\Data\Workdata\708085\pf\GGS\recall5.txt" if b !=., replace


 snapshot restore 1
 forvalue t=2020(-1)2000{
 	cap drop _merge
	cap drop help
	cap drop e_faelle_id
 	local n = `t'-1
	 merge 1:1 pnr using $dorg/bef12_`n', keep(1 3) keepus(e_faelle_id) 
	 gen cohab_`t' = e_faelle_id != ""
	 gen in_accord`t' = 1
	 replace in_accord`t'= 0 if (cohab_`t'==0 & year_start_0 < `t' )
	 gen help =0
	 forvalue v =1/8{
	 	replace in_accord`t' = 0 if ///
		(cohab_`t'==0 & year_start_`v' < `t' & year_end_`v' >= `t')
		replace help = help+1 if ///
		(cohab_`t'==1 & year_start_`v' >= `t' | year_end_`v' < `t')
	 }
	 replace in_accord`t' =0 if help == 8 & year_start_0 >= `t' 
	 replace in_accord`t'=. if _merge ==1
	 drop help _merge e_faelle_id
 }
 
 reshape long in_accord cohab_, i(pnr) j(a_year)
 gsort pnr -a_year
 by pnr: gen mark =_n==1
 keep if a_year-doby >= 18 | mark ==1
 gen doby2 = doby
 
forvalue t = 2020(-1)2000{
	replace doby2 = `t'-19 if `t'- doby < 19 // account for very few in cohab when 18
	qui	sureg   (eq1: accordance i.b1971.doby2##i.cohab_jan) ///
			(eq2: in_accord i.b1971.doby2##i.cohab_) [aw=pervgt ] if a_year==`t'
	qui margins, at(cohab_jan_2020 =(0 1) cohab_ = (0 1)) atmeans
	matrix b = r(b)
	matrix V = r(V)
	if `t'==2020 matrix a =     (b[1,1],V[1,1]^.5,`t',1,1)
	if `t'< 2020 matrix a = a \ (b[1,1],V[1,1]^.5,`t',1,1)
	matrix a = a \ (b[1,3],V[3,3]^.5,`t',1,2)
	matrix a = a \ (b[1,5],V[5,5]^.5,`t',2,1)
	matrix a = a \ (b[1,6],V[6,6]^.5,`t',2,2)
	matrix a = a \ (b[1,5]-b[1,1],(V[1,1]+V[5,5]-2*V[5,1])^.5,`t',3,1)
	matrix a = a \ (b[1,6]-b[1,3],(V[3,3]+V[6,6]-2*V[6,3])^.5,`t',3,2)

}

svmat a
rename (a1 a2 a3 a4 a5) (b se year_est sample group)
tostring sample group, replace
replace group = "Single" if group =="1"
replace group = "Cohabiting" if group =="2"
replace sample = "Baseline" if sample == "1"
replace sample = "Recall" if sample == "2"
replace sample = "Difference" if sample == "3"

outsheet b se year_est sample group using "Y:\Data\Workdata\708085\pf\GGS\recall6.txt" if b !=., replace

