library(matrixStats) # for numerically stable logsumexps options(amelia.parallel="no", amelia.ncpus=1) library(Amelia) source("measerr_methods.R") ## for my more generic function. run_simulation <- function(df, result, outcome_formula = y ~ x + z, proxy_formula = w_pred ~ x, truth_formula = x ~ z){ accuracy <- df[,mean(w_pred==x)] result <- append(result, list(accuracy=accuracy)) (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])) loa0.feasible <- lm(y ~ x.obs.0 + z, data = df[!(is.na(x.obs.1))]) loa0.ci.Bxy <- confint(loa0.feasible)['x.obs.0',] loa0.ci.Bzy <- confint(loa0.feasible)['z',] result <- append(result, list(Bxy.est.loa0.feasible=coef(loa0.feasible)['x.obs.0'], Bzy.est.loa0.feasible=coef(loa0.feasible)['z'], Bxy.ci.upper.loa0.feasible = loa0.ci.Bxy[2], Bxy.ci.lower.loa0.feasible = loa0.ci.Bxy[1], Bzy.ci.upper.loa0.feasible = loa0.ci.Bzy[2], Bzy.ci.lower.loa0.feasible = loa0.ci.Bzy[1])) df.loa0.mle <- copy(df) df.loa0.mle[,x:=x.obs.0] loa0.mle <- measerr_mle(df.loa0.mle, outcome_formula=outcome_formula, proxy_formula=proxy_formula, truth_formula=truth_formula) fisher.info <- solve(loa0.mle$hessian) coef <- loa0.mle$par ci.upper <- coef + sqrt(diag(fisher.info)) * 1.96 ci.lower <- coef - sqrt(diag(fisher.info)) * 1.96 result <- append(result, list(Bxy.est.loa0.mle=coef['x'], Bzy.est.loa0.mle=coef['z'], Bxy.ci.upper.loa0.mle = ci.upper['x'], Bxy.ci.lower.loa0.mle = ci.lower['x'], Bzy.ci.upper.loa0.mle = ci.upper['z'], Bzy.ci.lower.loa0.mle = ci.upper['z'])) loco.feasible <- lm(y ~ x.obs.1 + z, data = df[(!is.na(x.obs.1)) & (x.obs.1 == x.obs.0)]) loco.feasible.ci.Bxy <- confint(loco.feasible)['x.obs.1',] loco.feasible.ci.Bzy <- confint(loco.feasible)['z',] result <- append(result, list(Bxy.est.loco.feasible=coef(loco.feasible)['x.obs.1'], Bzy.est.loco.feasible=coef(loco.feasible)['z'], Bxy.ci.upper.loco.feasible = loco.feasible.ci.Bxy[2], Bxy.ci.lower.loco.feasible = loco.feasible.ci.Bxy[1], Bzy.ci.upper.loco.feasible = loco.feasible.ci.Bzy[2], Bzy.ci.lower.loco.feasible = loco.feasible.ci.Bzy[1])) df.loco.mle <- copy(df) df.loco.mle[,x.obs:=NA] df.loco.mle[(x.obs.0)==(x.obs.1),x.obs:=x.obs.0] df.loco.mle[,x.true:=x] df.loco.mle[,x:=x.obs] print(df.loco.mle[!is.na(x.obs.1),mean(x.true==x,na.rm=TRUE)]) loco.mle <- measerr_mle(df.loco.mle, outcome_formula=outcome_formula, proxy_formula=proxy_formula, truth_formula=truth_formula) fisher.info <- solve(loco.mle$hessian) coef <- loco.mle$par ci.upper <- coef + sqrt(diag(fisher.info)) * 1.96 ci.lower <- coef - sqrt(diag(fisher.info)) * 1.96 result <- append(result, list(Bxy.est.loco.mle=coef['x'], Bzy.est.loco.mle=coef['z'], Bxy.ci.upper.loco.mle = ci.upper['x'], Bxy.ci.lower.loco.mle = ci.lower['x'], Bzy.ci.upper.loco.mle = ci.upper['z'], Bzy.ci.lower.loco.mle = ci.upper['z'])) ## print(rater_formula) ## print(proxy_formula) ## mle.irr <- measerr_irr_mle( df, outcome_formula = outcome_formula, rater_formula = rater_formula, proxy_formula=proxy_formula, truth_formula=truth_formula) ## fisher.info <- solve(mle.irr$hessian) ## coef <- mle.irr$par ## ci.upper <- coef + sqrt(diag(fisher.info)) * 1.96 ## ci.lower <- coef - sqrt(diag(fisher.info)) * 1.96 ## result <- append(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'])) return(result) }