]> code.communitydata.science - ml_measurement_error_public.git/blob - simulations/irr_dv_simulation_base.R
Added, but didn't test the remaining robustness checks.
[ml_measurement_error_public.git] / simulations / irr_dv_simulation_base.R
1 library(matrixStats) # for numerically stable logsumexps
2
3 options(amelia.parallel="no",
4         amelia.ncpus=1)
5 library(Amelia)
6
7 source("pl_methods.R")
8 source("measerr_methods_2.R") ## for my more generic function.
9
10 run_simulation_depvar <- function(df, result, outcome_formula = y ~ x + z, coder_formulas = list(y.obs.0 ~ 1, y.obs.1 ~ 1), proxy_formula = w_pred ~ y.obs.1+y.obs.0+y){
11     (accuracy <- df[,mean(w_pred==y)])
12     result <- append(result, list(accuracy=accuracy))
13     (error.cor.z <- cor(df$x, df$w_pred - df$z))
14     (error.cor.x <- cor(df$x, df$w_pred - df$y))
15     (error.cor.y <- cor(df$y, df$y - df$w_pred))
16     result <- append(result, list(error.cor.x = error.cor.x,
17                                   error.cor.z = error.cor.z,
18                                   error.cor.y = error.cor.y))
19
20     model.null <- glm(y~1, data=df,family=binomial(link='logit'))
21     (model.true <- glm(y ~ x + z, data=df,family=binomial(link='logit')))
22     (lik.ratio <- exp(logLik(model.true) - logLik(model.null)))
23
24     true.ci.Bxy <- confint(model.true)['x',]
25     true.ci.Bzy <- confint(model.true)['z',]
26
27
28     result <- append(result, list(lik.ratio=lik.ratio))
29
30     result <- append(result, list(Bxy.est.true=coef(model.true)['x'],
31                                   Bzy.est.true=coef(model.true)['z'],
32                                   Bxy.ci.upper.true = true.ci.Bxy[2],
33                                   Bxy.ci.lower.true = true.ci.Bxy[1],
34                                   Bzy.ci.upper.true = true.ci.Bzy[2],
35                                   Bzy.ci.lower.true = true.ci.Bzy[1]))
36                                   
37     (model.naive <- lm(y~w_pred+z, data=df))
38     
39     naive.ci.Bxy <- confint(model.naive)['w_pred',]
40     naive.ci.Bzy <- confint(model.naive)['z',]
41
42     result <- append(result, list(Bxy.est.naive=coef(model.naive)['w_pred'],
43                                   Bzy.est.naive=coef(model.naive)['z'],
44                                   Bxy.ci.upper.naive = naive.ci.Bxy[2],
45                                   Bxy.ci.lower.naive = naive.ci.Bxy[1],
46                                   Bzy.ci.upper.naive = naive.ci.Bzy[2],
47                                   Bzy.ci.lower.naive = naive.ci.Bzy[1]))
48
49
50
51     loa0.feasible <- glm(y.obs.0 ~ x + z, data = df[!(is.na(y.obs.0))], family=binomial(link='logit'))
52
53     loa0.ci.Bxy <- confint(loa0.feasible)['x',]
54     loa0.ci.Bzy <- confint(loa0.feasible)['z',]
55
56     result <- append(result, list(Bxy.est.loa0.feasible=coef(loa0.feasible)['x'],
57                                   Bzy.est.loa0.feasible=coef(loa0.feasible)['z'],
58                                   Bxy.ci.upper.loa0.feasible = loa0.ci.Bxy[2],
59                                   Bxy.ci.lower.loa0.feasible = loa0.ci.Bxy[1],
60                                   Bzy.ci.upper.loa0.feasible = loa0.ci.Bzy[2],
61                                   Bzy.ci.lower.loa0.feasible = loa0.ci.Bzy[1]))
62
63
64     ## df.loa0.mle <- copy(df)
65     ## df.loa0.mle[,y:=y.obs.0]
66     ## loa0.mle <- measerr_mle_dv(df.loa0.mle, outcome_formula=outcome_formula, proxy_formula=proxy_formula)
67     ## fisher.info <- solve(loa0.mle$hessian)
68     ## coef <- loa0.mle$par
69     ## ci.upper <- coef + sqrt(diag(fisher.info)) * 1.96
70     ## ci.lower <- coef - sqrt(diag(fisher.info)) * 1.96
71
72     ## result <- append(result, list(Bxy.est.loa0.mle=coef['x'],
73     ##                               Bzy.est.loa0.mle=coef['z'],
74     ##                               Bxy.ci.upper.loa0.mle = ci.upper['x'],
75     ##                               Bxy.ci.lower.loa0.mle = ci.lower['x'],
76     ##                               Bzy.ci.upper.loa0.mle = ci.upper['z'],
77     ##                               Bzy.ci.lower.loa0.mle = ci.upper['z']))
78
79     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'))
80
81     loco.feasible.ci.Bxy <- confint(loco.feasible)['x',]
82     loco.feasible.ci.Bzy <- confint(loco.feasible)['z',]
83
84     result <- append(result, list(Bxy.est.loco.feasible=coef(loco.feasible)['x'],
85                                   Bzy.est.loco.feasible=coef(loco.feasible)['z'],
86                                   Bxy.ci.upper.loco.feasible = loco.feasible.ci.Bxy[2],
87                                   Bxy.ci.lower.loco.feasible = loco.feasible.ci.Bxy[1],
88                                   Bzy.ci.upper.loco.feasible = loco.feasible.ci.Bzy[2],
89                                   Bzy.ci.lower.loco.feasible = loco.feasible.ci.Bzy[1]))
90
91     
92     ## df.double.proxy <- copy(df)
93     ## df.double.proxy <- df.double.proxy[,y.obs:=NA]
94     ## df.double.proxy <- df.double.proxy[,y:=NA]
95     
96     ## double.proxy.mle <- measerr_irr_mle_dv(df.double.proxy, outcome_formula=y~x+z, outcome_family=binomial(link='logit'), coder_formulas=list(y.obs.0 ~ y),  proxy_formula=w_pred ~ y.obs.0 + y, proxy_family=binomial(link='logit'))
97     ## print(double.proxy.mle$hessian)
98     ## fisher.info <- solve(double.proxy.mle$hessian)
99     ## coef <- double.proxy.mle$par
100     ## ci.upper <- coef + sqrt(diag(fisher.info)) * 1.96
101     ## ci.lower <- coef - sqrt(diag(fisher.info)) * 1.96
102
103     ## result <- append(result, list(Bxy.est.double.proxy=coef['x'],
104     ##                               Bzy.est.double.proxy=coef['z'],
105     ##                               Bxy.ci.upper.double.proxy = ci.upper['x'],
106     ##                               Bxy.ci.lower.double.proxy = ci.lower['x'],
107     ##                               Bzy.ci.upper.double.proxy = ci.upper['z'],
108     ##                               Bzy.ci.lower.double.proxy = ci.lower['z']))
109
110
111     df.triple.proxy <- copy(df)
112     df.triple.proxy <- df.triple.proxy[,y.obs:=NA]
113     df.triple.proxy <- df.triple.proxy[,y:=NA]
114     
115     triple.proxy.mle <- measerr_irr_mle_dv(df.triple.proxy, outcome_formula=outcome_formula, outcome_family=binomial(link='logit'), coder_formulas=coder_formulas, proxy_formula=proxy_formula, proxy_family=binomial(link='logit'))
116     print(triple.proxy.mle$hessian)
117     fisher.info <- solve(triple.proxy.mle$hessian)
118     print(fisher.info)
119     coef <- triple.proxy.mle$par
120     ci.upper <- coef + sqrt(diag(fisher.info)) * 1.96
121     ci.lower <- coef - sqrt(diag(fisher.info)) * 1.96
122
123     result <- append(result, list(Bxy.est.triple.proxy=coef['x'],
124                                   Bzy.est.triple.proxy=coef['z'],
125                                   Bxy.ci.upper.triple.proxy = ci.upper['x'],
126                                   Bxy.ci.lower.triple.proxy = ci.lower['x'],
127                                   Bzy.ci.upper.triple.proxy = ci.upper['z'],
128                                   Bzy.ci.lower.triple.proxy = ci.lower['z']))
129
130     ## df.loco.mle <- copy(df)
131     ## df.loco.mle[,y.obs:=NA]
132     ## df.loco.mle[(y.obs.0)==(y.obs.1),y.obs:=y.obs.0]
133     ## df.loco.mle[,y.true:=y]
134     ## df.loco.mle[,y:=y.obs]
135     ## print(df.loco.mle[!is.na(y.obs.1),mean(y.true==y,na.rm=TRUE)])
136     ## loco.mle <- measerr_mle_dv(df.loco.mle, outcome_formula=outcome_formula, proxy_formula=proxy_formula)
137     ## fisher.info <- solve(loco.mle$hessian)
138     ## coef <- loco.mle$par
139     ## ci.upper <- coef + sqrt(diag(fisher.info)) * 1.96
140     ## ci.lower <- coef - sqrt(diag(fisher.info)) * 1.96
141
142     ## result <- append(result, list(Bxy.est.loco.mle=coef['x'],
143     ##                               Bzy.est.loco.mle=coef['z'],
144     ##                               Bxy.ci.upper.loco.mle = ci.upper['x'],
145     ##                               Bxy.ci.lower.loco.mle = ci.lower['x'],
146     ##                               Bzy.ci.upper.loco.mle = ci.upper['z'],
147     ##                               Bzy.ci.lower.loco.mle = ci.lower['z']))
148
149
150
151     ## my implementatoin of liklihood based correction
152     mod.zhang <- zhang.mle.dv(df.loco.mle)
153     coef <- coef(mod.zhang)
154     ci <- confint(mod.zhang,method='quad')
155
156     result <- append(result,
157                      list(Bxy.est.zhang = coef['Bxy'],
158                           Bxy.ci.upper.zhang = ci['Bxy','97.5 %'],
159                           Bxy.ci.lower.zhang = ci['Bxy','2.5 %'],
160                           Bzy.est.zhang = coef['Bzy'],
161                           Bzy.ci.upper.zhang = ci['Bzy','97.5 %'],
162                           Bzy.ci.lower.zhang = ci['Bzy','2.5 %']))
163
164     
165
166     print(df.loco.mle)
167
168     # amelia says use normal distribution for binary variables.
169     tryCatch({
170         amelia.out.k <- amelia(df.loco.mle, m=200, p2s=0, idvars=c('y','ystar','w','y.obs.1','y.obs.0','y.true'))
171         mod.amelia.k <- zelig(y.obs~x+z, model='ls', data=amelia.out.k$imputations, cite=FALSE)
172         (coefse <- combine_coef_se(mod.amelia.k, messages=FALSE))
173         est.x.mi <- coefse['x','Estimate']
174         est.x.se <- coefse['x','Std.Error']
175         result <- append(result,
176                          list(Bxy.est.amelia.full = est.x.mi,
177                               Bxy.ci.upper.amelia.full = est.x.mi + 1.96 * est.x.se,
178                               Bxy.ci.lower.amelia.full = est.x.mi - 1.96 * est.x.se
179                               ))
180
181         est.z.mi <- coefse['z','Estimate']
182         est.z.se <- coefse['z','Std.Error']
183
184         result <- append(result,
185                          list(Bzy.est.amelia.full = est.z.mi,
186                               Bzy.ci.upper.amelia.full = est.z.mi + 1.96 * est.z.se,
187                               Bzy.ci.lower.amelia.full = est.z.mi - 1.96 * est.z.se
188                               ))
189
190     },
191     error = function(e){
192         message("An error occurred:\n",e)
193         result$error <- paste0(result$error,'\n', e)
194     })
195     ## mle.irr <- measerr_irr_mle( df, outcome_formula = outcome_formula, rater_formula = rater_formula, proxy_formula=proxy_formula, truth_formula=truth_formula)
196
197     ## fisher.info <- solve(mle.irr$hessian)
198     ## coef <- mle.irr$par
199     ## ci.upper <- coef + sqrt(diag(fisher.info)) * 1.96
200     ## ci.lower <- coef - sqrt(diag(fisher.info)) * 1.96
201     
202     ## result <- append(result,
203     ##                  list(Bxy.est.mle = coef['x'],
204     ##                       Bxy.ci.upper.mle = ci.upper['x'],
205     ##                       Bxy.ci.lower.mle = ci.lower['x'],
206     ##                       Bzy.est.mle = coef['z'],
207     ##                       Bzy.ci.upper.mle = ci.upper['z'],
208     ##                       Bzy.ci.lower.mle = ci.lower['z']))
209
210     return(result)
211
212 }

Community Data Science Collective || Want to submit a patch?