txt"Import data" import delimited "/Users/matthewnudelman/Desktop/Storage/School/UCSF/2019 8. Spring/EPI265/HW/HW9/Book1.csv", encoding(ISO-8859-1) clear txt"Treatment = Alendronate" txt"Mediator = Bone Mineral Density" txt"Outcome = Vertebral fractures" txt"Create treatment (Alendronate)" gen newtx = 0 if tx =="Placebo" replace newtx = 1 if tx =="Alendronate" drop tx rename newtx tx txt"Create outcome (spinal fracture)" gen new_vfx2 = 0 if new_vfx =="no" replace new_vfx2 = 1 if new_vfx =="yes" drop new_vfx rename new_vfx2 new_vfx txt"Create mediator (Bone mineral density BMD)" gen bmd_ch = fwp_bmd - bl_bmd txt"Select 10,000 sample size" sample 10000, count txt"Baron and Kenny approach" txt"First I'll calculate the DIRECT effect" logistic new_vfx i.tx bmd_ch matrix define ebd = e(b) scalar direct=ebd[1,2] di exp(direct) txt"Next, I'll calculate the TOTAL effect " logistic new_vfx i.tx matrix define ebt = e(b) scalar total=ebt[1,2] di exp(total) txt"Last, I'll calculate the % mediated with confounder using logit coefficients" di (total-direct)/total txt"Calculating Controlled Direct Effect (CDE) " txt"I'll do this by fitting two models about how the treatment effects 1) the mediator and 2) the outcome" qui regress bmd_ch i.tx qui logistic new_vfx i.tx txt"Create interaction term between mediator and treatment" gen tx_bmd_ch = tx*bmd_ch txt"Using the Medeff command" medeff /// (regress bmd_ch tx) /// (logit new_vfx tx bmd_ch tx_bmd_ch), /// mediate(bmd_ch) treat(tx) interact(tx_bmd_ch)