* This file generates the Attrition weights for the Panel data
* Updated version of weights.do (and probably closer to the data)
* Uses Out\psia08_iaf_hhdata and Out\psia08_iaf_indata_labeled generated by attrition_hhvar
* Please note that "D:\Mozambique\Mozambique\PSIA_from_Elena\allinstataformat" folder actually refers to the IAF 2003 survey (folder names are misleading)
* Collect other data
* Runs probit regressions to predict the individual probabilities to be included in the panel
* Infers the Inverse probability weights
* Saves Out/hhweights and Out/indweights = temp/weights = temp/indweightsout
* Temporary dataset used: temp/psia08_iaf_inddata, temp/psia08_iaf_inddata, temp/sec_j1, temp/wealth, temp/sec_b2, temp/psia08_iaf_hhdata

clear
capture log close
set more off
*cd "C:\Documents and Settings\wb360030\My Documents\Mozambique"
cd "D:\Mozambique\Mozambique"
set memory 200m

log using Out/weights.log, replace
*use "C:\Documents and Settings\wb360030\My Documents\Mozambique PSIA data from P\PSIA data\IAF merge files\psia08_iaf_hhdata.dta", replace
use "D:\Mozambique\Mozambique PSIA data from P\PSIA data\IAF merge files\psia08_iaf_hhdata.dta", replace
save temp\psia08_iaf_hhdata.dta, replace

*******************************************************************************************
* 				Individual information from the IAF survey 				*
*******************************************************************************************

use Out\psia08_iaf_hhdata.dta, replace

******************************************
* Generate the attrition variable for hh *
******************************************

* I do not consider the households with missing information on eligibility: those are the split households.
gen hhselected = 2-a20 if sp == 0
tab hhselected
tab hhselected sp, mis

count if ea2 == 194 & af == 18 & sp == 1
count if sp == 1

************************************
* Merge with info from individuals *
************************************

drop if sp != 0

count if ea2 == 194 & af == 18 & sp == 1
count if sp == 1

sort ea2 af sp
save temp\psia08_iaf_hhdata.dta, replace

