1 library(matrixStats) # for numerically stable logsumexps
3 options(amelia.parallel="no",
7 source("measerr_methods.R") ## for my more generic function.
9 run_simulation_depvar <- function(df, result, outcome_formula = y ~ x + z, rater_formula = y.obs ~ x, proxy_formula = w_pred ~ y){
11 accuracy <- df[,mean(w_pred==y)]
12 result <- append(result, list(accuracy=accuracy))
14 (model.true <- glm(y ~ x + z, data=df, family=binomial(link='logit')))
15 true.ci.Bxy <- confint(model.true)['x',]
16 true.ci.Bzy <- confint(model.true)['z',]
18 result <- append(result, list(Bxy.est.true=coef(model.true)['x'],
19 Bzy.est.true=coef(model.true)['z'],
20 Bxy.ci.upper.true = true.ci.Bxy[2],
21 Bxy.ci.lower.true = true.ci.Bxy[1],
22 Bzy.ci.upper.true = true.ci.Bzy[2],
23 Bzy.ci.lower.true = true.ci.Bzy[1]))
27 loa0.feasible <- glm(y.obs.0 ~ x + z, data = df[!(is.na(y.obs.0))], family=binomial(link='logit'))
29 loa0.ci.Bxy <- confint(loa0.feasible)['x',]
30 loa0.ci.Bzy <- confint(loa0.feasible)['z',]
32 result <- append(result, list(Bxy.est.loa0.feasible=coef(loa0.feasible)['x'],
33 Bzy.est.loa0.feasible=coef(loa0.feasible)['z'],
34 Bxy.ci.upper.loa0.feasible = loa0.ci.Bxy[2],
35 Bxy.ci.lower.loa0.feasible = loa0.ci.Bxy[1],
36 Bzy.ci.upper.loa0.feasible = loa0.ci.Bzy[2],
37 Bzy.ci.lower.loa0.feasible = loa0.ci.Bzy[1]))
40 df.loa0.mle <- copy(df)
41 df.loa0.mle[,y:=y.obs.0]
42 loa0.mle <- measerr_mle_dv(df.loa0.mle, outcome_formula=outcome_formula, proxy_formula=proxy_formula)
43 fisher.info <- solve(loa0.mle$hessian)
45 ci.upper <- coef + sqrt(diag(fisher.info)) * 1.96
46 ci.lower <- coef - sqrt(diag(fisher.info)) * 1.96
48 result <- append(result, list(Bxy.est.loa0.mle=coef['x'],
49 Bzy.est.loa0.mle=coef['z'],
50 Bxy.ci.upper.loa0.mle = ci.upper['x'],
51 Bxy.ci.lower.loa0.mle = ci.lower['x'],
52 Bzy.ci.upper.loa0.mle = ci.upper['z'],
53 Bzy.ci.lower.loa0.mle = ci.upper['z']))
55 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'))
57 loco.feasible.ci.Bxy <- confint(loco.feasible)['x',]
58 loco.feasible.ci.Bzy <- confint(loco.feasible)['z',]
60 result <- append(result, list(Bxy.est.loco.feasible=coef(loco.feasible)['x'],
61 Bzy.est.loco.feasible=coef(loco.feasible)['z'],
62 Bxy.ci.upper.loco.feasible = loco.feasible.ci.Bxy[2],
63 Bxy.ci.lower.loco.feasible = loco.feasible.ci.Bxy[1],
64 Bzy.ci.upper.loco.feasible = loco.feasible.ci.Bzy[2],
65 Bzy.ci.lower.loco.feasible = loco.feasible.ci.Bzy[1]))
68 df.loco.mle <- copy(df)
69 df.loco.mle[,y.obs:=NA]
70 df.loco.mle[(y.obs.0)==(y.obs.1),y.obs:=y.obs.0]
71 df.loco.mle[,y.true:=y]
72 df.loco.mle[,y:=y.obs]
73 print(df.loco.mle[!is.na(y.obs.1),mean(y.true==y,na.rm=TRUE)])
74 loco.mle <- measerr_mle_dv(df.loco.mle, outcome_formula=outcome_formula, proxy_formula=proxy_formula)
75 fisher.info <- solve(loco.mle$hessian)
77 ci.upper <- coef + sqrt(diag(fisher.info)) * 1.96
78 ci.lower <- coef - sqrt(diag(fisher.info)) * 1.96
80 result <- append(result, list(Bxy.est.loco.mle=coef['x'],
81 Bzy.est.loco.mle=coef['z'],
82 Bxy.ci.upper.loco.mle = ci.upper['x'],
83 Bxy.ci.lower.loco.mle = ci.lower['x'],
84 Bzy.ci.upper.loco.mle = ci.upper['z'],
85 Bzy.ci.lower.loco.mle = ci.upper['z']))
90 ## mle.irr <- measerr_irr_mle( df, outcome_formula = outcome_formula, rater_formula = rater_formula, proxy_formula=proxy_formula, truth_formula=truth_formula)
92 ## fisher.info <- solve(mle.irr$hessian)
93 ## coef <- mle.irr$par
94 ## ci.upper <- coef + sqrt(diag(fisher.info)) * 1.96
95 ## ci.lower <- coef - sqrt(diag(fisher.info)) * 1.96
97 ## result <- append(result,
98 ## list(Bxy.est.mle = coef['x'],
99 ## Bxy.ci.upper.mle = ci.upper['x'],
100 ## Bxy.ci.lower.mle = ci.lower['x'],
101 ## Bzy.est.mle = coef['z'],
102 ## Bzy.ci.upper.mle = ci.upper['z'],
103 ## Bzy.ci.lower.mle = ci.lower['z']))