1 library(predictionError)
3 options(amelia.parallel="no",
8 library(matrixStats) # for numerically stable logsumexps
10 source("pl_methods.R")
11 source("measerr_methods.R") ## for my more generic function.
13 ## This uses the pseudolikelihood approach from Carroll page 349.
15 ## assumes differential error, but that only depends on Y
16 ## inefficient, because pseudolikelihood
18 ## This uses the pseudo-likelihood approach from Carroll page 346.
19 my.pseudo.mle <- function(df){
20 p1.est <- mean(df[w_pred==1]$y.obs==1,na.rm=T)
21 p0.est <- mean(df[w_pred==0]$y.obs==0,na.rm=T)
23 nll <- function(B0, Bxy, Bzy){
25 pw <- vector(mode='numeric',length=nrow(df))
28 pw[df$w_pred==1] <- plogis(B0 + Bxy * dfw1$x + Bzy * dfw1$z, log=T)
29 pw[df$w_pred==0] <- plogis(B0 + Bxy * dfw0$x + Bzy * dfw0$z, lower.tail=FALSE, log=T)
31 probs <- colLogSumExps(rbind(log(1 - p0.est), log(p1.est + p0.est - 1) + pw))
35 mlefit <- mle2(minuslogl = nll, start = list(B0=0.0, Bxy=0.0, Bzy=0.0), control=list(maxit=1e6),method='L-BFGS-B')
41 ## This uses the likelihood approach from Carroll page 353.
42 ## assumes that we have a good measurement error model
43 my.mle <- function(df){
45 ## liklihood for observed responses
46 nll <- function(B0, Bxy, Bzy, gamma0, gamma_y, gamma_z, gamma_yz){
47 df.obs <- df[!is.na(y.obs)]
50 p.y.obs <- vector(mode='numeric', length=nrow(df.obs))
52 p.y.obs[yobs1] <- plogis(B0 + Bxy * df.obs[yobs1]$x + Bzy*df.obs[yobs1]$z,log=T)
53 p.y.obs[yobs0] <- plogis(B0 + Bxy * df.obs[yobs0]$x + Bzy*df.obs[yobs0]$z,lower.tail=FALSE,log=T)
55 wobs0 <- df.obs$w_pred==0
56 wobs1 <- df.obs$w_pred==1
57 p.w.obs <- vector(mode='numeric', length=nrow(df.obs))
59 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)
60 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)
62 p.obs <- p.w.obs + p.y.obs
64 df.unobs <- df[is.na(y.obs)]
66 p.unobs.0 <- vector(mode='numeric',length=nrow(df.unobs))
67 p.unobs.1 <- vector(mode='numeric',length=nrow(df.unobs))
69 wunobs.0 <- df.unobs$w_pred == 0
70 wunobs.1 <- df.unobs$w_pred == 1
72 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)
74 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)
76 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)
78 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)
80 p.unobs <- colLogSumExps(rbind(p.unobs.1, p.unobs.0))
82 p <- c(p.obs, p.unobs)
87 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')
92 run_simulation_depvar <- function(df, result, outcome_formula=y~x+z, proxy_formula=w_pred~y, confint_method='quad'){
94 (accuracy <- df[,mean(w_pred==y)])
95 result <- append(result, list(accuracy=accuracy))
96 (error.cor.z <- cor(df$z, df$y - df$w_pred))
97 (error.cor.x <- cor(df$x, df$y - df$w_pred))
98 (error.cor.y <- cor(df$y, df$y - df$w_pred))
99 result <- append(result, list(error.cor.x = error.cor.x,
100 error.cor.z = error.cor.z,
101 error.cor.y = error.cor.y))
103 model.null <- glm(y~1, data=df,family=binomial(link='logit'))
104 (model.true <- glm(y ~ x + z, data=df,family=binomial(link='logit')))
105 (lik.ratio <- exp(logLik(model.true) - logLik(model.null)))
107 true.ci.Bxy <- confint(model.true)['x',]
108 true.ci.Bzy <- confint(model.true)['z',]
110 result <- append(result, list(cor.xz=cor(df$x,df$z)))
111 result <- append(result, list(lik.ratio=lik.ratio))
113 result <- append(result, list(Bxy.est.true=coef(model.true)['x'],
114 Bzy.est.true=coef(model.true)['z'],
115 Bxy.ci.upper.true = true.ci.Bxy[2],
116 Bxy.ci.lower.true = true.ci.Bxy[1],
117 Bzy.ci.upper.true = true.ci.Bzy[2],
118 Bzy.ci.lower.true = true.ci.Bzy[1]))
120 (model.feasible <- glm(y.obs~x+z,data=df,family=binomial(link='logit')))
122 feasible.ci.Bxy <- confint(model.feasible)['x',]
123 result <- append(result, list(Bxy.est.feasible=coef(model.feasible)['x'],
124 Bxy.ci.upper.feasible = feasible.ci.Bxy[2],
125 Bxy.ci.lower.feasible = feasible.ci.Bxy[1]))
127 feasible.ci.Bzy <- confint(model.feasible)['z',]
128 result <- append(result, list(Bzy.est.feasible=coef(model.feasible)['z'],
129 Bzy.ci.upper.feasible = feasible.ci.Bzy[2],
130 Bzy.ci.lower.feasible = feasible.ci.Bzy[1]))
132 (model.naive <- glm(w_pred~x+z, data=df, family=binomial(link='logit')))
134 naive.ci.Bxy <- confint(model.naive)['x',]
135 naive.ci.Bzy <- confint(model.naive)['z',]
137 result <- append(result, list(Bxy.est.naive=coef(model.naive)['x'],
138 Bzy.est.naive=coef(model.naive)['z'],
139 Bxy.ci.upper.naive = naive.ci.Bxy[2],
140 Bxy.ci.lower.naive = naive.ci.Bxy[1],
141 Bzy.ci.upper.naive = naive.ci.Bzy[2],
142 Bzy.ci.lower.naive = naive.ci.Bzy[1]))
145 (model.naive.cont <- lm(w~x+z, data=df))
146 naivecont.ci.Bxy <- confint(model.naive.cont)['x',]
147 naivecont.ci.Bzy <- confint(model.naive.cont)['z',]
149 ## my implementation of liklihood based correction
154 if(confint_method=='quad'){
155 mod.caroll.lik <- measerr_mle_dv(temp.df, outcome_formula=outcome_formula, proxy_formula=proxy_formula)
156 fischer.info <- solve(mod.caroll.lik$hessian)
157 coef <- mod.caroll.lik$par
158 ci.upper <- coef + sqrt(diag(fischer.info)) * 1.96
159 ci.lower <- coef - sqrt(diag(fischer.info)) * 1.96
161 else{ ## confint_method is 'profile'
163 mod.caroll.lik <- measerr_mle_dv(temp.df, outcome_formula=outcome_formula, proxy_formula=proxy_formula, method='bbmle')
164 coef <- coef(mod.caroll.lik)
165 ci <- confint(mod.caroll.lik, method='spline')
166 ci.lower <- ci[,'2.5 %']
167 ci.upper <- ci[,'97.5 %']
170 result <- append(result,
171 list(Bxy.est.mle = coef['x'],
172 Bxy.ci.upper.mle = ci.upper['x'],
173 Bxy.ci.lower.mle = ci.lower['x'],
174 Bzy.est.mle = coef['z'],
175 Bzy.ci.upper.mle = ci.upper['z'],
176 Bzy.ci.lower.mle = ci.lower['z']))
179 ## my implementatoin of liklihood based correction
180 mod.zhang <- zhang.mle.dv(df)
181 coef <- coef(mod.zhang)
182 ci <- confint(mod.zhang,method='quad')
184 result <- append(result,
185 list(Bxy.est.zhang = coef['Bxy'],
186 Bxy.ci.upper.zhang = ci['Bxy','97.5 %'],
187 Bxy.ci.lower.zhang = ci['Bxy','2.5 %'],
188 Bzy.est.zhang = coef['Bzy'],
189 Bzy.ci.upper.zhang = ci['Bzy','97.5 %'],
190 Bzy.ci.lower.zhang = ci['Bzy','2.5 %']))
194 # amelia says use normal distribution for binary variables.
195 amelia_result <- list(Bxy.est.amelia.full = NA,
196 Bxy.ci.upper.amelia.full = NA,
197 Bxy.ci.lower.amelia.full = NA,
198 Bzy.est.amelia.full = NA,
199 Bzy.ci.upper.amelia.full = NA,
200 Bzy.ci.lower.amelia.full = NA
204 amelia.out.k <- amelia(df, m=200, p2s=0, idvars=c('y','ystar','w'))
205 mod.amelia.k <- zelig(y.obs~x+z, model='ls', data=amelia.out.k$imputations, cite=FALSE)
206 (coefse <- combine_coef_se(mod.amelia.k, messages=FALSE))
207 est.x.mi <- coefse['x','Estimate']
208 est.x.se <- coefse['x','Std.Error']
210 est.z.mi <- coefse['z','Estimate']
211 est.z.se <- coefse['z','Std.Error']
212 amelia_result <- list(Bxy.est.amelia.full = est.x.mi,
213 Bxy.ci.upper.amelia.full = est.x.mi + 1.96 * est.x.se,
214 Bxy.ci.lower.amelia.full = est.x.mi - 1.96 * est.x.se,
215 Bzy.est.amelia.full = est.z.mi,
216 Bzy.ci.upper.amelia.full = est.z.mi + 1.96 * est.z.se,
217 Bzy.ci.lower.amelia.full = est.z.mi - 1.96 * est.z.se
221 result[['error']] <- e}
223 result <- append(result,amelia_result)
230 ## outcome_formula, proxy_formula, and truth_formula are passed to measerr_mle
231 run_simulation <- function(df, result, outcome_formula=y~x+z, proxy_formula=NULL, truth_formula=NULL, confint_method='quad'){
233 accuracy <- df[,mean(w_pred==x)]
234 accuracy.y0 <- df[y<=0,mean(w_pred==x)]
235 accuracy.y1 <- df[y>=0,mean(w_pred==x)]
236 cor.y.xi <- cor(df$x - df$w_pred, df$y)
238 fnr <- df[w_pred==0,mean(w_pred!=x)]
239 fnr.y0 <- df[(w_pred==0) & (y<=0),mean(w_pred!=x)]
240 fnr.y1 <- df[(w_pred==0) & (y>=0),mean(w_pred!=x)]
242 fpr <- df[w_pred==1,mean(w_pred!=x)]
243 fpr.y0 <- df[(w_pred==1) & (y<=0),mean(w_pred!=x)]
244 fpr.y1 <- df[(w_pred==1) & (y>=0),mean(w_pred!=x)]
245 cor.resid.w_pred <- cor(resid(lm(y~x+z,df)),df$w_pred)
247 result <- append(result, list(accuracy=accuracy,
248 accuracy.y0=accuracy.y0,
249 accuracy.y1=accuracy.y1,
257 cor.resid.w_pred=cor.resid.w_pred
260 result <- append(result, list(cor.xz=cor(df$x,df$z)))
261 (model.true <- lm(y ~ x + z, data=df))
262 true.ci.Bxy <- confint(model.true)['x',]
263 true.ci.Bzy <- confint(model.true)['z',]
265 result <- append(result, list(Bxy.est.true=coef(model.true)['x'],
266 Bzy.est.true=coef(model.true)['z'],
267 Bxy.ci.upper.true = true.ci.Bxy[2],
268 Bxy.ci.lower.true = true.ci.Bxy[1],
269 Bzy.ci.upper.true = true.ci.Bzy[2],
270 Bzy.ci.lower.true = true.ci.Bzy[1]))
272 (model.feasible <- lm(y~x.obs+z,data=df))
274 feasible.ci.Bxy <- confint(model.feasible)['x.obs',]
275 result <- append(result, list(Bxy.est.feasible=coef(model.feasible)['x.obs'],
276 Bxy.ci.upper.feasible = feasible.ci.Bxy[2],
277 Bxy.ci.lower.feasible = feasible.ci.Bxy[1]))
279 feasible.ci.Bzy <- confint(model.feasible)['z',]
280 result <- append(result, list(Bzy.est.feasible=coef(model.feasible)['z'],
281 Bzy.ci.upper.feasible = feasible.ci.Bzy[2],
282 Bzy.ci.lower.feasible = feasible.ci.Bzy[1]))
284 (model.naive <- lm(y~w_pred+z, data=df))
286 naive.ci.Bxy <- confint(model.naive)['w_pred',]
287 naive.ci.Bzy <- confint(model.naive)['z',]
289 result <- append(result, list(Bxy.est.naive=coef(model.naive)['w_pred'],
290 Bzy.est.naive=coef(model.naive)['z'],
291 Bxy.ci.upper.naive = naive.ci.Bxy[2],
292 Bxy.ci.lower.naive = naive.ci.Bxy[1],
293 Bzy.ci.upper.naive = naive.ci.Bzy[2],
294 Bzy.ci.lower.naive = naive.ci.Bzy[1]))
296 amelia_result <- list(
297 Bxy.est.amelia.full = NULL,
298 Bxy.ci.upper.amelia.full = NULL,
299 Bxy.ci.lower.amelia.full = NULL,
300 Bzy.est.amelia.full = NULL,
301 Bzy.ci.upper.amelia.full = NULL,
302 Bzy.ci.lower.amelia.full = NULL
306 amelia.out.k <- amelia(df, m=200, p2s=0, idvars=c('x','w'))
307 mod.amelia.k <- zelig(y~x.obs+z, model='ls', data=amelia.out.k$imputations, cite=FALSE)
308 (coefse <- combine_coef_se(mod.amelia.k))
310 est.x.mi <- coefse['x.obs','Estimate']
311 est.x.se <- coefse['x.obs','Std.Error']
312 est.z.mi <- coefse['z','Estimate']
313 est.z.se <- coefse['z','Std.Error']
315 amelia_result <- list(Bxy.est.amelia.full = est.x.mi,
316 Bxy.ci.upper.amelia.full = est.x.mi + 1.96 * est.x.se,
317 Bxy.ci.lower.amelia.full = est.x.mi - 1.96 * est.x.se,
318 Bzy.est.amelia.full = est.z.mi,
319 Bzy.ci.upper.amelia.full = est.z.mi + 1.96 * est.z.se,
320 Bzy.ci.lower.amelia.full = est.z.mi - 1.96 * est.z.se
326 result[['error']] <- e}
330 result <- append(result, amelia_result)
333 mle_result <- list(Bxy.est.mle = NULL,
334 Bxy.ci.upper.mle = NULL,
335 Bxy.ci.lower.mle = NULL,
337 Bzy.ci.upper.mle = NULL,
338 Bzy.ci.lower.mle = NULL)
342 temp.df <- temp.df[,x:=x.obs]
343 if(confint_method=='quad'){
344 mod.caroll.lik <- measerr_mle(temp.df, outcome_formula=outcome_formula, proxy_formula=proxy_formula, truth_formula=truth_formula, method='optim')
345 fischer.info <- solve(mod.caroll.lik$hessian)
346 coef <- mod.caroll.lik$par
347 ci.upper <- coef + sqrt(diag(fischer.info)) * 1.96
348 ci.lower <- coef - sqrt(diag(fischer.info)) * 1.96
349 } else { # confint_method == 'bbmle'
351 mod.caroll.lik <- measerr_mle(temp.df, outcome_formula=outcome_formula, proxy_formula=proxy_formula, truth_formula=truth_formula, method='bbmle')
352 coef <- coef(mod.caroll.lik)
353 ci <- confint(mod.caroll.lik, method='spline')
354 ci.lower <- ci[,'2.5 %']
355 ci.upper <- ci[,'97.5 %']
357 mle_result <- list(Bxy.est.mle = coef['x'],
358 Bxy.ci.upper.mle = ci.upper['x'],
359 Bxy.ci.lower.mle = ci.lower['x'],
360 Bzy.est.mle = coef['z'],
361 Bzy.ci.upper.mle = ci.upper['z'],
362 Bzy.ci.lower.mle = ci.lower['z'])
365 error=function(e) {result[['error']] <- as.character(e)
369 result <- append(result, mle_result)
371 mod.zhang.lik <- zhang.mle.iv(df)
372 coef <- coef(mod.zhang.lik)
373 ci <- confint(mod.zhang.lik,method='quad')
374 result <- append(result,
375 list(Bxy.est.zhang = coef['Bxy'],
376 Bxy.ci.upper.zhang = ci['Bxy','97.5 %'],
377 Bxy.ci.lower.zhang = ci['Bxy','2.5 %'],
378 Bzy.est.zhang = coef['Bzy'],
379 Bzy.ci.upper.zhang = ci['Bzy','97.5 %'],
380 Bzy.ci.lower.zhang = ci['Bzy','2.5 %']))
382 ## What if we can't observe k -- most realistic scenario. We can't include all the ML features in a model.
383 ## amelia.out.nok <- amelia(df, m=200, p2s=0, idvars=c("x","w_pred"), noms=noms)
384 ## mod.amelia.nok <- zelig(y~x.obs+g, model='ls', data=amelia.out.nok$imputations, cite=FALSE)
385 ## (coefse <- combine_coef_se(mod.amelia.nok, messages=FALSE))
387 ## est.x.mi <- coefse['x.obs','Estimate']
388 ## est.x.se <- coefse['x.obs','Std.Error']
389 ## result <- append(result,
390 ## list(Bxy.est.amelia.nok = est.x.mi,
391 ## Bxy.ci.upper.amelia.nok = est.x.mi + 1.96 * est.x.se,
392 ## Bxy.ci.lower.amelia.nok = est.x.mi - 1.96 * est.x.se
395 ## est.g.mi <- coefse['g','Estimate']
396 ## est.g.se <- coefse['g','Std.Error']
398 ## result <- append(result,
399 ## list(Bgy.est.amelia.nok = est.g.mi,
400 ## Bgy.ci.upper.amelia.nok = est.g.mi + 1.96 * est.g.se,
401 ## Bgy.ci.lower.amelia.nok = est.g.mi - 1.96 * est.g.se
405 m <- nrow(df[!is.na(x.obs)])
406 p <- v <- train <- rep(0,N)
410 df <- df[order(x.obs)]
415 # gmm gets pretty close
416 (gmm.res <- predicted_covariates(y, x, z, w, v, train, p, max_iter=100, verbose=TRUE))
418 result <- append(result,
419 list(Bxy.est.gmm = gmm.res$beta[1,1],
420 Bxy.ci.upper.gmm = gmm.res$confint[1,2],
421 Bxy.ci.lower.gmm = gmm.res$confint[1,1],
422 gmm.ER_pval = gmm.res$ER_pval
425 result <- append(result,
426 list(Bzy.est.gmm = gmm.res$beta[2,1],
427 Bzy.ci.upper.gmm = gmm.res$confint[2,2],
428 Bzy.ci.lower.gmm = gmm.res$confint[2,1]))
432 ## mod.calibrated.mle <- mecor(y ~ MeasError(w_pred, reference = x.obs) + z, df, B=400, method='efficient')
433 ## (mod.calibrated.mle)
434 ## (mecor.ci <- summary(mod.calibrated.mle)$c$ci['x.obs',])
435 ## result <- append(result, list(
436 ## Bxy.est.mecor = mecor.ci['Estimate'],
437 ## Bxy.ci.upper.mecor = mecor.ci['UCI'],
438 ## Bxy.ci.lower.mecor = mecor.ci['LCI'])
441 ## (mecor.ci <- summary(mod.calibrated.mle)$c$ci['z',])
443 ## result <- append(result, list(
444 ## Bzy.est.mecor = mecor.ci['Estimate'],
445 ## Bzy.ci.upper.mecor = mecor.ci['UCI'],
446 ## Bzy.ci.lower.mecor = mecor.ci['LCI'])
449 ## error = function(e){
450 ## message("An error occurred:\n",e)
451 ## result$error <- paste0(result$error, '\n', e)
455 ## 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"))