use Out\psia08_iaf_indata_labeled.dta, replace
sort ea2 af 
foreach v of var sh* *hh *hhbis hhh* educoa rural b102 attsch agesch* eligible* attsch agesch eligible {
	local l`v' : variable label `v'
      if `"`l`v''"' == "" {
		local l`v' "`v'"
	}
}
collapse sh* *hh *hhbis hhh* educoa rural (count) b102 (max) attsch agesch* eligible* (mean) mattsch=attsch magesch=agesch meligible=eligible , /*
*/ by(ea2 af sp)
foreach v of var sh* *hh *hhbis hhh* educoa rural {
	label var `v' "(hh mean) `l`v''"
}
foreach v of var attsch agesch* eligible* {
	label var `v' "(hh max) `l`v''"
}
foreach v of var attsch agesch eligible {
	label var m`v' "(hh mean) `l`v''"
}
label var b102 "nb of hh members"


rename eligible eligiblehh
rename b102 nbmembers
rename attsch attschhh 
rename agesch ageschhh 
rename agesch08 agesch08hh 

sort ea2 af sp
merge ea2 af sp using temp\psia08_iaf_hhdata.dta
tab _m sp , mis
* Everyone is merged
drop _m

sort ea2 af 
merge ea2 af using Out\psia08_iaf_indata_labeled.dta
tab _m sp
tab _m hhselected , mis
drop _m
sort ea2 af b100

label values emptypehhh emptype
label values occtypehhh occtype

save temp\psia08_iaf_inddata.dta, replace

count if ea2 == 194 & af == 18 & sp == 1
count if sp == 1

***************************
* Merge file b2 from PSIA *
***************************

*use "C:\Documents and Settings\wb360030\My Documents\Mozambique PSIA data from P\PSIA data\Main Qs_stata\sec_b2", replace
use "D:\Mozambique\Mozambique PSIA data from P\PSIA data\Main Qs_stata\sec_b2", replace
*gen b100 = b201 
destring ea2, replace
sort ea2 af b100
tab sp
count
save temp\sec_b2, replace

use temp\psia08_iaf_inddata, replace
rename sp sp03
count
merge ea2 af b100 using temp\sec_b2, _merge(mergeind)

tab b100 mergeind , mis
drop if b100 == 99 & mergeind == 2

* 2 people in Inddata have 2 merges: solution with the names
sort ea2 af b100
by ea2 af b100: egen test = count(b100)
tab test
drop if test == 2 & b01a != "BATISTA ALBINO" & b01a != "ROSINHA BATISTA"

tab sp
tab mergeind
tab mergeind hhselected , mis
tab mergeind b108, mis
tab mergeind b112, mis

gen indselected = mergeind == 3

gen selection = hhselected + indselected
replace selection = 2 if indselected
tab selection

count if ea2 == 194 & af == 18 & sp == 1
count if sp == 1

**********************************
* Merge file education from PSIA *
**********************************

sort ea2 af sp x00a
save temp\psia08_iaf_inddata.dta, replace

*use "C:\Documents and Settings\wb360030\My Documents\Mozambique PSIA data from P\PSIA data\Main Qs_stata\sec_c0", replace
use "D:\Mozambique\Mozambique PSIA data from P\PSIA data\Main Qs_stata\sec_c0", replace
*rename x0a x00a
sort ea2 af sp x00a
merge ea2 af sp x00a using temp\psia08_iaf_inddata.dta
tab _m indselected, mis

drop if indselected == .
drop _m
sort ea2 af sp x00a
save temp\psia08_iaf_inddata.dta, replace

count if ea2 == 194 & af == 18 & sp == 1
count if sp == 1

**********************
* Education variable *
**********************

*gen educ08 = 0 if c01 == 2
*replace educ08 = 1 if c01 == 1 & (c03b == 0 | c03b == 1)
*replace educ08 = 2 if c03b == 2 | c03b == 6
*replace educ08 = 3 if c03b == 3 | c03b == 7  | c03b == 9
*replace educ08 = 4 if c03b == 4 | c03b == 8
*replace educ08 = 5 if c03b == 5
*replace educ08 = 6 if c03b == 10
sort ea2 af sp x00a
save temp\psia08_iaf_inddata.dta, replace
codebook ea2
tab ea2

count if ea2 == 194 & af == 18 & sp == 1
count if sp == 1

***********
* Weights *
***********

*use "C:\Documents and Settings\wb360030\My Documents\Mozambique PSIA data from P\PSIA weight\Adjusted_Wts_PSIA2", replace
use "D:\Mozambique\Mozambique PSIA data from P\PSIA weight\Adjusted_Wts_PSIA2", replace
gen test1 = noeasincollapsedstratum/noeasinpsiacollapstratum * cnweigh1 * wtadjustmissingnames
sum test1  basicpsiawt
drop test1

gen iafstratumpsia = iafstratum
recode iafstratumpsia 2199 = 2101
recode iafstratumpsia 7199 = 7101
recode iafstratumpsia 8199 = 8233
recode iafstratumpsia 8241 = 8233
recode iafstratumpsia 10199 = 10101

rename a1 ea2
rename cnweigh1 cnweigh1test
drop if _n > 221 
*& serial != .
*drop if ea2 == .
codebook ea2

tab ea2
keep ea2 cnweigh1test basicpsiawt iafstratumpsia
sort ea2
codebook ea2

merge ea2 using temp\psia08_iaf_inddata.dta
tab _m
drop _m

gen qlteami = 11 if prov == "CABO DELGADO"
replace qlteami = 10 if prov == "ZAMBEZIA"
replace qlteami = 9 if prov == "MANICA"
replace qlteami = 8 if prov == "NAMPULA"
replace qlteami = 7 if prov == "TETE"
replace qlteami = 6 if prov == "GAZA"
replace qlteami = 5 if prov == "SOFALA"
replace qlteami = 4 if prov == "MAPUTO PROV."
replace qlteami = 3 if prov == "MAPUTO CID."
replace qlteami = 2 if prov == "NIASSA"
replace qlteami = 1 if prov == "INHAMBANE"

gen qlteam = 11 if prov == "CABO DELGADO"
replace qlteam = 10 if prov == "ZAMBEZIA"
replace qlteam = 9 if prov == "TETE"
replace qlteam = 8 if prov == "GAZA"
replace qlteam = 7 if prov == "MANICA"
replace qlteam = 6 if prov == "INHAMBANE"
replace qlteam = 5 if prov == "NAMPULA"
replace qlteam = 4 if prov == "SOFALA"
replace qlteam = 3 if prov == "MAPUTO PROV."
replace qlteam = 2 if prov == "MAPUTO CID."
replace qlteam = 1 if prov == "NIASSA"

gen qltest = qlteam - qlteami 

gen qlteam2 = 1 if prov == "CABO DELGADO"
replace qlteam2 = 1 if prov == "ZAMBEZIA"
replace qlteam2 = 1 if prov == "TETE"
replace qlteam2 = 1 if prov == "GAZA"
replace qlteam2 = 0 if prov == "MANICA"
replace qlteam2 = 0 if prov == "INHAMBANE"
replace qlteam2 = 0 if prov == "NAMPULA"
replace qlteam2 = 0 if prov == "SOFALA"
replace qlteam2 = -1 if prov == "MAPUTO PROV."
replace qlteam2 = -1 if prov == "MAPUTO CID."
replace qlteam2 = -1 if prov == "NIASSA"

gen qlteam3 = 1 if prov == "CABO DELGADO"
replace qlteam3 = 1 if prov == "ZAMBEZIA"
replace qlteam3 = 0 if prov == "TETE"
replace qlteam3 = 1 if prov == "GAZA"
replace qlteam3 = 0 if prov == "MANICA"
replace qlteam3 = 0 if prov == "INHAMBANE"
replace qlteam3 = 0 if prov == "NAMPULA"
replace qlteam3 = 0 if prov == "SOFALA"
replace qlteam3 = -1 if prov == "MAPUTO PROV."
replace qlteam3 = -1 if prov == "MAPUTO CID."
replace qlteam3 = -1 if prov == "NIASSA"

gen qlteam4 = 1 if prov == "CABO DELGADO"
replace qlteam4 = 1 if prov == "ZAMBEZIA"
replace qlteam4 = 0 if prov == "TETE"
replace qlteam4 = 1 if prov == "GAZA"
replace qlteam4 = 0 if prov == "MANICA"
replace qlteam4 = 0 if prov == "INHAMBANE"
replace qlteam4 = 0 if prov == "NAMPULA"
replace qlteam4 = 0 if prov == "SOFALA"
replace qlteam4 = -1 if prov == "MAPUTO PROV."
replace qlteam4 = 0 if prov == "MAPUTO CID."
replace qlteam4 = -1 if prov == "NIASSA"

gen qlteamr = 1 if prov == "CABO DELGADO"
replace qlteamr = 1 if prov == "ZAMBEZIA"
replace qlteamr = 0 if prov == "TETE"
replace qlteamr = 0 if prov == "GAZA"
replace qlteamr = 1 if prov == "MANICA"
replace qlteamr = -1 if prov == "INHAMBANE"
replace qlteamr = 0 if prov == "NAMPULA"
replace qlteamr = 0 if prov == "SOFALA"
replace qlteamr = 0 if prov == "MAPUTO PROV."
replace qlteamr = -1 if prov == "MAPUTO CID."
replace qlteamr = -1 if prov == "NIASSA"

gen qlteamm = 0 if prov == "CABO DELGADO"
replace qlteamm = 0 if prov == "ZAMBEZIA"
replace qlteamm = 0 if prov == "TETE"
replace qlteamm = 1 if prov == "GAZA"
replace qlteamm = -1 if prov == "MANICA"
replace qlteamm = 1 if prov == "INHAMBANE"
replace qlteamm = 0 if prov == "NAMPULA"
replace qlteamm = 0 if prov == "SOFALA"
replace qlteamm = -1 if prov == "MAPUTO PROV."
replace qlteamm = 0 if prov == "MAPUTO CID."
replace qlteamm = 0 if prov == "NIASSA"

gen educbis = educ
replace educbis = 0 if educ == .

count if ea2 == 194 & af == 18 & sp == 1
count if sp == 1

foreach v of var qlteam* {
	label variable `v' "Field team quality index for attrition analysis"
}

