clear set more off cd "$constructeddirectory\" use alldhsirclean, clear cd "$outdirectory\" *Drop surveys without repeated years drop if countryname=="Burundi" drop if countryname=="CAR" drop if countryname=="Comoros" drop if countryname=="Congo-Brazzaville" drop if countryname=="DRC" drop if countryname=="Gabon" drop if countryname=="Sierra Leone" drop if countryname=="South Africa" drop if countryname=="Sudan" drop if countryname=="Swaziland" *Keep variables of interest egen householdid=group(countryroundid cluster hhid) rename respyearofbirth2 respyearofbirth_2 keep countryname polyg respage respage2 respyearofbirth respyearofbirth_2 /// countryroundid cluster hhid sampleweight householdid respid order countryroundid cluster hhid householdid respid sampleweight /// polyg respage respage2 respyearofbirth respyearofbirth_2 *Weights bysort countryname respyearofbirth : egen meanweight=mean(sampleweight) genl blockweight=sampleweight/meanweight, l(Relative weight by country X year of birth) *Fixing names for use in commands replace countryname="Congo_Brazzaville" if countryname=="Congo-Brazzaville" replace countryname="Ivory_Coast" if countryname=="Ivory Coast" replace countryname="Burkina_Faso" if countryname=="Burkina Faso" *Polynomials in age and year of birth gen long respage3=respage^3 gen long respage4=respage^4 gen yearofbirth=respyearofbirth-1937 gen long yearofbirth2=yearofbirth^2 gen long yearofbirth3=yearofbirth^3 gen long yearofbirth4=yearofbirth^4 *Estimating time trends cd "$outdirectory" foreach country in Benin /// Burkina_Faso /// Cameroon /// Chad /// Ethiopia /// Ghana /// Guinea /// Ivory_Coast /// Kenya /// Liberia /// Madagascar /// Malawi /// Mali /// Mozambique /// Namibia /// Niger /// Nigeria /// Rwanda /// Senegal /// Tanzania /// Togo /// Uganda /// Zambia /// Zimbabwe { reg polyg respage respage2 respage3 respage4 /// yearofbirth yearofbirth2 yearofbirth3 yearofbirth4 /// if countryname=="`country'" [pweight=sampleweight] /*Weighted regression*/ gen trend30_`country'=_b[_cons] /// +_b[yearofbirth]*yearofbirth+_b[yearofbirth2]*yearofbirth2 /// +_b[yearofbirth3]*yearofbirth3 +_b[yearofbirth4]*yearofbirth4 /// +_b[respage]*30+_b[respage2]*30^2+_b[respage3]*30^3 +_b[respage4]*30^4 /// if countryname=="`country'" /*Predicted values*/ gen weightedtrend30_`country'=trend30_`country'*blockweight /*Weight each observation*/ bysort countryname respyearofbirth : /// egen meantrend30_`country'=mean(weightedtrend30_`country') /*Weighted mean by country and year of birth */ } *Collapse to country X year of birth level duplicates drop countryname respyearofbirth, force encode countryname, gen(countryid) tsset countryid respyearofbirth la var meantrend30_Benin "Benin" la var meantrend30_Burkina_Faso "Burkina Faso" la var meantrend30_Cameroon "Cameroon" la var meantrend30_Chad "Chad" la var meantrend30_Ethiopia "Ethiopia" la var meantrend30_Ghana "Ghana" la var meantrend30_Guinea "Guinea" la var meantrend30_Ivory_Coast "Ivory Coast" la var meantrend30_Kenya "Kenya" la var meantrend30_Liberia "Liberia" la var meantrend30_Madagascar "Madagascar" la var meantrend30_Malawi "Malawi" la var meantrend30_Mali "Mali" la var meantrend30_Mozambique "Mozambique" la var meantrend30_Namibia "Namibia" la var meantrend30_Niger "Niger" la var meantrend30_Nigeria "Nigeria" la var meantrend30_Rwanda "Rwanda" la var meantrend30_Senegal "Senegal" la var meantrend30_Tanzania "Tanzania" la var meantrend30_Togo "Togo" la var meantrend30_Uganda "Uganda" la var meantrend30_Zambia "Zambia" la var meantrend30_Zimbabwe "Zimbabwe" *In groups of 4: twoway /// (tsline meantrend30_Guinea) /// (tsline meantrend30_Burkina_Faso) /// (tsline meantrend30_Benin) /// (tsline meantrend30_Mali) /// , scheme(lean1) ytitle("") ysc(r(0 1)) ylabel(0(0.2)1) graph export trend1.png, replace twoway /// (tsline meantrend30_Senegal) /// (tsline meantrend30_Togo) /// (tsline meantrend30_Chad) /// (tsline meantrend30_Niger) /// , scheme(lean1) ytitle("") ysc(r(0 1)) ylabel(0(0.2)1) graph export trend2.png, replace twoway /// (tsline meantrend30_Nigeria ) /// (tsline meantrend30_Cameroon ) /// (tsline meantrend30_Tanzania ) /// (tsline meantrend30_Uganda ) /// , scheme(lean1) ytitle("") ysc(r(0 1)) ylabel(0(0.2)1) graph export trend3.png, replace twoway /// (tsline meantrend30_Liberia ) /// (tsline meantrend30_Ivory_Coast) /// (tsline meantrend30_Ghana) /// (tsline meantrend30_Mozambique ) /// , scheme(lean1) ytitle("") ysc(r(0 1)) ylabel(0(0.2)1) graph export trend4.png, replace twoway /// (tsline meantrend30_Kenya ) /// (tsline meantrend30_Malawi ) /// (tsline meantrend30_Ethiopia ) /// (tsline meantrend30_Namibia) /// , scheme(lean1) ytitle("") ysc(r(0 1)) ylabel(0(0.2)1) graph export trend5.png, replace twoway /// (tsline meantrend30_Rwanda ) /// (tsline meantrend30_Zambia) /// (tsline meantrend30_Zimbabwe) /// (tsline meantrend30_Madagascar ) /// , scheme(lean1) ytitle("") ysc(r(0 1)) ylabel(0(0.2)1) graph export trend6.png, replace