1 library(predictionError)
3 options(amelia.parallel="no",
8 library(matrixStats) # for numerically stable logsumexps
10 source("measerr_methods.R") ## for my more generic function.
12 ## This uses the pseudolikelihood approach from Carroll page 349.
14 ## assumes differential error, but that only depends on Y
15 ## inefficient, because pseudolikelihood
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)
22 nll <- function(B0, Bxy, Bzy){
24 pw <- vector(mode='numeric',length=nrow(df))
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)
30 probs <- colLogSumExps(rbind(log(1 - p0.est), log(p1.est + p0.est - 1) + pw))
34 mlefit <- mle2(minuslogl = nll, start = list(B0=0.0, Bxy=0.0, Bzy=0.0), control=list(maxit=1e6),method='L-BFGS-B')
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 df.obs <- df[!is.na(x.obs)]
45 df.unobs <- df[is.na(x.obs)]
47 tn <- df.obs[(w_pred == 0) & (x.obs == w_pred),.N]
48 pn <- df.obs[(w_pred==0), .N]
51 tp <- df.obs[(w_pred==1) & (x.obs == w_pred),.N]
52 pp <- df.obs[(w_pred==1),.N]
55 nll <- function(B0=0, Bxy=0, Bzy=0, sigma_y=0.1){
58 ### Problem: accounting for uncertainty in ppv / npv
61 ll.y.obs <- with(df.obs, dnorm(y, B0 + Bxy * x + Bzy * z, sd=sigma_y,log=T))
64 # unobserved case; integrate out x
65 ll.x.1 <- with(df.unobs, dnorm(y, B0 + Bxy + Bzy * z, sd = sigma_y, log=T))
66 ll.x.0 <- with(df.unobs, dnorm(y, B0 + Bzy * z, sd = sigma_y,log=T))
69 lls.x.1 <- colLogSumExps(rbind(log(ppv) + ll.x.1, log(1-ppv) + ll.x.0))
72 lls.x.0 <- colLogSumExps(rbind(log(1-npv) + ll.x.1, log(npv) + ll.x.0))
74 lls <- colLogSumExps(rbind(df.unobs$w_pred * lls.x.1, (1-df.unobs$w_pred) * lls.x.0))
78 mlefit <- mle2(minuslogl = nll, control=list(maxit=1e6), lower=list(sigma_y=0.0001, B0=-Inf, Bxy=-Inf, Bzy=-Inf),
79 upper=list(sigma_y=Inf, B0=Inf, Bxy=Inf, Bzy=Inf),method='L-BFGS-B')
83 ## this is equivalent to the pseudo-liklihood model from Caroll
84 ## zhang.mle.dv <- function(df){
86 ## nll <- function(B0=0, Bxy=0, Bzy=0, ppv=0.9, npv=0.9){
87 ## df.obs <- df[!is.na(y.obs)]
90 ## ll.w0y0 <- with(df.obs[y.obs==0],dbinom(1-w_pred,1,npv,log=TRUE))
91 ## ll.w1y1 <- with(df.obs[y.obs==1],dbinom(w_pred,1,ppv,log=TRUE))
94 ## ll.y.obs <- vector(mode='numeric', length=nrow(df.obs))
95 ## ll.y.obs[df.obs$y.obs==1] <- with(df.obs[y.obs==1], plogis(B0 + Bxy * x + Bzy * z,log=T))
96 ## 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))
98 ## ll <- sum(ll.y.obs) + sum(ll.w0y0) + sum(ll.w1y1)
100 ## # unobserved case; integrate out y
102 ## ll.y.1 <- vector(mode='numeric', length=nrow(df))
103 ## pi.y.1 <- with(df,plogis(B0 + Bxy * x + Bzy*z, log=T))
104 ## ## 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)
105 ## lls.y.1 <- colLogSumExps(rbind(log(ppv) + pi.y.1, log(1-ppv) + pi.y.1))
108 ## ll.y.0 <- vector(mode='numeric', length=nrow(df))
109 ## pi.y.0 <- with(df,plogis(B0 + Bxy * x + Bzy*z, log=T,lower.tail=FALSE))
111 ## ## 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)
112 ## lls.y.0 <- colLogSumExps(rbind(log(npv) + pi.y.0, log(1-npv) + pi.y.0))
114 ## lls <- colLogSumExps(rbind(lls.y.1, lls.y.0))
115 ## ll <- ll + sum(lls)
118 ## 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),
119 ## upper=list(B0=Inf, Bxy=Inf, Bzy=Inf,ppv=0.999,npv=0.999))
123 zhang.mle.dv <- function(df){
124 df.obs <- df[!is.na(y.obs)]
125 df.unobs <- df[is.na(y.obs)]
127 fp <- df.obs[(w_pred==1) & (y.obs != w_pred),.N]
128 p <- df.obs[(w_pred==1),.N]
130 fn <- df.obs[(w_pred==0) & (y.obs != w_pred), .N]
131 n <- df.obs[(w_pred==0),.N]
134 nll <- function(B0=0, Bxy=0, Bzy=0){
138 ll.y.obs <- vector(mode='numeric', length=nrow(df.obs))
139 ll.y.obs[df.obs$y.obs==1] <- with(df.obs[y.obs==1], plogis(B0 + Bxy * x + Bzy * z,log=T))
140 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))
144 pi.y.1 <- with(df,plogis(B0 + Bxy * x + Bzy*z, log=T))
145 pi.y.0 <- with(df,plogis(B0 + Bxy * x + Bzy*z, log=T,lower.tail=FALSE))
147 lls <- with(df.unobs, colLogSumExps(rbind(w_pred * colLogSumExps(rbind(log(fpr), log(1 - fnr - fpr)+pi.y.1)),
148 (1-w_pred) * colLogSumExps(rbind(log(1-fpr), log(1 - fnr - fpr)+pi.y.0)))))
153 mlefit <- mle2(minuslogl = nll, control=list(maxit=1e6),method='L-BFGS-B',lower=c(B0=-Inf, Bxy=-Inf, Bzy=-Inf),
154 upper=c(B0=Inf, Bxy=Inf, Bzy=Inf))
158 ## This uses the likelihood approach from Carroll page 353.
159 ## assumes that we have a good measurement error model
160 my.mle <- function(df){
162 ## liklihood for observed responses
163 nll <- function(B0, Bxy, Bzy, gamma0, gamma_y, gamma_z, gamma_yz){
164 df.obs <- df[!is.na(y.obs)]
167 p.y.obs <- vector(mode='numeric', length=nrow(df.obs))
169 p.y.obs[yobs1] <- plogis(B0 + Bxy * df.obs[yobs1]$x + Bzy*df.obs[yobs1]$z,log=T)
170 p.y.obs[yobs0] <- plogis(B0 + Bxy * df.obs[yobs0]$x + Bzy*df.obs[yobs0]$z,lower.tail=FALSE,log=T)
172 wobs0 <- df.obs$w_pred==0
173 wobs1 <- df.obs$w_pred==1
174 p.w.obs <- vector(mode='numeric', length=nrow(df.obs))
176 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)
177 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)
179 p.obs <- p.w.obs + p.y.obs
181 df.unobs <- df[is.na(y.obs)]
183 p.unobs.0 <- vector(mode='numeric',length=nrow(df.unobs))
184 p.unobs.1 <- vector(mode='numeric',length=nrow(df.unobs))
186 wunobs.0 <- df.unobs$w_pred == 0
187 wunobs.1 <- df.unobs$w_pred == 1
189 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)
191 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)
193 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)
195 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)
197 p.unobs <- colLogSumExps(rbind(p.unobs.1, p.unobs.0))
199 p <- c(p.obs, p.unobs)
204 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')
209 run_simulation_depvar <- function(df, result, outcome_formula=y~x+z, proxy_formula=w_pred~y){
211 accuracy <- df[,mean(w_pred==y)]
212 result <- append(result, list(accuracy=accuracy))
214 (model.true <- glm(y ~ x + z, data=df,family=binomial(link='logit')))
215 true.ci.Bxy <- confint(model.true)['x',]
216 true.ci.Bzy <- confint(model.true)['z',]
218 result <- append(result, list(Bxy.est.true=coef(model.true)['x'],
219 Bzy.est.true=coef(model.true)['z'],
220 Bxy.ci.upper.true = true.ci.Bxy[2],
221 Bxy.ci.lower.true = true.ci.Bxy[1],
222 Bzy.ci.upper.true = true.ci.Bzy[2],
223 Bzy.ci.lower.true = true.ci.Bzy[1]))
225 (model.feasible <- glm(y.obs~x+z,data=df,family=binomial(link='logit')))
227 feasible.ci.Bxy <- confint(model.feasible)['x',]
228 result <- append(result, list(Bxy.est.feasible=coef(model.feasible)['x'],
229 Bxy.ci.upper.feasible = feasible.ci.Bxy[2],
230 Bxy.ci.lower.feasible = feasible.ci.Bxy[1]))
232 feasible.ci.Bzy <- confint(model.feasible)['z',]
233 result <- append(result, list(Bzy.est.feasible=coef(model.feasible)['z'],
234 Bzy.ci.upper.feasible = feasible.ci.Bzy[2],
235 Bzy.ci.lower.feasible = feasible.ci.Bzy[1]))
237 (model.naive <- glm(w_pred~x+z, data=df, family=binomial(link='logit')))
239 naive.ci.Bxy <- confint(model.naive)['x',]
240 naive.ci.Bzy <- confint(model.naive)['z',]
242 result <- append(result, list(Bxy.est.naive=coef(model.naive)['x'],
243 Bzy.est.naive=coef(model.naive)['z'],
244 Bxy.ci.upper.naive = naive.ci.Bxy[2],
245 Bxy.ci.lower.naive = naive.ci.Bxy[1],
246 Bzy.ci.upper.naive = naive.ci.Bzy[2],
247 Bzy.ci.lower.naive = naive.ci.Bzy[1]))
250 (model.naive.cont <- lm(w~x+z, data=df))
251 naivecont.ci.Bxy <- confint(model.naive.cont)['x',]
252 naivecont.ci.Bzy <- confint(model.naive.cont)['z',]
254 ## my implementation of liklihood based correction
258 mod.caroll.lik <- measerr_mle_dv(temp.df, outcome_formula=outcome_formula, proxy_formula=proxy_formula)
259 fisher.info <- solve(mod.caroll.lik$hessian)
260 coef <- mod.caroll.lik$par
261 ci.upper <- coef + sqrt(diag(fisher.info)) * 1.96
262 ci.lower <- coef - sqrt(diag(fisher.info)) * 1.96
263 result <- append(result,
264 list(Bxy.est.mle = coef['x'],
265 Bxy.ci.upper.mle = ci.upper['x'],
266 Bxy.ci.lower.mle = ci.lower['x'],
267 Bzy.est.mle = coef['z'],
268 Bzy.ci.upper.mle = ci.upper['z'],
269 Bzy.ci.lower.mle = ci.lower['z']))
272 ## my implementatoin of liklihood based correction
273 mod.zhang <- zhang.mle.dv(df)
274 coef <- coef(mod.zhang)
275 ci <- confint(mod.zhang,method='quad')
277 result <- append(result,
278 list(Bxy.est.zhang = coef['Bxy'],
279 Bxy.ci.upper.zhang = ci['Bxy','97.5 %'],
280 Bxy.ci.lower.zhang = ci['Bxy','2.5 %'],
281 Bzy.est.zhang = coef['Bzy'],
282 Bzy.ci.upper.zhang = ci['Bzy','97.5 %'],
283 Bzy.ci.lower.zhang = ci['Bzy','2.5 %']))
287 # amelia says use normal distribution for binary variables.
289 amelia.out.k <- amelia(df, m=200, p2s=0, idvars=c('y','ystar','w'))
290 mod.amelia.k <- zelig(y.obs~x+z, model='ls', data=amelia.out.k$imputations, cite=FALSE)
291 (coefse <- combine_coef_se(mod.amelia.k, messages=FALSE))
292 est.x.mi <- coefse['x','Estimate']
293 est.x.se <- coefse['x','Std.Error']
294 result <- append(result,
295 list(Bxy.est.amelia.full = est.x.mi,
296 Bxy.ci.upper.amelia.full = est.x.mi + 1.96 * est.x.se,
297 Bxy.ci.lower.amelia.full = est.x.mi - 1.96 * est.x.se
300 est.z.mi <- coefse['z','Estimate']
301 est.z.se <- coefse['z','Std.Error']
303 result <- append(result,
304 list(Bzy.est.amelia.full = est.z.mi,
305 Bzy.ci.upper.amelia.full = est.z.mi + 1.96 * est.z.se,
306 Bzy.ci.lower.amelia.full = est.z.mi - 1.96 * est.z.se
311 message("An error occurred:\n",e)
312 result$error <- paste0(result$error,'\n', e)
321 ## outcome_formula, proxy_formula, and truth_formula are passed to measerr_mle
322 run_simulation <- function(df, result, outcome_formula=y~x+z, proxy_formula=NULL, truth_formula=NULL){
324 accuracy <- df[,mean(w_pred==x)]
325 result <- append(result, list(accuracy=accuracy))
327 (model.true <- lm(y ~ x + z, data=df))
328 true.ci.Bxy <- confint(model.true)['x',]
329 true.ci.Bzy <- confint(model.true)['z',]
331 result <- append(result, list(Bxy.est.true=coef(model.true)['x'],
332 Bzy.est.true=coef(model.true)['z'],
333 Bxy.ci.upper.true = true.ci.Bxy[2],
334 Bxy.ci.lower.true = true.ci.Bxy[1],
335 Bzy.ci.upper.true = true.ci.Bzy[2],
336 Bzy.ci.lower.true = true.ci.Bzy[1]))
338 (model.feasible <- lm(y~x.obs+z,data=df))
340 feasible.ci.Bxy <- confint(model.feasible)['x.obs',]
341 result <- append(result, list(Bxy.est.feasible=coef(model.feasible)['x.obs'],
342 Bxy.ci.upper.feasible = feasible.ci.Bxy[2],
343 Bxy.ci.lower.feasible = feasible.ci.Bxy[1]))
345 feasible.ci.Bzy <- confint(model.feasible)['z',]
346 result <- append(result, list(Bzy.est.feasible=coef(model.feasible)['z'],
347 Bzy.ci.upper.feasible = feasible.ci.Bzy[2],
348 Bzy.ci.lower.feasible = feasible.ci.Bzy[1]))
350 (model.naive <- lm(y~w_pred+z, data=df))
352 naive.ci.Bxy <- confint(model.naive)['w_pred',]
353 naive.ci.Bzy <- confint(model.naive)['z',]
355 result <- append(result, list(Bxy.est.naive=coef(model.naive)['w_pred'],
356 Bzy.est.naive=coef(model.naive)['z'],
357 Bxy.ci.upper.naive = naive.ci.Bxy[2],
358 Bxy.ci.lower.naive = naive.ci.Bxy[1],
359 Bzy.ci.upper.naive = naive.ci.Bzy[2],
360 Bzy.ci.lower.naive = naive.ci.Bzy[1]))
364 amelia.out.k <- amelia(df, m=200, p2s=0, idvars=c('x','w'))
365 mod.amelia.k <- zelig(y~x.obs+z, model='ls', data=amelia.out.k$imputations, cite=FALSE)
366 (coefse <- combine_coef_se(mod.amelia.k, messages=FALSE))
368 est.x.mi <- coefse['x.obs','Estimate']
369 est.x.se <- coefse['x.obs','Std.Error']
370 result <- append(result,
371 list(Bxy.est.amelia.full = est.x.mi,
372 Bxy.ci.upper.amelia.full = est.x.mi + 1.96 * est.x.se,
373 Bxy.ci.lower.amelia.full = est.x.mi - 1.96 * est.x.se
376 est.z.mi <- coefse['z','Estimate']
377 est.z.se <- coefse['z','Std.Error']
379 result <- append(result,
380 list(Bzy.est.amelia.full = est.z.mi,
381 Bzy.ci.upper.amelia.full = est.z.mi + 1.96 * est.z.se,
382 Bzy.ci.lower.amelia.full = est.z.mi - 1.96 * est.z.se
387 message("An error occurred:\n",e)
388 result$error <-paste0(result$error,'\n', e)
394 temp.df <- temp.df[,x:=x.obs]
395 mod.caroll.lik <- measerr_mle(temp.df, outcome_formula=outcome_formula, proxy_formula=proxy_formula, truth_formula=truth_formula)
396 fisher.info <- solve(mod.caroll.lik$hessian)
397 coef <- mod.caroll.lik$par
398 ci.upper <- coef + sqrt(diag(fisher.info)) * 1.96
399 ci.lower <- coef - sqrt(diag(fisher.info)) * 1.96
402 result <- append(result,
403 list(Bxy.est.mle = coef['x'],
404 Bxy.ci.upper.mle = ci.upper['x'],
405 Bxy.ci.lower.mle = ci.lower['x'],
406 Bzy.est.mle = coef['z'],
407 Bzy.ci.upper.mle = ci.upper['z'],
408 Bzy.ci.lower.mle = ci.lower['z']))
412 message("An error occurred:\n",e)
413 result$error <- paste0(result$error,'\n', e)
418 mod.zhang.lik <- zhang.mle.iv(df)
419 coef <- coef(mod.zhang.lik)
420 ci <- confint(mod.zhang.lik,method='quad')
421 result <- append(result,
422 list(Bxy.est.zhang = coef['Bxy'],
423 Bxy.ci.upper.zhang = ci['Bxy','97.5 %'],
424 Bxy.ci.lower.zhang = ci['Bxy','2.5 %'],
425 Bzy.est.zhang = coef['Bzy'],
426 Bzy.ci.upper.zhang = ci['Bzy','97.5 %'],
427 Bzy.ci.lower.zhang = ci['Bzy','2.5 %']))
431 message("An error occurred:\n",e)
432 result$error <- paste0(result$error,'\n', e)
435 ## What if we can't observe k -- most realistic scenario. We can't include all the ML features in a model.
436 ## amelia.out.nok <- amelia(df, m=200, p2s=0, idvars=c("x","w_pred"), noms=noms)
437 ## mod.amelia.nok <- zelig(y~x.obs+g, model='ls', data=amelia.out.nok$imputations, cite=FALSE)
438 ## (coefse <- combine_coef_se(mod.amelia.nok, messages=FALSE))
440 ## est.x.mi <- coefse['x.obs','Estimate']
441 ## est.x.se <- coefse['x.obs','Std.Error']
442 ## result <- append(result,
443 ## list(Bxy.est.amelia.nok = est.x.mi,
444 ## Bxy.ci.upper.amelia.nok = est.x.mi + 1.96 * est.x.se,
445 ## Bxy.ci.lower.amelia.nok = est.x.mi - 1.96 * est.x.se
448 ## est.g.mi <- coefse['g','Estimate']
449 ## est.g.se <- coefse['g','Std.Error']
451 ## result <- append(result,
452 ## list(Bgy.est.amelia.nok = est.g.mi,
453 ## Bgy.ci.upper.amelia.nok = est.g.mi + 1.96 * est.g.se,
454 ## Bgy.ci.lower.amelia.nok = est.g.mi - 1.96 * est.g.se
458 m <- nrow(df[!is.na(x.obs)])
459 p <- v <- train <- rep(0,N)
463 df <- df[order(x.obs)]
468 # gmm gets pretty close
469 (gmm.res <- predicted_covariates(y, x, z, w, v, train, p, max_iter=100, verbose=TRUE))
471 result <- append(result,
472 list(Bxy.est.gmm = gmm.res$beta[1,1],
473 Bxy.ci.upper.gmm = gmm.res$confint[1,2],
474 Bxy.ci.lower.gmm = gmm.res$confint[1,1],
475 gmm.ER_pval = gmm.res$ER_pval
478 result <- append(result,
479 list(Bzy.est.gmm = gmm.res$beta[2,1],
480 Bzy.ci.upper.gmm = gmm.res$confint[2,2],
481 Bzy.ci.lower.gmm = gmm.res$confint[2,1]))
485 mod.calibrated.mle <- mecor(y ~ MeasError(w_pred, reference = x.obs) + z, df, B=400, method='efficient')
487 (mecor.ci <- summary(mod.calibrated.mle)$c$ci['x.obs',])
488 result <- append(result, list(
489 Bxy.est.mecor = mecor.ci['Estimate'],
490 Bxy.ci.upper.mecor = mecor.ci['UCI'],
491 Bxy.ci.lower.mecor = mecor.ci['LCI'])
494 (mecor.ci <- summary(mod.calibrated.mle)$c$ci['z',])
496 result <- append(result, list(
497 Bzy.est.mecor = mecor.ci['Estimate'],
498 Bzy.ci.upper.mecor = mecor.ci['UCI'],
499 Bzy.ci.lower.mecor = mecor.ci['LCI'])
503 message("An error occurred:\n",e)
504 result$error <- paste0(result$error, '\n', e)
508 ## 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"))