**********
* Wealth *
**********

gen sp2 = sp
replace sp = 0 if indselected == 0
sort ea2 af sp

* First merge for info in 2008
merge ea2 af sp using Out/wealth
tab _m indselected, mis
*Nonmerge are non identified hhds with sp > 0
drop if _m == 2
drop _m
replace sp = sp2 if indselected == 0
drop sp2
save temp\psia08_iaf_inddata.dta, replace

* Second merge for info in 2003
use Out/wealth, replace
drop wealth*08 wealthc08*
keep if sp == 0
save temp/wealth, replace

use temp\psia08_iaf_inddata.dta, replace
drop wealth*03 wealthc03*
sort ea2 af sp
merge ea2 af using temp/wealth
tab _m indselected, mis
drop _m

gen occtypehhh2 = occtypehhh
recode occtypehhh2 6 = 9

sort ea2 af sp
save temp\psia08_iaf_inddata.dta, replace

count if ea2 == 194 & af == 18 & sp == 1
count if sp == 1

******************************
* Knowledge of the community *
******************************
tab ea2

*use "C:\Documents and Settings\wb360030\My Documents\Mozambique PSIA data from P\PSIA data\Main Qs_stata\sec_h1.dta", replace
use "D:\Mozambique\Mozambique PSIA data from P\PSIA data\Main Qs_stata\sec_h1.dta", replace
sort ea2
collapse (mean) mh03=h03 , by(ea2)
replace mh03 = 2-mh03
sum mh03, detail
label variable mh03 "share of the households in the ea having answered yes to h03"
sort ea2
tab ea2

