cd "G:\Il mio Drive\Multifonte\AA 2025_26\Simulazione 1" use dn_wave1.dta,clear gen ID=_n gen yrbirth=dn003 gen mobirth=dn002 gen gender=dn042 gen interview=1 *tutti i record hanno mergeid = missing tab dn042 if mergeid=="" tab dn042 if mergeid!="" *10575 hanno hhid = missing tab dn042 if hhid=="" *13338 hanno coupleid = missing tab dn042 if coupleid=="" *8160 hanno sia hhid sia coupleid= missing tab dn042 if hhid=="" & coupleid=="" save dn_wave11.dta,replace ********************************************************************************* * STEP 1 ********************************************************************************* ********************************************************************************* clear use sharew1_rel8-0-0_cv_r.dta ***************************** tab interview if mergeid=="" tab interview if coupleid=="" tab interview if hhid=="" ***************************** gen id=_n reclink2 hhid1 interview gender yrbirth using dn_wave11.dta, /// gen(myscore) idm(id) idu(ID) minscore(.9) require( hhid1 interview) npairs(1) upr(_) ********************************** tab _merge interview tab myscore interview tab myscore interview if hhid!="" ********************************** bys hhid interview gender yrbirth: gen N=_n bys hhid interview gender yrbirth: egen N1=max(N) tab myscore N1 tab myscore N1 if hhid=="" br mobirth hhid1 interview gender yrbirth dn002 dn003 if N1==2 & myscore==1 ***************************************** gen nomatch=1 if (_merge==1 & interview==1) | (_merge==3 & N1!=1) tab nomatch interview , mi drop if nomatch==1 save step1.dta, replace ******************************************************************************************* * STEP 2 ******************************************************************************************* ******************************************************************************************* clear use sharew1_rel8-0-0_cv_r.dta gen id=_n reclink2 coupleid1 interview gender yrbirth country using dn_wave11.dta, /// gen(myscore2) idm(id) idu(ID) minscore(.9) require(coupleid1) npairs(1) upr(_) /// exclude(step1.dta) _merge(_merge2) tab _merge myscore bys coupleid1 interview gender yrbirth country: gen N=_n bys coupleid1 interview gender yrbirth country: egen N1=max(N) tab N1 myscore tab N1 myscore if coupleid!="" ******************************************************************************** drop if N1>1 | _merge==1 tab gender append using step1.dta, gen(step) recode step(0=2) save step1.dta, replace ******************************************************************************************* * STEP 3 ******************************************************************************************* ******************************************************************************************* clear use dn_wave11 tab1 language country int_year int_month gender mobirth yrbirth clear use sharew1_rel8-0-0_cv_r.dta, clear tab1 language country int_year int_month gender mobirth yrbirth ******************************** gen id=_n reclink2 interview gender mobirth yrbirth country language int_year int_month using dn_wave11.dta, /// gen(myscore3) idm(id) idu(ID) minscore(.7) require(country) npairs(1) upr(_) /// exclude(step1.dta) _merge(_merge3) wmatch(10 6 7 6 10 5 5 5) wnomatch(8 7 4 5 10 5 7 3) tab _merge3 myscore3 bys int_year int_month interview gender mobirth yrbirth country language: gen N=_n by int_year int_month interview gender mobirth yrbirth country language: egen N1=max(N) tab N1 myscore br myscore3 int_year _int_y int_month _int_m yrbirth _yrbirth mobirth _mobirth country _country if N1==1 & myscore3<1 ****************************** drop if N1>1 | _merge==1 | myscore<.9 tab gender append using step1.dta, gen(step2) recode step2(0=3) tab step step2 if interview==1,mi ********************************************************************* /***********esercizio 2 ********************************************* */ use healthW1.dta *elimino i missing per costruzione che rigurdano coloro che non sono stati intervistai * per quanto riguarda la variabile ph009_1 imputerò i missing solo di coloro che hanno avuto un attaco cardiaco drop if interview!=1 keep ph009_1 age_int gender numeracy country dn014 iv007_ ph006d1 misstable sum fre iv007_ *la moda è 2= Almost never recode iv007_ (min/-1=.) recode iv007_(.=2) *ricodifico con la moda fre dn014_ *la moda è 6=vedovo recode dn014_(min/-1=.) recode dn014_(.=6) *ricodifico con la moda misstable sum recode ph009_1 numeracy (min/-1=.) codebook ph009_1 numeracy *missing "reali" di ph004_ 10,047/30,416 *missing "reali" di numeracy 5,241/30,416 *missing di ph009_1 27,026/30,416, *questi sono anche missing per costruzione perche sto considerando anche *chi non ha mai avuto un attacco cardiaco tab ph009_1 if ph006d1==1,mi *337 missing veri ******modello di imputazione hist ph009_1, norm recode ph006d1(-1 -2 .=0) *ph009_1 normale troncata *ph004_ logit *numeracy ologit recode ph009_1 (min/max=0) if ph006d1!=1 mi set mlong *scelgo il formato in cui salvare i dati mi register imputed ph009_1 numeracy *variabili da imputare mi register regular age_int gender country dn014 iv007_ mi register passive ph006d1 * (logit) ph004_ //// ********************************************************************************* ********************************************************************************* set more off mi impute chained /// (reg if ph006d1==1)ph009_1 //// (ologit) numeracy = age_int gender i.country i.dn014 iv007_ , add(5) rseed(53421) force *numeracy: missing imputed values produced * This may occur when imputation variables are used as independent variables or * when independent variables contain missing values. You can specify option force * if you wish to proceed anyway. midiagplots ph009_1, m(1/3) combine midiagplots numeracy , m(1/3) combine * sottostimiamo coloro che non hanno long term illness ************************************************************ *modello con i soli valori osservati/ completi mi xeq 0:ologit numeracy age_int gender country i.dn014 * -.2347198 *modello con i valori imputati nei M dataset mi estimate, dots: ologit numeracy age_int gender country i.dn014 ************************************************************* mimrgns,dydx( dn014) predict(outcome(5)) mi extract 0,clear ologit numeracy age_int gender country i.dn014 margins, dydx(dn014) predict(outcome(5))