+ pw <- vector(mode='numeric',length=nrow(df))
+ dfw1 <- df[w_pred==1]
+ dfw0 <- df[w_pred==0]
+ pw[df$w_pred==1] <- plogis(B0 + Bxy * dfw1$x + Bzy * dfw1$z, log=T)
+ pw[df$w_pred==0] <- plogis(B0 + Bxy * dfw0$x + Bzy * dfw0$z, lower.tail=FALSE, log=T)
+
+ probs <- colLogSumExps(rbind(log(1 - p0.est), log(p1.est + p0.est - 1) + pw))
+ return(-1*sum(probs))
+ }
+
+ mlefit <- mle2(minuslogl = nll, start = list(B0=0.0, Bxy=0.0, Bzy=0.0), control=list(maxit=1e6),method='L-BFGS-B')
+ return(mlefit)
+
+}
+
+
+## model from Zhang's arxiv paper, with predictions for y
+## Zhang got this model from Hausman 1998
+### I think this is actually eqivalent to the pseudo.mle method
+zhang.mle.iv <- function(df){
+ df.obs <- df[!is.na(x.obs)]
+ df.unobs <- df[is.na(x.obs)]
+
+ tn <- df.obs[(w_pred == 0) & (x.obs == w_pred),.N]
+ pn <- df.obs[(w_pred==0), .N]
+ npv <- tn / pn
+
+ tp <- df.obs[(w_pred==1) & (x.obs == w_pred),.N]
+ pp <- df.obs[(w_pred==1),.N]
+ ppv <- tp / pp
+
+ nll <- function(B0=0, Bxy=0, Bzy=0, sigma_y=0.1){
+
+ ## fpr = 1 - TNR
+ ### Problem: accounting for uncertainty in ppv / npv
+
+ ## fnr = 1 - TPR
+ ll.y.obs <- with(df.obs, dnorm(y, B0 + Bxy * x + Bzy * z, sd=sigma_y,log=T))
+ ll <- sum(ll.y.obs)
+
+ # unobserved case; integrate out x
+ ll.x.1 <- with(df.unobs, dnorm(y, B0 + Bxy + Bzy * z, sd = sigma_y, log=T))
+ ll.x.0 <- with(df.unobs, dnorm(y, B0 + Bzy * z, sd = sigma_y,log=T))
+
+ ## case x == 1
+ lls.x.1 <- colLogSumExps(rbind(log(ppv) + ll.x.1, log(1-ppv) + ll.x.0))
+
+ ## case x == 0
+ lls.x.0 <- colLogSumExps(rbind(log(1-npv) + ll.x.1, log(npv) + ll.x.0))
+
+ lls <- colLogSumExps(rbind(df.unobs$w_pred * lls.x.1, (1-df.unobs$w_pred) * lls.x.0))
+ ll <- ll + sum(lls)
+ return(-ll)
+ }
+ mlefit <- mle2(minuslogl = nll, control=list(maxit=1e6), lower=list(sigma_y=0.0001, B0=-Inf, Bxy=-Inf, Bzy=-Inf),
+ upper=list(sigma_y=Inf, B0=Inf, Bxy=Inf, Bzy=Inf),method='L-BFGS-B')
+ return(mlefit)
+}
+
+## this is equivalent to the pseudo-liklihood model from Caroll
+## zhang.mle.dv <- function(df){
+
+## nll <- function(B0=0, Bxy=0, Bzy=0, ppv=0.9, npv=0.9){
+## df.obs <- df[!is.na(y.obs)]
+
+## ## fpr = 1 - TNR
+## ll.w0y0 <- with(df.obs[y.obs==0],dbinom(1-w_pred,1,npv,log=TRUE))
+## ll.w1y1 <- with(df.obs[y.obs==1],dbinom(w_pred,1,ppv,log=TRUE))
+
+## # observed case
+## ll.y.obs <- vector(mode='numeric', length=nrow(df.obs))
+## ll.y.obs[df.obs$y.obs==1] <- with(df.obs[y.obs==1], plogis(B0 + Bxy * x + Bzy * z,log=T))
+## 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))
+
+## ll <- sum(ll.y.obs) + sum(ll.w0y0) + sum(ll.w1y1)
+
+## # unobserved case; integrate out y
+## ## case y = 1
+## ll.y.1 <- vector(mode='numeric', length=nrow(df))
+## pi.y.1 <- with(df,plogis(B0 + Bxy * x + Bzy*z, log=T))
+## ## 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)
+## lls.y.1 <- colLogSumExps(rbind(log(ppv) + pi.y.1, log(1-ppv) + pi.y.1))
+
+## ## case y = 0
+## ll.y.0 <- vector(mode='numeric', length=nrow(df))
+## pi.y.0 <- with(df,plogis(B0 + Bxy * x + Bzy*z, log=T,lower.tail=FALSE))
+
+## ## 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)
+## lls.y.0 <- colLogSumExps(rbind(log(npv) + pi.y.0, log(1-npv) + pi.y.0))
+
+## lls <- colLogSumExps(rbind(lls.y.1, lls.y.0))
+## ll <- ll + sum(lls)
+## return(-ll)
+## }
+## 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),
+## upper=list(B0=Inf, Bxy=Inf, Bzy=Inf,ppv=0.999,npv=0.999))
+## return(mlefit)
+## }
+
+zhang.mle.dv <- function(df){
+ df.obs <- df[!is.na(y.obs)]
+ df.unobs <- df[is.na(y.obs)]
+
+ fp <- df.obs[(w_pred==1) & (y.obs != w_pred),.N]
+ p <- df.obs[(w_pred==1),.N]
+ fpr <- fp / p
+ fn <- df.obs[(w_pred==0) & (y.obs != w_pred), .N]
+ n <- df.obs[(w_pred==0),.N]
+ fnr <- fn / n
+
+ nll <- function(B0=0, Bxy=0, Bzy=0){
+
+
+ ## observed case
+ ll.y.obs <- vector(mode='numeric', length=nrow(df.obs))
+ ll.y.obs[df.obs$y.obs==1] <- with(df.obs[y.obs==1], plogis(B0 + Bxy * x + Bzy * z,log=T))
+ 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))
+
+ ll <- sum(ll.y.obs)
+
+ pi.y.1 <- with(df,plogis(B0 + Bxy * x + Bzy*z, log=T))
+ pi.y.0 <- with(df,plogis(B0 + Bxy * x + Bzy*z, log=T,lower.tail=FALSE))
+
+ lls <- with(df.unobs, colLogSumExps(rbind(w_pred * colLogSumExps(rbind(log(fpr), log(1 - fnr - fpr)+pi.y.1)),
+ (1-w_pred) * colLogSumExps(rbind(log(1-fpr), log(1 - fnr - fpr)+pi.y.0)))))
+
+ ll <- ll + sum(lls)
+ return(-ll)
+ }
+ mlefit <- mle2(minuslogl = nll, control=list(maxit=1e6),method='L-BFGS-B',lower=c(B0=-Inf, Bxy=-Inf, Bzy=-Inf),
+ upper=c(B0=Inf, Bxy=Inf, Bzy=Inf))
+ return(mlefit)
+}
+
+## This uses the likelihood approach from Carroll page 353.
+## assumes that we have a good measurement error model
+my.mle <- function(df){
+
+ ## liklihood for observed responses
+ nll <- function(B0, Bxy, Bzy, gamma0, gamma_y, gamma_z, gamma_yz){
+ df.obs <- df[!is.na(y.obs)]
+ yobs0 <- df.obs$y==0
+ yobs1 <- df.obs$y==1
+ p.y.obs <- vector(mode='numeric', length=nrow(df.obs))
+
+ p.y.obs[yobs1] <- plogis(B0 + Bxy * df.obs[yobs1]$x + Bzy*df.obs[yobs1]$z,log=T)
+ p.y.obs[yobs0] <- plogis(B0 + Bxy * df.obs[yobs0]$x + Bzy*df.obs[yobs0]$z,lower.tail=FALSE,log=T)
+
+ wobs0 <- df.obs$w_pred==0
+ wobs1 <- df.obs$w_pred==1
+ p.w.obs <- vector(mode='numeric', length=nrow(df.obs))
+
+ 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)
+ 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)
+
+ p.obs <- p.w.obs + p.y.obs
+
+ df.unobs <- df[is.na(y.obs)]
+
+ p.unobs.0 <- vector(mode='numeric',length=nrow(df.unobs))
+ p.unobs.1 <- vector(mode='numeric',length=nrow(df.unobs))
+
+ wunobs.0 <- df.unobs$w_pred == 0
+ wunobs.1 <- df.unobs$w_pred == 1
+
+ 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)
+
+ 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)
+
+ 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)
+
+ 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)
+
+ p.unobs <- colLogSumExps(rbind(p.unobs.1, p.unobs.0))
+
+ p <- c(p.obs, p.unobs)
+
+ return(-1*(sum(p)))
+ }
+
+ 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')
+
+ return(mlefit)
+}
+
+run_simulation_depvar <- function(df, result, outcome_formula=y~x+z, proxy_formula=w_pred~y){
+
+ accuracy <- df[,mean(w_pred==y)]