merge ea2 using temp\psia08_iaf_inddata.dta
tab _m
tab ea2 if _m == 2
drop _m


sort ea2 af sp x00a
save temp\psia08_iaf_inddata, replace

count if ea2 == 194 & af == 18 & sp == 1
count if sp == 1

*************************************
* Demographic composition of the hh *
*************************************

*use "C:\Documents and Settings\wb360030\My Documents\Mozambique PSIA data from P\PSIA data\Main Qs_stata\sec_b0", replace
use "D:\Mozambique\Mozambique PSIA data from P\PSIA data\Main Qs_stata\sec_b0", replace
*rename x0 x00a
sort ea2 af sp x00a
merge ea2 af sp x00a using temp\psia08_iaf_inddata.dta
tab _m indselected, mis
drop if _m == 1
drop _m
sort ea2 af sp x00a
save temp\psia08_iaf_inddata.dta, replace

gen attsch08 = 0 if c01 == 2
replace attsch08 = 2 - c05 if c01 == 1

gen scol03 = attsch if b103 > 6 & b103 < 15
sum scol03
tab scol03, mis

gen enf = b103 < 18 if b103 != .
gen ageprim = b103 > 5 & b103 <14 if b103 != .
gen agesec = b103 >13 & b103 <19 if b103 != .

gen b102b = b102 if b103 > 6 & b103 < 15
gen agescol03 = b103 > 6 & b103 < 15 if b103 !=.
gen agescol08 = b03 > 6 & b03 < 15 if b03 !=.
sort ea2 af sp

sort ea2 af sp x00a

save temp\psia08_iaf_inddata.dta, replace
collapse (mean) txscol03=scol03 mb102=b102b shenf=enf shageprim=ageprim shagesec=agesec (max) iagescol03=agescol03 , by(prov ea2 af)
sort ea2 af
merge ea2 af using temp\psia08_iaf_inddata.dta
tab _m hhselected, mis
drop _m
drop b102b

gen txscol03bis = txscol03
replace txscol03bis = 0 if iagescol03==0

gen occtypehhhb = occtypehhh 
replace occtypehhhb = 5 if occtypehhh >=5 & occtypehhh !=.

gen occtypej = occtype
replace occtypej = 8 if occtype == 9
replace occtypej = 1 if occtype == 2 | occtype == 3 | occtype == 4

gen occtypej2 = occtypej
replace occtypej2 = 2 if occtype == 2 | occtype == 3 | occtype == 4

count if ea2 == 194 & af == 18 & sp == 1
count if sp == 1

**************
* Regression *
**************

* 0-5 years

char b104[omit] 3
char b102[omit] 2

xi: probit indselected nbmembers hhhage shenf iagescol03 wealthc031 educhh txscol03bis maladhhh /*
*/ i.occtypehhhb b102 malad i.b102*i.b104 i.b103 /*
*/ i.iafstratumpsia basicpsiawt /*
*/ [pw=basicpsiawt] if b103 < 6 , cluster(ea2)
predict pselec05b if b103 < 7
* All the 0-5 y.o.  with b104 = 6 were respondents: probability = 1
replace pselec05b = 1 if b103 < 6 & b104 == 6 
outreg2	using "Note attrition 2\fs_weights" , label bdec(3) replace addstat(Pseudo R-square, e(r2_p))

* 6-10 years

gen b104610 = b104
replace b104610 = 5 if b104610 == 6 & b103 < 11 & b103 > 5
xi: probit indselected nbmembers hhhage shenf iagescol03 wealthc031 educhh txscol03bis maladhhh i.occtypehhhb /*
*/  b102 malad i.b102*i.b104 educbis i.rural*i.occtypej i.b103 /*
*/ i.iafstratumpsia basicpsiawt/*
*/ [pw=basicpsiawt] if b103 < 11 & b103 > 5, cluster(ea2)
predict pselec610b if b103 < 11 & b103 > 5, pr
*replace pselec610b = 1 if b103 < 11 & b103 > 5 & b104 == 6 & rural == 1
outreg2	using "Note attrition 2\fs_weights" , label bdec(3) addstat(Pseudo R-square, e(r2_p))

