set more off set scheme s1mono * Figure 9.1 set seed 9896 clear all set obs 200 gen age = 40+40*uniform() gen smoking = uniform()<1/(1+exp(-7.5+0.14*age)) replace smoking=0 if age>70 replace smoking=1 if age<50 gen risk = 0.7 + 0.005*(age-55) if smoking==1 replace risk = 0.55 + 0.03*(age-55) if smoking==0 gen dead = uniform()1" label values ppv ppv label variable ppv "Possible positivity violation" tab phototherapy ppv, row logistic over_thresh i.phototherapy lps_rcs* if ppv==0, cluster(hospital) margins phototherapy matrix b = r(b) scalar EY0 = b[1, 1] scalar EY1 = b[1, 2] * Marginal odds-ratio scalar marginal_OR = EY1/(1-EY1)*(1-EY0)/EY0 scalar list marginal_OR * Marginal risk difference margins, dydx(phototherapy) margins r.phototherapy * Table 9.17 capture drop lps100 lps_rcs* gen lps100 = logit_ps*100 qui mkspline lps_rcs = lps100, cubic qui logistic over_thresh i.phototherapy lps_rcs*, cluster(hospital) margins phototherapy, subpop(phototherapy) matrix b = r(b) scalar EY0 = b[1, 1] scalar EY1 = b[1, 2] * Marginal odds-ratio display EY1/(1-EY1)*(1-EY0)/EY0 * Marginal risk difference margins, dydx(phototherapy) subpop(phototherapy) margins r.phototherapy, subpop(phototherapy) * Table 9.18 set seed 9896 capture drop order gen order = uniform() sort order psmatch2 phototherapy, out(over_thresh) pscore(prop_score) noreplace * Table 9.19 gen smrw = phototherapy + (1-phototherapy)*prop_score/(1-prop_score) gen big_smrw = smrw>20 tab big_smrw sum smrw logistic over_thresh i.phototherapy [pweight=smrw], cluster(hospital) * Marginal risk difference margins, dydx(phototherapy) margins r.phototherapy * Table 9.20 use fitdata, clear gen delta_bmd=cobmd-blbmd mkspline age_spl=age, cubic nknots(3) gen treat = tx qui summ blbmd gen bmd_base = (blbmd/r(sd)) qui summ cobmd gen bmd_post = (cobmd/r(sd)) gen bmdd = cobmd-blbmd qui summ bmdd gen bmd_diff = bmdd/r(sd) gen frac_new = newvfx gen frac_base = blvfx gen ofrac_base = blnspfx * controlled direct effect estimate quietly logistic frac_new i.treat bmd_diff bmd_base i.frac_base i.smoking age_spl* margins treat, at(bmd_diff==0) margins, dydx(treat) at(bmd_diff==0) margins r.treat, at(bmd_diff==0) * Table 9.21 use hersdata, clear set seed 9896 gen ldlch = LDL1-LDL reg ldlch HT predict res, resid * unmeasured confounder associated with LDL change gen U = res + 30*invnorm(uniform()) * percent compliance depends on U gen HT_use = normal(-4 + 6*HT - 0.02*U + 0.5*invnorm(uniform())) tab HT, sum(HT_use) reg ldlch HT reg ldlch HT_use ivregress 2sls ldlch (HT_use = HT) estat endogenous estat firststage /* numbers don't match exactly -- I must not have set the seed in the example in the book */ * Table 9.22 use phototherapy, clear gen phototherapy2 = phototherapy egen hosp_year = group(hospital year) biprobit /// (over_thresh male i.gest_age##c.birth_wt i.qual_TSB i.age_days i.phototherapy) /// (phototherapy2 = i.hosp_year male i.gest_age##c.birth_wt i.qual_TSB i.age_days), nolog margins, at(phototherapy = (0 1)) predict(pmarg1) scalar EY0 = el(r(b), 1, 1) scalar EY1 = el(r(b), 1, 2) scalar marginal_OR = EY1/(1-EY1)*(1-EY0)/EY0 scalar list marginal_OR * marginal risk difference margins, dydx(phototherapy) predict(pmarg1) margins r.phototherapy, predict(pmarg1) * biprobit model omitting control variables qui biprobit (over_thresh i.phototherapy) (phototherapy2 = i.hosp_year), cluster(hospital) nolog margins, at(phototherapy = (0 1)) predict(pmarg1) scalar EY0 = el(r(b), 1, 1) scalar EY1 = el(r(b), 1, 2) scalar marginal_OR = EY1/(1-EY1)*(1-EY0)/EY0 scalar list marginal_OR * marginal risk-difference margins, dydx(phototherapy) predict(pmarg1) margins r.phototherapy, predict(pmarg1) scalar marginal_RD = EY1-EY0 scalar list marginal_RD * show that adding the IV to a model for phototherapy increases pseudo-R^2 qui logistic phototherapy male i.gest_age##c.birth_wt i.qual_TSB i.age_days scalar psuedoR2_base = e(r2_p) qui logistic phototherapy male i.gest_age##c.birth_wt i.qual_TSB i.age_days i.hosp_year scalar psuedoR2_IV = e(r2_p) scalar list psuedoR2_base psuedoR2_IV