+}
+
+
+## outcome_formula, proxy_formula, and truth_formula are passed to measerr_mle
+run_simulation <- function(df, result, outcome_formula=y~x+z, proxy_formula=NULL, truth_formula=NULL, confint_method='quad'){
+
+ accuracy <- df[,mean(w_pred==x)]
+ accuracy.y0 <- df[y<=0,mean(w_pred==x)]
+ accuracy.y1 <- df[y>=0,mean(w_pred==x)]
+ cor.y.xi <- cor(df$x - df$w_pred, df$y)
+
+ fnr <- df[w_pred==0,mean(w_pred!=x)]
+ fnr.y0 <- df[(w_pred==0) & (y<=0),mean(w_pred!=x)]
+ fnr.y1 <- df[(w_pred==0) & (y>=0),mean(w_pred!=x)]
+
+ fpr <- df[w_pred==1,mean(w_pred!=x)]
+ fpr.y0 <- df[(w_pred==1) & (y<=0),mean(w_pred!=x)]
+ fpr.y1 <- df[(w_pred==1) & (y>=0),mean(w_pred!=x)]
+ cor.resid.w_pred <- cor(resid(lm(y~x+z,df)),df$w_pred)
+
+ result <- append(result, list(accuracy=accuracy,
+ accuracy.y0=accuracy.y0,
+ accuracy.y1=accuracy.y1,
+ cor.y.xi=cor.y.xi,
+ fnr=fnr,
+ fnr.y0=fnr.y0,
+ fnr.y1=fnr.y1,
+ fpr=fpr,
+ fpr.y0=fpr.y0,
+ fpr.y1=fpr.y1,
+ cor.resid.w_pred=cor.resid.w_pred
+ ))
+
+ result <- append(result, list(cor.xz=cor(df$x,df$z)))
+ (model.true <- lm(y ~ x + z, data=df))
+ true.ci.Bxy <- confint(model.true)['x',]
+ true.ci.Bzy <- confint(model.true)['z',]
+
+ result <- append(result, list(Bxy.est.true=coef(model.true)['x'],
+ Bzy.est.true=coef(model.true)['z'],
+ Bxy.ci.upper.true = true.ci.Bxy[2],
+ Bxy.ci.lower.true = true.ci.Bxy[1],
+ Bzy.ci.upper.true = true.ci.Bzy[2],
+ Bzy.ci.lower.true = true.ci.Bzy[1]))
+
+ (model.feasible <- lm(y~x.obs+z,data=df))
+
+ feasible.ci.Bxy <- confint(model.feasible)['x.obs',]
+ result <- append(result, list(Bxy.est.feasible=coef(model.feasible)['x.obs'],
+ Bxy.ci.upper.feasible = feasible.ci.Bxy[2],
+ Bxy.ci.lower.feasible = feasible.ci.Bxy[1]))
+
+ feasible.ci.Bzy <- confint(model.feasible)['z',]
+ result <- append(result, list(Bzy.est.feasible=coef(model.feasible)['z'],
+ Bzy.ci.upper.feasible = feasible.ci.Bzy[2],
+ Bzy.ci.lower.feasible = feasible.ci.Bzy[1]))
+
+ (model.naive <- lm(y~w_pred+z, data=df))
+
+ naive.ci.Bxy <- confint(model.naive)['w_pred',]
+ naive.ci.Bzy <- confint(model.naive)['z',]
+
+ result <- append(result, list(Bxy.est.naive=coef(model.naive)['w_pred'],
+ Bzy.est.naive=coef(model.naive)['z'],
+ Bxy.ci.upper.naive = naive.ci.Bxy[2],
+ Bxy.ci.lower.naive = naive.ci.Bxy[1],
+ Bzy.ci.upper.naive = naive.ci.Bzy[2],
+ Bzy.ci.lower.naive = naive.ci.Bzy[1]))
+
+ amelia_result <- list(
+ Bxy.est.amelia.full = NULL,
+ Bxy.ci.upper.amelia.full = NULL,
+ Bxy.ci.lower.amelia.full = NULL,
+ Bzy.est.amelia.full = NULL,
+ Bzy.ci.upper.amelia.full = NULL,
+ Bzy.ci.lower.amelia.full = NULL
+ )
+
+ tryCatch({
+ amelia.out.k <- amelia(df, m=200, p2s=0, idvars=c('x','w'))
+ mod.amelia.k <- zelig(y~x.obs+z, model='ls', data=amelia.out.k$imputations, cite=FALSE)
+ (coefse <- combine_coef_se(mod.amelia.k))
+
+ est.x.mi <- coefse['x.obs','Estimate']
+ est.x.se <- coefse['x.obs','Std.Error']
+ est.z.mi <- coefse['z','Estimate']
+ est.z.se <- coefse['z','Std.Error']
+
+ amelia_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,
+ Bzy.est.amelia.full = est.z.mi,
+ Bzy.ci.upper.amelia.full = est.z.mi + 1.96 * est.z.se,
+ Bzy.ci.lower.amelia.full = est.z.mi - 1.96 * est.z.se
+ )
+
+ },
+
+ error = function(e){
+ result[['error']] <- e}
+ )
+
+
+ result <- append(result, amelia_result)
+
+
+ mle_result <- list(Bxy.est.mle = NULL,
+ Bxy.ci.upper.mle = NULL,
+ Bxy.ci.lower.mle = NULL,
+ Bzy.est.mle = NULL,
+ Bzy.ci.upper.mle = NULL,
+ Bzy.ci.lower.mle = NULL)
+
+ tryCatch({
+ temp.df <- copy(df)
+ temp.df <- temp.df[,x:=x.obs]
+ if(confint_method=='quad'){
+ mod.caroll.lik <- measerr_mle(temp.df, outcome_formula=outcome_formula, proxy_formula=proxy_formula, truth_formula=truth_formula, method='optim')
+ fischer.info <- solve(mod.caroll.lik$hessian)
+ coef <- mod.caroll.lik$par
+ ci.upper <- coef + sqrt(diag(fischer.info)) * 1.96
+ ci.lower <- coef - sqrt(diag(fischer.info)) * 1.96
+ } else { # confint_method == 'bbmle'
+
+ mod.caroll.lik <- measerr_mle(temp.df, outcome_formula=outcome_formula, proxy_formula=proxy_formula, truth_formula=truth_formula, method='bbmle')
+ coef <- coef(mod.caroll.lik)
+ ci <- confint(mod.caroll.lik, method='spline')
+ ci.lower <- ci[,'2.5 %']
+ ci.upper <- ci[,'97.5 %']
+ }
+ mle_result <- list(Bxy.est.mle = coef['x'],
+ Bxy.ci.upper.mle = ci.upper['x'],
+ Bxy.ci.lower.mle = ci.lower['x'],
+ Bzy.est.mle = coef['z'],
+ Bzy.ci.upper.mle = ci.upper['z'],
+ Bzy.ci.lower.mle = ci.lower['z'])
+ },
+
+ error=function(e) {result[['error']] <- as.character(e)
+ })
+
+
+ result <- append(result, mle_result)
+
+ mod.zhang.lik <- zhang.mle.iv(df)
+ coef <- coef(mod.zhang.lik)
+ ci <- confint(mod.zhang.lik,method='quad')