* 11-17b years

gen b1041117 = b104
replace b1041117 = 2 if b1041117 == 1 & b102 == 2
xi: probit indselected nbmembers hhhage shenf iagescol03 wealthc031 educhh txscol03bis maladhhh i.occtypehhhb /*
*/  b102 malad i.b102*i.b1041117 educbis i.rural*i.occtypej i.b103 /*
*/ i.iafstratumpsia basicpsiawt /*
*/ [pw=basicpsiawt] if b103 < 18 & b103 > 10, cluster(ea2)
predict pselec1117b if b103 < 18 & b103 > 10, pr
* All the 11-17 y.o. in the stratum 4233 were respondents: probability = 1
replace pselec1117b = 1 if b103 < 18 & b103 > 10 & iafstratumpsia == 4233
outreg2	using "Note attrition 2\fs_weights" , label bdec(3) addstat(Pseudo R-square, e(r2_p))

xi: probit indselected nbmembers hhhage shenf iagescol03 wealthc031 educhh txscol03bis maladhhh i.occtypehhhb /*
*/  b102 malad i.b102*i.b1041117 educbis i.rural*i.occtypej2 i.b103 /*
*/ i.iafstratumpsia basicpsiawt /*
*/ [pw=basicpsiawt] if b103 < 18 & b103 > 10, cluster(ea2)

* 18-25
* recode strata to have at least 1 selected among 18-25 per strata
gen iafstratumpsia1825 = iafstratumpsia
recode iafstratumpsia1825 2213 = 2226
* Same for occupations
gen occtype1825 = occtype
recode occtype1825 7 = 9
recode occtype1825 3 = 4 if rural == 1
gen occtype1825rural = occtype1825 * 10 + rural
xi: probit indselected nbmembers hhhage shenf iagescol03 wealthc031 educhh txscol03bis maladhhh i.occtypehhhb /*
*/  b102 malad i.b102*i.b104 educbis i.rural*i.occtype1825 i.b103 /*
*/ i.iafstratumpsia1825 basicpsiawt /*
*/ [pw=basicpsiawt] if b103 < 26 & b103 > 17, cluster(ea2)
*xi: probit indselected nbmembers hhhage shenf iagescol03 wealthc031 educhh txscol03bis maladhhh i.occtypehhhb /*
*/  b102 malad i.b102*i.b104 i.occtype1825|educbis i.rural*i.occtype1825 i.b103 /*
*/ i.iafstratumpsia1825 basicpsiawt /*
*/ [pw=basicpsiawt] if b103 < 26 & b103 > 17, cluster(ea2)
*xi: probit indselected nbmembers hhhage shenf iagescol03 wealthc031 educhh txscol03bis maladhhh i.occtypehhhb /*
*/  b102 malad i.b102*i.b104 i.occtype1825|educbis i.rural*i.occtype1825 i.b103 /*
*/ i.iafstratumpsia1825 basicpsiawt /*
*/ [pw=basicpsiawt] if b103 < 26 & b103 > 17, cluster(ea2)
predict pselec1825b if b103 < 26 & b103 > 17, pr
outreg2	using "Note attrition 2\fs_weights" , label bdec(3) addstat(Pseudo R-square, e(r2_p))

* 26-34
char b104[omit] 1

gen b102rural = 10*b102 + rural
gen b1042634 = b104
gen occtyperural = occtype * 10 + rural
*recode b1042634 6 = 3 if rural == 1
xi: probit indselected nbmembers hhhage shenf iagescol03 wealthc031 educhh txscol03bis maladhhh i.occtypehhhb /*
*/  b102 malad i.b102*i.b1042634 educbis i.rural*i.occtype i.b103 /*
*/ i.iafstratumpsia basicpsiawt /*
*/ [pw=basicpsiawt] if b103 < 35 & b103 > 25, cluster(ea2)
*xi: probit indselected nbmembers hhhage shenf iagescol03 wealthc031 educhh txscol03bis maladhhh i.occtypehhhb /*
*/  b102 malad i.b102*i.b1042634 i.occtype|educbis i.rural*i.occtype i.b103 /*
*/ i.iafstratumpsia basicpsiawt /*
*/ [pw=basicpsiawt] if b103 < 35 & b103 > 25, cluster(ea2)
predict pselec2634b if b103 < 35 & b103 > 25, pr
outreg2	using "Note attrition 2\fs_weights" , label bdec(3) addstat(Pseudo R-square, e(r2_p))

