clear
set more off

cd "C:\Documents\IFLS"

global data "C:\Documents\IFLS\Data"
global output "C:\Documents\IFLS\Output"
global log "C:\Documents\IFLS\log"

global hh14 "C:\Documents\IFLS\Data\2014\hh"

log using "$log\relighappinesspolicy", replace

//x1 relig (b3a)//
use "$hh14\b3a_tr.dta", replace
desc
keep hhid14 pid14 pidlink tr11
tab tr11
recode tr11 (1/3=1) (4/7=0)
rename tr11 religius
tab religius
label define religius 0"tidak religius" 1"religius"
label value religius religius
keep hhid14 pid14 pidlink religius
save "$output\variabelreligius", replace

//tempat tinggal (bk)//
use "$hh14\bk_sc1.dta", clear
desc
order hhid14 sc05
keep hhid14 sc05
tab sc05, m
gen urban= (sc05==1)
label define urban 1"perkotaan" 0"perdesaan"
label values urban urban
keep hhid14 urban
save "$output\variabeltempattinggal.dta", replace

//x2 islameduc (bk)//
//gender//
//maritalstat//
//educyear//
use "$hh14\bk_ar1.dta", clear
tab ar07
recode ar07 (1=1) (3=0)
gen sex= ar07
label define sex 1"male" 0"female"
label values sex sex

tab ar13
gen marstat= (ar13==2)
tab marstat, m
label define marstat 1"married" 0"not married"
label values marstat marstat

tab ar16 ar17
drop if ar16==95 | ar16==98 | ar16==99
generate educyear=0 if ar16==1 | ar16==90 | ar16==2 & ar17==0 | ar16==11 & ar17==0 | ar16==72 & ar17==0

replace  educyear=1 if ar16==2 & ar17==1 | ar16==11 & ar17==1 | ar16==72 & ar17==1

replace  educyear=2 if ar16==2 & ar17==2 | ar16==11 & ar17==2 | ar16==72 & ar17==2

replace  educyear=3 if ar16==2 & ar17==3 | ar16==11 & ar17==3 | ar16==72 & ar17==3

replace  educyear=4 if ar16==2 & ar17==4 | ar16==11 & ar17==4 | ar16==72 & ar17==4

replace  educyear=5 if ar16==2 & ar17==5 | ar16==11 & ar17==5 | ar16==72 & ar17==5

replace  educyear=6 if ar16==2 & (ar17==6 | ar17==7) | ar16==11 & (ar17==6 | ar17==7) | ar16==72 & (ar17==6 | ar17==7) | ar16==3 & ar17==0 | ar16==4 & ar17==0 | ar16==12 & ar17==0 | ar16==73 & ar17==0

replace  educyear=7 if ar16==3 & ar17==1 | ar16==12 & ar17==1 | ar16==73 & ar17==1 | ar16==4 & ar17==1

replace  educyear=8 if ar16==3 & ar17==2 | ar16==12 & ar17==2 | ar16==73 & ar17==2 | ar16==4 & ar17==2

replace  educyear=9 if ar16==3 & (ar17==3 | ar17==7) | ar16==12 & (ar17==3 | ar17==7) | ar16==73 & (ar17==3 | ar17==7) | ar16==4 & (ar17==3 | ar17==7) | ar16==5 & ar17==0 | ar16==6 & ar17==0 | ar16==15 & ar17==0 | ar16==74 & ar17==0

replace educyear=10 if ar16==5 & ar17==1 | ar16==6 & ar17==1 | ar16==15 & ar17==1 | ar16==74 & ar17==1

replace educyear=11 if ar16==5 & ar17==2 | ar16==6 & ar17==2 | ar16==15 & ar17==2 | ar16==74 & ar17==2

replace educyear=12 if ar16==5 & (ar17==3 | ar17==7) | ar16==6 & (ar17==3 | ar17==7) | ar16==15 & (ar17==3 | ar17==7) | ar16==74 & (ar17==3 | ar17==7) | ar16==13 & ar17==0 | ar16==60 & ar17==0 | ar16==61 & ar17==0

replace educyear=16 if ar16==13 & (ar17==1 | ar17==2 | ar17==3 | ar17==4 | ar17==7) | ar16==60 & (ar17==1 | ar17==2 | ar17==3 | ar17==4 | ar17==7) | ar16==61 & (ar17==1 | ar17==2 | ar17==3 | ar17==4 | ar17==7) | ar16==62 | ar16==63

gen     educ=1 if educyear>=0 & educyear<7
replace educ=2 if educyear>=7 & educyear<=12
replace educ=3 if educyear>12
tab educ
label define educ 1"tidaksekolah dan SD" 2"SMP dan SMA" 3"Kuliah"
label values educ educ
save "$output\variabeleduc", replace
keep hhid14 pid14 pidlink sex marstat educ educyear
save "$output\variabelgendereducmarstat", replace

//famsize//
use "$hh14\bk_ar1.dta", clear
duplicates report pidlink
duplicates report hhid14 pid14
//(1 copies = tidak ada duplikat, 2 copies = 1 duplikat, dst.)
bys pidlink: gen dup_pidlink= _N
br hhid14 pid14 pidlink dup_pidlink
gen member_ar= (ar01a==1 | ar01a==2 | ar01a==5 | ar01a==11)
bys hhid14: egen famsize=sum(member_ar)
tab famsize, m
keep hhid14 pid14 pidlink famsize
save "$output\variabelfamsize", replace

//kebahagiaan//
use "$hh14\b3a_sw.dta", clear
desc
tab sw12
recode sw12 (1/2=1) (3/4=0)
rename sw12 kebahagiaan
tab kebahagiaan
label define kebahagiaan 0"tidak bahagia" 1"bahagia"
label value kebahagiaan kebahagiaan
keep hhid14 pid14 pidlink kebahagiaan
save "$output\variabelkebahagiaan", replace

**MERGE**
use "$output\variabelreligius", clear
merge 1:1 hhid14 pid14 using "$output\variabelgendereducmarstat"
drop if _merge !=3
drop _merge
save "$output\cleansekolahstata.dta", replace

merge 1:1 hhid14 pid14 using "$output\variabelkebahagiaan"
drop if _merge !=3
drop _merge
save "$output\cleansekolahstata.dta", replace

merge m:1 hhid14 using "$output\variabeltempattinggal.dta"
drop if _merge !=3
drop _merge
save "$output\cleansekolahstata.dta", replace

merge 1:1 hhid14 pid14 using "$output\variabelfamsize"
drop if _merge !=3
drop _merge
save "$output\cleansekolahstata.dta", replace

**REGRESSION**
use "$output\cleansekolahstata.dta", replace
desc
sum
keep kebahagiaan religius sex urban marstat educ educyear famsize

reg kebahagiaan religius sex urban marstat educ famsize
estat hettest
//terdapat hetero//
estat vif
//tidak terdapat multikol//

probit kebahagiaan religius sex urban marstat educ famsize, robust
margins, dydx(_all)

probit kebahagiaan religius sex urban marstat educ famsize
margins, dydx(_all)

save "$output\resultsekolahstata.dta", replace
log close