]> code.communitydata.science - ml_measurement_error_public.git/blob - simulations/simulation_base.R
Add simulation code of IRR
[ml_measurement_error_public.git] / simulations / simulation_base.R
1 library(predictionError)
2 library(mecor)
3 options(amelia.parallel="no",
4         amelia.ncpus=1)
5 library(Amelia)
6 library(Zelig)
7 library(bbmle)
8 library(matrixStats) # for numerically stable logsumexps
9
10 source("measerr_methods.R") ## for my more generic function.
11
12 ## This uses the pseudolikelihood approach from Carroll page 349.
13 ## assumes MAR
14 ## assumes differential error, but that only depends on Y
15 ## inefficient, because pseudolikelihood
16     
17 ## This uses the pseudo-likelihood approach from Carroll page 346.
18 my.pseudo.mle <- function(df){
19     p1.est <- mean(df[w_pred==1]$y.obs==1,na.rm=T)
20     p0.est <- mean(df[w_pred==0]$y.obs==0,na.rm=T)
21     
22     nll <- function(B0, Bxy, Bzy){
23
24         pw <- vector(mode='numeric',length=nrow(df))
25         dfw1 <- df[w_pred==1]
26         dfw0 <- df[w_pred==0]
27         pw[df$w_pred==1] <- plogis(B0 + Bxy * dfw1$x + Bzy * dfw1$z, log=T)
28         pw[df$w_pred==0] <- plogis(B0 + Bxy * dfw0$x + Bzy * dfw0$z, lower.tail=FALSE, log=T)
29         
30         probs <- colLogSumExps(rbind(log(1 - p0.est), log(p1.est + p0.est - 1) + pw))
31         return(-1*sum(probs))
32     }
33     
34     mlefit <- mle2(minuslogl = nll, start = list(B0=0.0, Bxy=0.0, Bzy=0.0), control=list(maxit=1e6),method='L-BFGS-B')
35     return(mlefit)
36
37 }
38
39
40 ## model from Zhang's arxiv paper, with predictions for y
41 ## Zhang got this model from Hausman 1998
42 ### I think this is actually eqivalent to the pseudo.mle method
43 zhang.mle.iv <- function(df){
44     nll <- function(B0=0, Bxy=0, Bzy=0, sigma_y=0.1, ppv=0.9, npv=0.9){
45     df.obs <- df[!is.na(x.obs)]
46     df.unobs <- df[is.na(x.obs)]
47
48     ## fpr = 1 - TNR
49     ### Problem: accounting for uncertainty in ppv / npv
50     
51     ll.w1x1.obs <- with(df.obs[(w_pred==1)], dbinom(x.obs,size=1,prob=ppv,log=T))
52     ll.w0x0.obs <- with(df.obs[(w_pred==0)], dbinom(1-x.obs,size=1,prob=npv,log=T))
53
54     ## fnr = 1 - TPR
55     ll.y.obs <- with(df.obs, dnorm(y, B0 + Bxy * x + Bzy * z, sd=sigma_y,log=T))
56     ll <- sum(ll.y.obs)
57     ll <- ll + sum(ll.w1x1.obs) + sum(ll.w0x0.obs)
58
59     # unobserved case; integrate out x
60     ll.x.1 <- with(df.unobs, dnorm(y, B0 + Bxy + Bzy * z, sd = sigma_y, log=T))
61     ll.x.0 <- with(df.unobs, dnorm(y, B0 + Bzy * z, sd = sigma_y,log=T))
62
63     ## case x == 1
64     lls.x.1 <- colLogSumExps(rbind(log(ppv) + ll.x.1, log(1-ppv) + ll.x.0))
65     
66     ## case x == 0
67     lls.x.0 <- colLogSumExps(rbind(log(1-npv) + ll.x.1, log(npv) + ll.x.0))
68
69     lls <- colLogSumExps(rbind(lls.x.1, lls.x.0))
70     ll <- ll + sum(lls)
71     return(-ll)
72     }    
73     mlefit <- mle2(minuslogl = nll, control=list(maxit=1e6), lower=list(sigma_y=0.0001, B0=-Inf, Bxy=-Inf, Bzy=-Inf,ppv=0.00001, npv=0.00001),
74                    upper=list(sigma_y=Inf, B0=Inf, Bxy=Inf, Bzy=Inf, ppv=0.99999,npv=0.99999),method='L-BFGS-B')
75     return(mlefit)
76 }
77
78 ## this is equivalent to the pseudo-liklihood model from Carolla
79 zhang.mle.dv <- function(df){
80
81     nll <- function(B0=0, Bxy=0, Bzy=0, ppv=0.9, npv=0.9){
82     df.obs <- df[!is.na(y.obs)]
83
84     ## fpr = 1 - TNR
85     ll.w0y0 <- with(df.obs[y.obs==0],dbinom(1-w_pred,1,npv,log=TRUE))
86     ll.w1y1 <- with(df.obs[y.obs==1],dbinom(w_pred,1,ppv,log=TRUE))
87
88     # observed case
89     ll.y.obs <- vector(mode='numeric', length=nrow(df.obs))
90     ll.y.obs[df.obs$y.obs==1] <- with(df.obs[y.obs==1], plogis(B0 + Bxy * x + Bzy * z,log=T))
91     ll.y.obs[df.obs$y.obs==0] <- with(df.obs[y.obs==0], plogis(B0 + Bxy * x + Bzy * z,log=T,lower.tail=FALSE))
92
93     ll <- sum(ll.y.obs) + sum(ll.w0y0) + sum(ll.w1y1)
94
95     # unobserved case; integrate out y
96     ## case y = 1
97     ll.y.1 <- vector(mode='numeric', length=nrow(df))
98     pi.y.1 <- with(df,plogis(B0 + Bxy * x + Bzy*z, log=T))
99     ## P(w=1| y=1)P(y=1) + P(w=0|y=1)P(y=1) = P(w=1,y=1) + P(w=0,y=1)
100     lls.y.1 <- colLogSumExps(rbind(log(ppv) + pi.y.1, log(1-ppv) + pi.y.1))
101     
102     ## case y = 0
103     ll.y.0 <- vector(mode='numeric', length=nrow(df))
104     pi.y.0 <- with(df,plogis(B0 + Bxy * x + Bzy*z, log=T,lower.tail=FALSE))
105
106     ## P(w=1 | y=0)P(y=0) + P(w=0|y=0)P(y=0) = P(w=1,y=0) + P(w=0,y=0)
107     lls.y.0 <- colLogSumExps(rbind(log(npv) + pi.y.0, log(1-npv) + pi.y.0))
108
109     lls <- colLogSumExps(rbind(lls.y.1, lls.y.0))
110     ll <- ll + sum(lls)
111     return(-ll)
112     }    
113     mlefit <- mle2(minuslogl = nll, control=list(maxit=1e6),method='L-BFGS-B',lower=list(B0=-Inf, Bxy=-Inf, Bzy=-Inf, ppv=0.001,npv=0.001),
114                    upper=list(B0=Inf, Bxy=Inf, Bzy=Inf,ppv=0.999,npv=0.999))
115     return(mlefit)
116 }
117
118 ## This uses the likelihood approach from Carroll page 353.
119 ## assumes that we have a good measurement error model
120 my.mle <- function(df){
121     
122     ## liklihood for observed responses
123     nll <- function(B0, Bxy, Bzy, gamma0, gamma_y, gamma_z, gamma_yz){
124         df.obs <- df[!is.na(y.obs)]
125         yobs0 <- df.obs$y==0 
126         yobs1 <- df.obs$y==1
127         p.y.obs <- vector(mode='numeric', length=nrow(df.obs))
128         
129         p.y.obs[yobs1] <- plogis(B0 + Bxy * df.obs[yobs1]$x + Bzy*df.obs[yobs1]$z,log=T)
130         p.y.obs[yobs0] <- plogis(B0 + Bxy * df.obs[yobs0]$x + Bzy*df.obs[yobs0]$z,lower.tail=FALSE,log=T)
131
132         wobs0 <- df.obs$w_pred==0
133         wobs1 <- df.obs$w_pred==1
134         p.w.obs <- vector(mode='numeric', length=nrow(df.obs))
135
136         p.w.obs[wobs1] <- plogis(gamma0 + gamma_y * df.obs[wobs1]$y + gamma_z*df.obs[wobs1]$z + df.obs[wobs1]$z*df.obs[wobs1]$y* gamma_yz, log=T)
137         p.w.obs[wobs0] <- plogis(gamma0 + gamma_y * df.obs[wobs0]$y + gamma_z*df.obs[wobs0]$z + df.obs[wobs0]$z*df.obs[wobs0]$y* gamma_yz, lower.tail=FALSE, log=T)
138         
139         p.obs <- p.w.obs + p.y.obs
140
141         df.unobs <- df[is.na(y.obs)]
142
143         p.unobs.0 <- vector(mode='numeric',length=nrow(df.unobs))
144         p.unobs.1 <- vector(mode='numeric',length=nrow(df.unobs))
145
146         wunobs.0 <- df.unobs$w_pred == 0
147         wunobs.1 <- df.unobs$w_pred == 1
148         
149         p.unobs.0[wunobs.1] <- plogis(B0 + Bxy * df.unobs[wunobs.1]$x + Bzy*df.unobs[wunobs.1]$z, log=T) + plogis(gamma0 + gamma_y + gamma_z*df.unobs[wunobs.1]$z + df.unobs[wunobs.1]$z*gamma_yz, log=T)
150
151         p.unobs.0[wunobs.0] <- plogis(B0 + Bxy * df.unobs[wunobs.0]$x + Bzy*df.unobs[wunobs.0]$z, log=T) + plogis(gamma0 + gamma_y + gamma_z*df.unobs[wunobs.0]$z + df.unobs[wunobs.0]$z*gamma_yz, lower.tail=FALSE, log=T)
152
153         p.unobs.1[wunobs.1] <- plogis(B0 + Bxy * df.unobs[wunobs.1]$x + Bzy*df.unobs[wunobs.1]$z, log=T, lower.tail=FALSE) + plogis(gamma0 + gamma_z*df.unobs[wunobs.1]$z, log=T)
154
155         p.unobs.1[wunobs.0] <- plogis(B0 + Bxy * df.unobs[wunobs.0]$x + Bzy*df.unobs[wunobs.0]$z, log=T, lower.tail=FALSE) + plogis(gamma0 + gamma_z*df.unobs[wunobs.0]$z, lower.tail=FALSE, log=T)
156
157         p.unobs <- colLogSumExps(rbind(p.unobs.1, p.unobs.0))
158
159         p <- c(p.obs, p.unobs)
160
161         return(-1*(sum(p)))
162     }
163
164     mlefit <- mle2(minuslogl = nll, start = list(B0=0, Bxy=0,Bzy=0, gamma0=0, gamma_y=0, gamma_z=0, gamma_yz=0), control=list(maxit=1e6),method='L-BFGS-B')
165
166     return(mlefit)
167 }
168
169 run_simulation_depvar <- function(df, result, outcome_formula=y~x+z, proxy_formula=w_pred~y){
170
171     accuracy <- df[,mean(w_pred==y)]
172     result <- append(result, list(accuracy=accuracy))
173
174     (model.true <- glm(y ~ x + z, data=df,family=binomial(link='logit')))
175     true.ci.Bxy <- confint(model.true)['x',]
176     true.ci.Bzy <- confint(model.true)['z',]
177
178     result <- append(result, list(Bxy.est.true=coef(model.true)['x'],
179                                   Bzy.est.true=coef(model.true)['z'],
180                                   Bxy.ci.upper.true = true.ci.Bxy[2],
181                                   Bxy.ci.lower.true = true.ci.Bxy[1],
182                                   Bzy.ci.upper.true = true.ci.Bzy[2],
183                                   Bzy.ci.lower.true = true.ci.Bzy[1]))
184                                   
185     (model.feasible <- glm(y.obs~x+z,data=df,family=binomial(link='logit')))
186
187     feasible.ci.Bxy <- confint(model.feasible)['x',]
188     result <- append(result, list(Bxy.est.feasible=coef(model.feasible)['x'],
189                                   Bxy.ci.upper.feasible = feasible.ci.Bxy[2],
190                                   Bxy.ci.lower.feasible = feasible.ci.Bxy[1]))
191
192     feasible.ci.Bzy <- confint(model.feasible)['z',]
193     result <- append(result, list(Bzy.est.feasible=coef(model.feasible)['z'],
194                                   Bzy.ci.upper.feasible = feasible.ci.Bzy[2],
195                                   Bzy.ci.lower.feasible = feasible.ci.Bzy[1]))
196
197     (model.naive <- glm(w_pred~x+z, data=df, family=binomial(link='logit')))
198
199     naive.ci.Bxy <- confint(model.naive)['x',]
200     naive.ci.Bzy <- confint(model.naive)['z',]
201
202     result <- append(result, list(Bxy.est.naive=coef(model.naive)['x'],
203                                   Bzy.est.naive=coef(model.naive)['z'],
204                                   Bxy.ci.upper.naive = naive.ci.Bxy[2],
205                                   Bxy.ci.lower.naive = naive.ci.Bxy[1],
206                                   Bzy.ci.upper.naive = naive.ci.Bzy[2],
207                                   Bzy.ci.lower.naive = naive.ci.Bzy[1]))
208
209
210     (model.naive.cont <- lm(w~x+z, data=df))
211     naivecont.ci.Bxy <- confint(model.naive.cont)['x',]
212     naivecont.ci.Bzy <- confint(model.naive.cont)['z',]
213
214     ## my implementatoin of liklihood based correction
215
216     temp.df <- copy(df)
217     temp.df[,y:=y.obs]
218     mod.caroll.lik <- measerr_mle_dv(temp.df, outcome_formula=outcome_formula, proxy_formula=proxy_formula)
219     fisher.info <- solve(mod.caroll.lik$hessian)
220     coef <- mod.caroll.lik$par
221     ci.upper <- coef + sqrt(diag(fisher.info)) * 1.96
222     ci.lower <- coef - sqrt(diag(fisher.info)) * 1.96
223     result <- append(result,
224                      list(Bxy.est.mle = coef['x'],
225                           Bxy.ci.upper.mle = ci.upper['x'],
226                           Bxy.ci.lower.mle = ci.lower['x'],
227                           Bzy.est.mle = coef['z'],
228                           Bzy.ci.upper.mle = ci.upper['z'],
229                           Bzy.ci.lower.mle = ci.lower['z']))
230
231
232     ## my implementatoin of liklihood based correction
233     mod.zhang <- zhang.mle.dv(df)
234     coef <- coef(mod.zhang)
235     ci <- confint(mod.zhang,method='quad')
236
237     result <- append(result,
238                      list(Bxy.est.zhang = coef['Bxy'],
239                           Bxy.ci.upper.zhang = ci['Bxy','97.5 %'],
240                           Bxy.ci.lower.zhang = ci['Bxy','2.5 %'],
241                           Bzy.est.zhang = coef['Bzy'],
242                           Bzy.ci.upper.zhang = ci['Bzy','97.5 %'],
243                           Bzy.ci.lower.zhang = ci['Bzy','2.5 %']))
244                           
245
246     # amelia says use normal distribution for binary variables.
247     tryCatch({
248         amelia.out.k <- amelia(df, m=200, p2s=0, idvars=c('y','ystar','w'))
249         mod.amelia.k <- zelig(y.obs~x+z, model='ls', data=amelia.out.k$imputations, cite=FALSE)
250         (coefse <- combine_coef_se(mod.amelia.k, messages=FALSE))
251         est.x.mi <- coefse['x','Estimate']
252         est.x.se <- coefse['x','Std.Error']
253         result <- append(result,
254                          list(Bxy.est.amelia.full = est.x.mi,
255                               Bxy.ci.upper.amelia.full = est.x.mi + 1.96 * est.x.se,
256                               Bxy.ci.lower.amelia.full = est.x.mi - 1.96 * est.x.se
257                               ))
258
259         est.z.mi <- coefse['z','Estimate']
260         est.z.se <- coefse['z','Std.Error']
261
262         result <- append(result,
263                          list(Bzy.est.amelia.full = est.z.mi,
264                               Bzy.ci.upper.amelia.full = est.z.mi + 1.96 * est.z.se,
265                               Bzy.ci.lower.amelia.full = est.z.mi - 1.96 * est.z.se
266                               ))
267
268     },
269     error = function(e){
270         message("An error occurred:\n",e)
271         result$error <- paste0(result$error,'\n', e)
272     })
273
274
275     return(result)
276
277 }
278
279
280 ## outcome_formula, proxy_formula, and truth_formula are passed to measerr_mle 
281 run_simulation <-  function(df, result, outcome_formula=y~x+z, proxy_formula=w_pred~x, truth_formula=x~z){
282
283     accuracy <- df[,mean(w_pred==x)]
284     result <- append(result, list(accuracy=accuracy))
285
286     (model.true <- lm(y ~ x + z, data=df))
287     true.ci.Bxy <- confint(model.true)['x',]
288     true.ci.Bzy <- confint(model.true)['z',]
289
290     result <- append(result, list(Bxy.est.true=coef(model.true)['x'],
291                                   Bzy.est.true=coef(model.true)['z'],
292                                   Bxy.ci.upper.true = true.ci.Bxy[2],
293                                   Bxy.ci.lower.true = true.ci.Bxy[1],
294                                   Bzy.ci.upper.true = true.ci.Bzy[2],
295                                   Bzy.ci.lower.true = true.ci.Bzy[1]))
296                                   
297     (model.feasible <- lm(y~x.obs+z,data=df))
298
299     feasible.ci.Bxy <- confint(model.feasible)['x.obs',]
300     result <- append(result, list(Bxy.est.feasible=coef(model.feasible)['x.obs'],
301                                   Bxy.ci.upper.feasible = feasible.ci.Bxy[2],
302                                   Bxy.ci.lower.feasible = feasible.ci.Bxy[1]))
303
304     feasible.ci.Bzy <- confint(model.feasible)['z',]
305     result <- append(result, list(Bzy.est.feasible=coef(model.feasible)['z'],
306                                   Bzy.ci.upper.feasible = feasible.ci.Bzy[2],
307                                   Bzy.ci.lower.feasible = feasible.ci.Bzy[1]))
308
309     (model.naive <- lm(y~w_pred+z, data=df))
310     
311     naive.ci.Bxy <- confint(model.naive)['w_pred',]
312     naive.ci.Bzy <- confint(model.naive)['z',]
313
314     result <- append(result, list(Bxy.est.naive=coef(model.naive)['w_pred'],
315                                   Bzy.est.naive=coef(model.naive)['z'],
316                                   Bxy.ci.upper.naive = naive.ci.Bxy[2],
317                                   Bxy.ci.lower.naive = naive.ci.Bxy[1],
318                                   Bzy.ci.upper.naive = naive.ci.Bzy[2],
319                                   Bzy.ci.lower.naive = naive.ci.Bzy[1]))
320                                   
321
322     tryCatch({
323     amelia.out.k <- amelia(df, m=200, p2s=0, idvars=c('x','w_pred'))
324     mod.amelia.k <- zelig(y~x.obs+z, model='ls', data=amelia.out.k$imputations, cite=FALSE)
325     (coefse <- combine_coef_se(mod.amelia.k, messages=FALSE))
326
327     est.x.mi <- coefse['x.obs','Estimate']
328     est.x.se <- coefse['x.obs','Std.Error']
329     result <- append(result,
330                      list(Bxy.est.amelia.full = est.x.mi,
331                           Bxy.ci.upper.amelia.full = est.x.mi + 1.96 * est.x.se,
332                           Bxy.ci.lower.amelia.full = est.x.mi - 1.96 * est.x.se
333                           ))
334
335     est.z.mi <- coefse['z','Estimate']
336     est.z.se <- coefse['z','Std.Error']
337
338     result <- append(result,
339                      list(Bzy.est.amelia.full = est.z.mi,
340                           Bzy.ci.upper.amelia.full = est.z.mi + 1.96 * est.z.se,
341                           Bzy.ci.lower.amelia.full = est.z.mi - 1.96 * est.z.se
342                           ))
343
344     },
345     error = function(e){
346         message("An error occurred:\n",e)
347         result$error <-paste0(result$error,'\n', e)
348     }
349     )
350
351     tryCatch({
352         temp.df <- copy(df)
353         temp.df <- temp.df[,x:=x.obs]
354         mod.caroll.lik <- measerr_mle(temp.df, outcome_formula=outcome_formula, proxy_formula=proxy_formula, truth_formula=truth_formula)
355         fisher.info <- solve(mod.caroll.lik$hessian)
356         coef <- mod.caroll.lik$par
357         ci.upper <- coef + sqrt(diag(fisher.info)) * 1.96
358         ci.lower <- coef - sqrt(diag(fisher.info)) * 1.96
359         
360         
361         result <- append(result,
362                          list(Bxy.est.mle = coef['x'],
363                               Bxy.ci.upper.mle = ci.upper['x'],
364                               Bxy.ci.lower.mle = ci.lower['x'],
365                               Bzy.est.mle = coef['z'],
366                               Bzy.ci.upper.mle = ci.upper['z'],
367                               Bzy.ci.lower.mle = ci.lower['z']))
368     },
369
370     error = function(e){
371         message("An error occurred:\n",e)
372         result$error <- paste0(result$error,'\n', e)
373     })
374
375     tryCatch({
376
377         mod.zhang.lik <- zhang.mle.iv(df)
378         coef <- coef(mod.zhang.lik)
379         ci <- confint(mod.zhang.lik,method='quad')
380         result <- append(result,
381                          list(Bxy.est.zhang = coef['Bxy'],
382                               Bxy.ci.upper.zhang = ci['Bxy','97.5 %'],
383                               Bxy.ci.lower.zhang = ci['Bxy','2.5 %'],
384                               Bzy.est.zhang = coef['Bzy'],
385                               Bzy.ci.upper.zhang = ci['Bzy','97.5 %'],
386                               Bzy.ci.lower.zhang = ci['Bzy','2.5 %']))
387     },
388
389     error = function(e){
390         message("An error occurred:\n",e)
391         result$error <- paste0(result$error,'\n', e)
392     })
393
394     ## What if we can't observe k -- most realistic scenario. We can't include all the ML features in a model.
395     ## amelia.out.nok <- amelia(df, m=200, p2s=0, idvars=c("x","w_pred"), noms=noms)
396     ## mod.amelia.nok <- zelig(y~x.obs+g, model='ls', data=amelia.out.nok$imputations, cite=FALSE)
397     ## (coefse <- combine_coef_se(mod.amelia.nok, messages=FALSE))
398
399     ## est.x.mi <- coefse['x.obs','Estimate']
400     ## est.x.se <- coefse['x.obs','Std.Error']
401     ## result <- append(result,
402     ##                  list(Bxy.est.amelia.nok = est.x.mi,
403     ##                       Bxy.ci.upper.amelia.nok = est.x.mi + 1.96 * est.x.se,
404     ##                       Bxy.ci.lower.amelia.nok = est.x.mi - 1.96 * est.x.se
405     ##                       ))
406
407     ## est.g.mi <- coefse['g','Estimate']
408     ## est.g.se <- coefse['g','Std.Error']
409
410     ## result <- append(result,
411     ##                  list(Bgy.est.amelia.nok = est.g.mi,
412     ##                       Bgy.ci.upper.amelia.nok = est.g.mi + 1.96 * est.g.se,
413     ##                       Bgy.ci.lower.amelia.nok = est.g.mi - 1.96 * est.g.se
414     ##                       ))
415
416     N <- nrow(df)
417     m <- nrow(df[!is.na(x.obs)])
418     p <- v <- train <- rep(0,N)
419     M <- m
420     p[(M+1):(N)] <- 1
421     v[1:(M)] <- 1
422     df <- df[order(x.obs)]
423     y <- df[,y]
424     x <- df[,x.obs]
425     z <- df[,z]
426     w <- df[,w_pred]
427     # gmm gets pretty close
428     (gmm.res <- predicted_covariates(y, x, z, w, v, train, p, max_iter=100, verbose=TRUE))
429
430     result <- append(result,
431                      list(Bxy.est.gmm = gmm.res$beta[1,1],
432                           Bxy.ci.upper.gmm = gmm.res$confint[1,2],
433                           Bxy.ci.lower.gmm = gmm.res$confint[1,1],
434                           gmm.ER_pval = gmm.res$ER_pval
435                           ))
436
437     result <- append(result,
438                      list(Bzy.est.gmm = gmm.res$beta[2,1],
439                           Bzy.ci.upper.gmm = gmm.res$confint[2,2],
440                           Bzy.ci.lower.gmm = gmm.res$confint[2,1]))
441
442
443     tryCatch({
444     mod.calibrated.mle <- mecor(y ~ MeasError(w_pred, reference = x.obs) + z, df, B=400, method='efficient')
445     (mod.calibrated.mle)
446     (mecor.ci <- summary(mod.calibrated.mle)$c$ci['x.obs',])
447     result <- append(result, list(
448                                  Bxy.est.mecor = mecor.ci['Estimate'],
449                                  Bxy.ci.upper.mecor = mecor.ci['UCI'],
450                                  Bxy.ci.lower.mecor = mecor.ci['LCI'])
451                      )
452
453     (mecor.ci <- summary(mod.calibrated.mle)$c$ci['z',])
454
455     result <- append(result, list(
456                                  Bzy.est.mecor = mecor.ci['Estimate'],
457                                  Bzy.ci.upper.mecor = mecor.ci['UCI'],
458                                  Bzy.ci.lower.mecor = mecor.ci['LCI'])
459                      )
460     },
461     error = function(e){
462         message("An error occurred:\n",e)
463         result$error <- paste0(result$error, '\n', e)
464     }
465     )
466 ##    clean up memory
467 ##    rm(list=c("df","y","x","g","w","v","train","p","amelia.out.k","amelia.out.nok", "mod.calibrated.mle","gmm.res","mod.amelia.k","mod.amelia.nok", "model.true","model.naive","model.feasible"))
468     
469 ##    gc()
470     return(result)
471 }

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