* 35-49

gen occtype3549 = occtype
* recode occupation to have at least 1 selected among 35-49 per occupation
recode occtype3549 6 = 9
recode occtype3549 9 = 8 if rural == 1
gen b1043549 = b104
recode b1043549 6 = 3 

xi: probit indselected nbmembers hhhage shenf iagescol03 wealthc031 educhh txscol03bis maladhhh i.occtypehhhb /*
*/  b102 malad i.b102*i.b1043549 educbis i.rural*i.occtype3549 i.b103 /*
*/ i.iafstratumpsia basicpsiawt /*
*/ [pw=basicpsiawt] if b103 < 50 & b103 > 34, cluster(ea2)
*xi: probit indselected nbmembers hhhage shenf iagescol03 wealthc031 educhh txscol03bis maladhhh i.occtypehhhb /*
*/  b102 malad i.b102*i.b1043549 i.occtype3549|educbis i.rural*i.occtype3549 i.b103 /*
*/ i.iafstratumpsia basicpsiawt /*
*/ [pw=basicpsiawt] if b103 < 50 & b103 > 34, cluster(ea2)
predict pselec3549b if b103 < 50 & b103 > 34, pr
outreg2	using "Note attrition 2\fs_weights" , label bdec(3) addstat(Pseudo R-square, e(r2_p))

* 50+

*recode age to have reasonable sample by age group
gen b1035199 = b103
recode b1035199 81 = 80
recode b1035199 82 = 80
recode b1035199 83 = 80
recode b1035199 84 = 80
recode b1035199 86 = 85
recode b1035199 87 = 85
recode b1035199 88 = 85
recode b1035199 89 = 85
replace b1035199 = 85 if b1035199 >= 90
gen occtype5199 = occtype
recode occtype5199 6 = 9
gen iafstratumpsia5199 = iafstratumpsia
recode iafstratumpsia5199 2101 = 3103
recode iafstratumpsia5199 3199 = 3103
recode iafstratumpsia5199 3199 = 3103
recode iafstratumpsia5199 4233 = 4224

tab indselected hhselected if b103 >= 50, column
replace indselected = 1 if b110 == 4 & b103 >= 50
tab indselected hhselected if b103 >= 50, column

xi: probit indselected nbmembers hhhage shenf iagescol03 wealthc031 educhh txscol03bis maladhhh i.occtypehhhb /*
*/  b102 malad i.b102*i.b1043549 educbis i.rural*i.occtype5199 i.b1035199 /*
*/ i.iafstratumpsia5199 basicpsiawt /*
*/ [pw=basicpsiawt] if b103 < 99 & b103 > 49, cluster(ea2)
*xi: probit indselected nbmembers hhhage shenf iagescol03 wealthc031 educhh txscol03bis maladhhh i.occtypehhhb /*
*/  b102 malad i.b102*i.b1043549 i.occtype5199|educbis i.rural*i.occtype5199 i.b1035199 /*
*/ i.iafstratumpsia5199 basicpsiawt /*
*/ [pw=basicpsiawt] if b103 < 99 & b103 > 49, cluster(ea2)
predict pselec5099b if b103 < 99 & b103 > 49, pr
replace pselec5099b = 1 if b1035199 == 77

gen pselecb = pselec05b if b103 < 6
replace pselecb = pselec610b if b103 < 11 & b103 > 5
replace pselecb = pselec1117b if b103 < 18 & b103 > 10
replace pselecb = pselec1825b if b103 < 26 & b103 > 17
replace pselecb = pselec2634b if b103 < 35 & b103 > 25
replace pselecb = pselec3549b if b103 < 50 & b103 > 34
replace pselecb = pselec5099b if b103 > 49

gen psiaselecwt_inb = basicpsiawt/pselecb
sum  psiaselecwt_inb pselecb, detail
replace psiaselecwt_inb = 50000 if psiaselecwt_inb > 50000 & psiaselecwt_inb <.

label variable psiaselecwt_inb "Inverse Probability weighting for individuals, EOPS, Feb/25/2010"

rename psia_wt eops_wt
rename basicpsiawt basiceopswt 
rename psiaselecwt_inb eopsselecwt_inb
 
