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 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)]
49 ### Problem: accounting for uncertainty in ppv / npv
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))
55 ll.y.obs <- with(df.obs, dnorm(y, B0 + Bxy * x + Bzy * z, sd=sigma_y,log=T))
57 ll <- ll + sum(ll.w1x1.obs) + sum(ll.w0x0.obs)
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))
64 lls.x.1 <- colLogSumExps(rbind(log(ppv) + ll.x.1, log(1-ppv) + ll.x.0))
67 lls.x.0 <- colLogSumExps(rbind(log(1-npv) + ll.x.1, log(npv) + ll.x.0))
69 lls <- colLogSumExps(rbind(lls.x.1, lls.x.0))
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')
78 ## this is equivalent to the pseudo-liklihood model from Carolla
79 zhang.mle.dv <- function(df){
81 nll <- function(B0=0, Bxy=0, Bzy=0, ppv=0.9, npv=0.9){
82 df.obs <- df[!is.na(y.obs)]
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))
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))
93 ll <- sum(ll.y.obs) + sum(ll.w0y0) + sum(ll.w1y1)
95 # unobserved case; integrate out y
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))
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))
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))
109 lls <- colLogSumExps(rbind(lls.y.1, lls.y.0))
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))
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){
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)]
127 p.y.obs <- vector(mode='numeric', length=nrow(df.obs))
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)
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))
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)
139 p.obs <- p.w.obs + p.y.obs
141 df.unobs <- df[is.na(y.obs)]
143 p.unobs.0 <- vector(mode='numeric',length=nrow(df.unobs))
144 p.unobs.1 <- vector(mode='numeric',length=nrow(df.unobs))
146 wunobs.0 <- df.unobs$w_pred == 0
147 wunobs.1 <- df.unobs$w_pred == 1
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)
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)
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)
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)
157 p.unobs <- colLogSumExps(rbind(p.unobs.1, p.unobs.0))
159 p <- c(p.obs, p.unobs)
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')
169 run_simulation_depvar <- function(df, result, outcome_formula=y~x+z, proxy_formula=w_pred~y){
171 accuracy <- df[,mean(w_pred==y)]
172 result <- append(result, list(accuracy=accuracy))
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',]
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]))
185 (model.feasible <- glm(y.obs~x+z,data=df,family=binomial(link='logit')))
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]))
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]))
197 (model.naive <- glm(w_pred~x+z, data=df, family=binomial(link='logit')))
199 naive.ci.Bxy <- confint(model.naive)['x',]
200 naive.ci.Bzy <- confint(model.naive)['z',]
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]))
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',]
214 ## my implementatoin of liklihood based correction
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']))
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')
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 %']))
246 # amelia says use normal distribution for binary variables.
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
259 est.z.mi <- coefse['z','Estimate']
260 est.z.se <- coefse['z','Std.Error']
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
270 message("An error occurred:\n",e)
271 result$error <- paste0(result$error,'\n', e)
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){
283 accuracy <- df[,mean(w_pred==x)]
284 result <- append(result, list(accuracy=accuracy))
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',]
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]))
297 (model.feasible <- lm(y~x.obs+z,data=df))
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]))
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]))
309 (model.naive <- lm(y~w_pred+z, data=df))
311 naive.ci.Bxy <- confint(model.naive)['w_pred',]
312 naive.ci.Bzy <- confint(model.naive)['z',]
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]))
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))
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
335 est.z.mi <- coefse['z','Estimate']
336 est.z.se <- coefse['z','Std.Error']
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
346 message("An error occurred:\n",e)
347 result$error <-paste0(result$error,'\n', e)
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
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']))
371 message("An error occurred:\n",e)
372 result$error <- paste0(result$error,'\n', e)
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 %']))
390 message("An error occurred:\n",e)
391 result$error <- paste0(result$error,'\n', e)
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))
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
407 ## est.g.mi <- coefse['g','Estimate']
408 ## est.g.se <- coefse['g','Std.Error']
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
417 m <- nrow(df[!is.na(x.obs)])
418 p <- v <- train <- rep(0,N)
422 df <- df[order(x.obs)]
427 # gmm gets pretty close
428 (gmm.res <- predicted_covariates(y, x, z, w, v, train, p, max_iter=100, verbose=TRUE))
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
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]))
444 mod.calibrated.mle <- mecor(y ~ MeasError(w_pred, reference = x.obs) + z, df, B=400, method='efficient')
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'])
453 (mecor.ci <- summary(mod.calibrated.mle)$c$ci['z',])
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'])
462 message("An error occurred:\n",e)
463 result$error <- paste0(result$error, '\n', e)
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"))