+run_simulation_depvar <- function(df, result){
+
+ accuracy <- df[,mean(w_pred==y)]
+ result <- append(result, list(accuracy=accuracy))
+
+ (model.true <- glm(y ~ x + g, data=df,family=binomial(link='logit')))
+ true.ci.Bxy <- confint(model.true)['x',]
+ true.ci.Bgy <- confint(model.true)['g',]
+
+ result <- append(result, list(Bxy.est.true=coef(model.true)['x'],
+ Bgy.est.true=coef(model.true)['g'],
+ Bxy.ci.upper.true = true.ci.Bxy[2],
+ Bxy.ci.lower.true = true.ci.Bxy[1],
+ Bgy.ci.upper.true = true.ci.Bgy[2],
+ Bgy.ci.lower.true = true.ci.Bgy[1]))
+
+ (model.feasible <- glm(y.obs~x+g,data=df,family=binomial(link='logit')))
+
+ feasible.ci.Bxy <- confint(model.feasible)['x',]
+ result <- append(result, list(Bxy.est.feasible=coef(model.feasible)['x'],
+ Bxy.ci.upper.feasible = feasible.ci.Bxy[2],
+ Bxy.ci.lower.feasible = feasible.ci.Bxy[1]))
+
+ feasible.ci.Bgy <- confint(model.feasible)['g',]
+ result <- append(result, list(Bgy.est.feasible=coef(model.feasible)['g'],
+ Bgy.ci.upper.feasible = feasible.ci.Bgy[2],
+ Bgy.ci.lower.feasible = feasible.ci.Bgy[1]))
+
+ (model.naive <- glm(w_pred~x+g, data=df, family=binomial(link='logit')))
+
+ naive.ci.Bxy <- confint(model.naive)['x',]
+ naive.ci.Bgy <- confint(model.naive)['g',]
+
+ result <- append(result, list(Bxy.est.naive=coef(model.naive)['x'],
+ Bgy.est.naive=coef(model.naive)['g'],
+ Bxy.ci.upper.naive = naive.ci.Bxy[2],
+ Bxy.ci.lower.naive = naive.ci.Bxy[1],
+ Bgy.ci.upper.naive = naive.ci.Bgy[2],
+ Bgy.ci.lower.naive = naive.ci.Bgy[1]))
+
+
+ (model.naive.cont <- lm(w~x+g, data=df))
+ naivecont.ci.Bxy <- confint(model.naive.cont)['x',]
+ naivecont.ci.Bgy <- confint(model.naive.cont)['g',]
+
+ ## my implementatoin of liklihood based correction
+ mod.caroll.lik <- logistic.correction.liklihood(df)
+ coef <- coef(mod.caroll.lik)
+ ci <- confint(mod.caroll.lik)
+
+ result <- append(result,
+ list(Bxy.est.mle = coef['Bxy'],
+ Bxy.ci.upper.mle = ci['Bxy','97.5 %'],
+ Bxy.ci.lower.mle = ci['Bxy','2.5 %'],
+ Bgy.est.mle = coef['Bgy'],
+ Bgy.ci.upper.mle = ci['Bgy','97.5 %'],
+ Bgy.ci.lower.mle = ci['Bgy','2.5 %']))
+
+
+ ## my implementatoin of liklihood based correction
+ mod.caroll.pseudo <- logistic.correction.pseudo(df)
+ coef <- coef(mod.caroll.pseudo)
+ ci <- confint(mod.caroll.pseudo)
+
+ result <- append(result,
+ list(Bxy.est.pseudo = coef['Bxy'],
+ Bxy.ci.upper.pseudo = ci['Bxy','97.5 %'],
+ Bxy.ci.lower.pseudo = ci['Bxy','2.5 %'],
+ Bgy.est.pseudo = coef['Bgy'],
+ Bgy.ci.upper.pseudo = ci['Bgy','97.5 %'],
+ Bgy.ci.lower.pseudo = ci['Bgy','2.5 %']))
+
+
+ # amelia says use normal distribution for binary variables.
+ amelia.out.k <- amelia(df, m=200, p2s=0, idvars=c('y','ystar','w_pred'))
+ mod.amelia.k <- zelig(y.obs~x+g, model='ls', data=amelia.out.k$imputations, cite=FALSE)
+ (coefse <- combine_coef_se(mod.amelia.k, messages=FALSE))
+
+ est.x.mi <- coefse['x','Estimate']
+ est.x.se <- coefse['x','Std.Error']
+ result <- append(result,
+ list(Bxy.est.amelia.full = est.x.mi,
+ Bxy.ci.upper.amelia.full = est.x.mi + 1.96 * est.x.se,
+ Bxy.ci.lower.amelia.full = est.x.mi - 1.96 * est.x.se
+ ))
+
+ est.g.mi <- coefse['g','Estimate']
+ est.g.se <- coefse['g','Std.Error']
+
+ result <- append(result,
+ list(Bgy.est.amelia.full = est.g.mi,
+ Bgy.ci.upper.amelia.full = est.g.mi + 1.96 * est.g.se,
+ Bgy.ci.lower.amelia.full = est.g.mi - 1.96 * est.g.se
+ ))
+
+ return(result)
+
+}
+