save temp/weights, replace
keep ea2 af sp x00a b100 *wt* *weigh* case_id pind indselected
sort ea2 af b100
save temp/indweightsout, replace
save Out/indweights, replace

use temp/weights, replace

count if ea2 == 194 & af == 18 & sp == 1
count if sp == 1

****************************
* Regression by households *
****************************

sort ea2 af sp x00a
save temp\psia08_iaf_inddata, replace

*use "C:\Documents and Settings\wb360030\My Documents\Mozambique PSIA data from P\PSIA data\Main Qs_stata\sec_j1"
use "D:\Mozambique\Mozambique PSIA data from P\PSIA data\Main Qs_stata\sec_j1"
sort case_id
save temp/sec_j1, replace

*use "C:\Documents and Settings\wb360030\My Documents\Mozambique PSIA data from P\PSIA data\Main Qs_stata\sec_b0", replace
use "D:\Mozambique\Mozambique PSIA data from P\PSIA data\Main Qs_stata\sec_b0", replace
*rename x0 x00a
sort ea2 af sp x00a
merge ea2 af sp x00a using temp\psia08_iaf_inddata.dta
tab _m indselected, mis
drop if _m == 1
drop _m
sort ea2 af sp x00a
save temp\psia08_iaf_inddata.dta, replace

*use "C:\Documents and Settings\wb360030\My Documents\Mozambique PSIA data from P\PSIA data\Main Qs_stata\sec_c1", replace
use "D:\Mozambique\Mozambique PSIA data from P\PSIA data\Main Qs_stata\sec_c1", replace
rename c100 x00a
keep if c101 == 2008
tostring x00a, replace
sort ea2 af sp x00a
merge ea2 af sp x00a using temp\psia08_iaf_inddata.dta
tab _m indselected, mis
drop if _m == 1
drop _m

sort ea2 af sp case_id x00a
*gen agescol08 = b03 > 5 & b03 < 18 if b03 !=.
by ea2 af sp: egen iagescol08 = max(agescol08 )
replace iagescol08 = 0 if hhselected == 0
replace iagescol08 = . if sp > 0

*gen agescol03 = b103 > 5 & b103 < 18 if b103 !=.
*by ea2 af : egen iagescol03 = max(agescol03 )

