set more off clear cd "C:\Users\monic\Documents\2019_Spring\EPI265 Epi methods 3" capture log close log using "HW7", replace set seed 713894 clear all set seed 713894 set obs 10000 // population gen id=_n * Generate the binary exposure (ApoE4 prevalence 0.04) gen apoe4 = runiform()< 0.04 tab apoe4 * Generate the confounder of the mediator-outcome association gen genx = runiform()< 0.10 tab genx * Generate the continous mediator variable (Neurofibrilary tangles) gen nft = 5 + (9*apoe4) + (-2*genx) + rnormal() sum nft * Generate the continuous outcome variable (cognitive function) gen cognition = (-0.4*apoe4) + (-0.01*nft) + (0.1*genx) + runiform() sum cognition * Direct effect of ApoE4 on Cognition, not mediated by m is -0.4 * Indirect effect of ApoE4 on Cognition, via m is -0.09 (-0.01*9) * Total effect of ApoE4 on cognition is -0.4+ -0.09 = -0.49 and there is no interaction between m and x * Estimate of the direct effect of ApoE4 with and without the confounder genx regress cognition apoe4 // total effect scalar total_effect_est =_b[apoe4] regress cognition apoe4 nft // direct effect without the genx scalar biased_direct_effect_est=_b[apoe4] regress cognition apoe4 nft genx // direct effect scalar direct_effect_est=_b[apoe4] dis direct_effect_est dis in red "indirect effect estimated w/o control for c is: " round(total_effect_est-biased_direct_effect_est,.001) dis in red "indirect effect estimated w/ control for c is: " round(total_effect_est-direct_effect_est,.001) dis in red "total effect is: " round(total_effect_est,.001) * Bad measurement of the mediator gen badnft = nft + rnormal() sum badnft * estimates regress cognition apoe4 // total effect scalar total_effect_est2 =_b[apoe4] regress cognition apoe4 badnft // direct effect without the genx scalar biased_direct_effect_est2=_b[apoe4] regress cognition apoe4 badnft genx // direct effect scalar direct_effect_est2=_b[apoe4] dis direct_effect_est2 dis in red "indirect effect estimated w/o control for c is: " round(total_effect_est2-biased_direct_effect_est2,.001) dis in red "indirect effect estimated w/ control for c is: " round(total_effect_est2-direct_effect_est2,.001) dis in red "total effect is: " round(total_effect_est2,.001) * controlled direct effects preserve expand 3 sort id by id: gen copy=_n replace nft=0 if copy==2 replace apoe4=0 if copy==2 replace cognition=. if copy==2 replace nft=0 if copy==3 replace apoe4=1 if copy==3 replace cognition=. if copy==3 gen apo_n = apoe4*nft regress cognition apoe4 nft apo_n genx if copy==1 predict cf_y_x0_m0 if copy==2 predict cf_y_x1_m0 if copy==3 * What's the average value of y if x is set to 0 and m is set to 0? sum cf_y_x0_m0 if copy==2 scalar mean_cf_y_x0_m0=r(mean) * What the average potential outcome for y if x is set to 1 and m is set to 0? sum cf_y_x1_m0 if copy==3 scalar mean_cf_y_x1_m0=r(mean) dis "Estimated direct effect of apoe4, setting m to 0, is: " round(mean_cf_y_x1_m0-mean_cf_y_x0_m0,.001) *Natural indirect effects restore preserve expand 3 sort id by id: gen copy=_n replace nft=. if copy==2 replace apoe4=0 if copy==2 replace cognition=. if copy==2 replace nft=. if copy==3 replace apoe4=1 if copy==3 replace cognition=. if copy==3 gen apo_n = apoe4*nft if copy==1 regress nft apoe4 genx if copy==1 predict cf_m_x0 if copy==2 predict cf_m_x1 if copy==3 replace nft=cf_m_x0 if copy==2 replace nft=cf_m_x1 if copy==3 replace apoe4=1 if copy==2 replace apo_n = apoe4*nft regress cognition apoe4 nft apo_n genx if copy==1 predict cf_y_x1_cf_m_x0 if copy==2 predict cf_y_x1_cf_m_x1 if copy==3 sum cf_y_x1_cf_m_x0 if copy==2 scalar mean_cf_y_x1_cf_m_x0=r(mean) sum cf_y_x1_cf_m_x1 if copy==3 scalar mean_cf_y_x1_cf_m_x1=r(mean) dis "the natural indirect effect of x on y, mediated by m is: " round(mean_cf_y_x1_cf_m_x1-mean_cf_y_x1_cf_m_x0,.001) restore gen apo_n = apoe4*nft medeff (regress nft apoe4 genx) (regress cognition apoe4 nft apo_n genx) /// , mediate(nft) treat(apoe4) interact(apo_n)