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_depvar <- function(df, result, outcome_formula = y ~ x + z, rater_formula = y.obs ~ x, proxy_formula = w_pred ~ y){ accuracy <- df[,mean(w_pred==y)] result <- append(result, list(accuracy=accuracy)) (model.true <- glm(y ~ x + z, data=df, family=binomial(link='logit'))) 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 <- glm(y.obs.0 ~ x + z, data = df[!(is.na(y.obs.0))], family=binomial(link='logit')) loa0.ci.Bxy <- confint(loa0.feasible)['x',] loa0.ci.Bzy <- confint(loa0.feasible)['z',] result <- append(result, list(Bxy.est.loa0.feasible=coef(loa0.feasible)['x'], 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[,y:=y.obs.0] loa0.mle <- measerr_mle_dv(df.loa0.mle, outcome_formula=outcome_formula, proxy_formula=proxy_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 <- glm(y.obs.0 ~ x + z, data = df[(!is.na(y.obs.0)) & (y.obs.1 == y.obs.0)], family=binomial(link='logit')) loco.feasible.ci.Bxy <- confint(loco.feasible)['x',] loco.feasible.ci.Bzy <- confint(loco.feasible)['z',] result <- append(result, list(Bxy.est.loco.feasible=coef(loco.feasible)['x'], 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[,y.obs:=NA] df.loco.mle[(y.obs.0)==(y.obs.1),y.obs:=y.obs.0] df.loco.mle[,y.true:=y] df.loco.mle[,y:=y.obs] print(df.loco.mle[!is.na(y.obs.1),mean(y.true==y,na.rm=TRUE)]) loco.mle <- measerr_mle_dv(df.loco.mle, outcome_formula=outcome_formula, proxy_formula=proxy_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) }