foreach v of var hhselected mh03 educhh educhhh b102 maladhh maladhhh hhhage wealth* nbmembers occtypehhh* iafstratumpsia rural /*
*/ eligiblehh ia05hh ia613hh ia1420hh ia617hh ia5199hh ia1825hh ia1417hh ia1820hh ia1217hh ia611hh *wt* *weigh* /*
*/ shenf txscol03bis sp iagescol0* {
	local l`v' : variable label `v'
      if `"`l`v''"' == "" {
		local l`v' "`v'"
	}
}
collapse hhselected mh03 educhh educhhh mb102=b102 maladhh maladhhh hhhage wealth* nbmembers occtypehhh* iafstratumpsia rural /*
*/ eligiblehh ia05hh ia613hh ia1420hh ia617hh ia5199hh ia1825hh ia1417hh ia1820hh ia1217hh ia611hh *wt* *weigh* /*
*/ shenf txscol03bis sp (max) iagescol0* , by(ea2 af case_id)
foreach v of var hhselected mh03 educhh educhhh maladhh maladhhh hhhage wealth* nbmembers occtypehhh* iafstratumpsia rural /*
*/ eligiblehh ia05hh ia613hh ia1420hh ia617hh ia5199hh ia1825hh ia1417hh ia1820hh ia1217hh ia611hh *wt* *weigh* /*
*/ shenf txscol03bis sp {
	label var `v' "(hh mean) `l`v''"
}
foreach v of var iagescol0* {
	label var `v' "(hh max) `l`v''"
}
foreach v in b102 {
	label var m`v' "(hh max) `l`v''"
}

tab sp, mis
sort case_id
merge case_id using temp/sec_j1, _merge(mergetrans)
drop i0* j1
tab mergetrans sp, mis
gen hhselectedtra = mergetrans == 3 if hhselected < .
drop mergetrans

xi: probit hhselected educhhh mb102 maladhhh hhhage wealthc031 nbmembers /*
*/ i.occtypehhh2 eligiblehh ia05hh ia613hh ia1420hh ia5199hh /*
*/ i.iafstratumpsia basiceopswt /*
*/ [pw=basiceopswt], cluster(ea2)

xi: probit hhselected educhhh mb102 maladhhh hhhage wealthc031 nbmembers /*
*/ i.occtypehhh2 eligiblehh ia05hh ia613hh ia1417hh ia1820hh ia5199hh /*
*/ i.iafstratumpsia basiceopswt /*
*/ [pw=basiceopswt], cluster(ea2)

xi: probit hhselected educhhh mb102 maladhhh hhhage wealthc031 nbmembers /*
*/ i.occtypehhh2 eligiblehh ia05hh ia613hh ia1417hh ia5199hh /*
*/ i.iafstratumpsia basiceopswt /*
*/ [pw=basiceopswt], cluster(ea2)

xi: probit hhselected educhhh mb102 maladhhh hhhage wealthc031 nbmembers /*
*/ i.occtypehhh2 eligiblehh ia05hh ia611hh ia1217hh ia1820hh ia5199hh /*
*/ i.iafstratumpsia basiceopswt /*
*/ [pw=basiceopswt], cluster(ea2)

xi: probit hhselected educhhh mb102 maladhhh hhhage wealthc031 nbmembers /*
*/ i.occtypehhh2 eligiblehh ia05hh ia611hh ia1217hh ia5199hh /*
*/ i.iafstratumpsia basiceopswt /*
*/ [pw=basiceopswt], cluster(ea2)

xi: probit hhselected educhhh mb102 maladhhh hhhage wealthc031 nbmembers /*
*/ i.occtypehhh2 eligiblehh ia05hh ia617hh ia1825hh ia5199hh /*
*/ i.iafstratumpsia basiceopswt /*
*/ [pw=basiceopswt], cluster(ea2)

xi: probit hhselected nbmembers hhhage shenf iagescol03 wealthc031 educhh txscol03bis maladhhh i.occtypehhhb eligiblehh /*
*/ i.iafstratumpsia basiceopswt /*
*/ [pw=basiceopswt], cluster(ea2)
predict pselec, pr
gen psiaselecwt_hh = basiceopswt/pselec
outreg2	using "Note attrition 2\fs_weights" , label bdec(3) addstat(Pseudo R-square, e(r2_p))

xi: probit hhselectedtra nbmembers hhhage shenf iagescol03 wealthc031 educhh txscol03bis maladhhh i.occtypehhhb eligiblehh /*
*/ i.iafstratumpsia basiceopswt /*
*/ [pw=basiceopswt], cluster(ea2)
predict pselectra, pr
gen psiaselecwt_hhtra = basiceopswt/pselectra

tab hhselected hhselectedtra, mis

xi: probit hhselected educhhh mb102 maladhhh hhhage wealthc031 nbmembers /*
*/ i.occtypehhh2 /*
*/ i.iafstratumpsia basiceopswt /*
*/ [pw=basiceopswt] if iagescol03 == 1, cluster(ea2)
xi: probit iagescol08 nbmembers hhhage shenf iagescol03 wealthc031 educhh txscol03bis maladhhh i.occtypehhhb eligiblehh /*
*/ i.iafstratumpsia basiceopswt /*
*/ [pw=basiceopswt] if iagescol03 == 1, cluster(ea2)
predict pselec2, pr
replace pselec2 = 1 if iafstratumpsia == 1224 | iafstratumpsia == 2101 | iafstratumpsia == 3103 | iafstratumpsia == 3226 | iafstratumpsia == 4102 | /*
*/ iafstratumpsia == 4224 | iafstratumpsia == 4233 | iafstratumpsia == 5241 | iafstratumpsia == 5242 | iafstratumpsia == 7226 | iafstratumpsia == 9233 | /*
*/ iafstratumpsia == 9241 | iafstratumpsia == 10233 
gen psiaselecwt_educhh = basiceopswt/pselec2

label variable psiaselecwt_hh "Inverse Probability weighting for households, EOPS, Feb/25/2010"
label variable psiaselecwt_educhh "Inverse Probability weighting for households, EOPS, for child educ panel"

keep ea2 af psiaselecwt_hh psiaselecwt_hhtra psiaselecwt_educhh *wt* *weigh* case_id
drop  eopsselecwt_inb

drop psia_wt 
*rename basiceopswt basiceopswt 
rename psiaselecwt_hh eopsselecwt_hh 
rename psiaselecwt_hhtra eopsselecwt_hhtra 
rename psiaselecwt_educhh eopsselecwt_educhh 

sort ea2 af 
save Out/hhweights, replace

use Out/indweights, replace
save temp/weights, replace
