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 <- function(df, result, outcome_formula = y ~ x + z, proxy_formula = w_pred ~ x, truth_formula = x ~ z){
11 accuracy <- df[,mean(w_pred==x)]
12 result <- append(result, list(accuracy=accuracy))
14 (model.true <- lm(y ~ x + z, data=df))
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 <- lm(y ~ x.obs.0 + z, data = df[!(is.na(x.obs.1))])
29 loa0.ci.Bxy <- confint(loa0.feasible)['x.obs.0',]
30 loa0.ci.Bzy <- confint(loa0.feasible)['z',]
32 result <- append(result, list(Bxy.est.loa0.feasible=coef(loa0.feasible)['x.obs.0'],
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[,x:=x.obs.0]
42 loa0.mle <- measerr_mle(df.loa0.mle, outcome_formula=outcome_formula, proxy_formula=proxy_formula, truth_formula=truth_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 <- lm(y ~ x.obs.1 + z, data = df[(!is.na(x.obs.1)) & (x.obs.1 == x.obs.0)])
57 loco.feasible.ci.Bxy <- confint(loco.feasible)['x.obs.1',]
58 loco.feasible.ci.Bzy <- confint(loco.feasible)['z',]
60 result <- append(result, list(Bxy.est.loco.feasible=coef(loco.feasible)['x.obs.1'],
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[,x.obs:=NA]
70 df.loco.mle[(x.obs.0)==(x.obs.1),x.obs:=x.obs.0]
71 df.loco.mle[,x.true:=x]
72 df.loco.mle[,x:=x.obs]
73 print(df.loco.mle[!is.na(x.obs.1),mean(x.true==x,na.rm=TRUE)])
74 loco.mle <- measerr_mle(df.loco.mle, outcome_formula=outcome_formula, proxy_formula=proxy_formula, truth_formula=truth_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']))
87 ## print(rater_formula)
88 ## print(proxy_formula)
89 ## mle.irr <- measerr_irr_mle( df, outcome_formula = outcome_formula, rater_formula = rater_formula, proxy_formula=proxy_formula, truth_formula=truth_formula)
91 ## fisher.info <- solve(mle.irr$hessian)
92 ## coef <- mle.irr$par
93 ## ci.upper <- coef + sqrt(diag(fisher.info)) * 1.96
94 ## ci.lower <- coef - sqrt(diag(fisher.info)) * 1.96
96 ## result <- append(result,
97 ## list(Bxy.est.mle = coef['x'],
98 ## Bxy.ci.upper.mle = ci.upper['x'],
99 ## Bxy.ci.lower.mle = ci.lower['x'],
100 ## Bzy.est.mle = coef['z'],
101 ## Bzy.ci.upper.mle = ci.upper['z'],
102 ## Bzy.ci.lower.mle = ci.lower['z']))