+
+## 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){
+ nll <- function(B0=0, Bxy=0, Bzy=0, sigma_y=0.1, ppv=0.9, npv=0.9){
+ df.obs <- df[!is.na(x.obs)]
+ df.unobs <- df[is.na(x.obs)]
+
+ ## fpr = 1 - TNR
+ ### Problem: accounting for uncertainty in ppv / npv
+
+ ll.w1x1.obs <- with(df.obs[(w_pred==1)], dbinom(x.obs,size=1,prob=ppv,log=T))
+ ll.w0x0.obs <- with(df.obs[(w_pred==0)], dbinom(1-x.obs,size=1,prob=npv,log=T))
+
+ ## 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)
+ ll <- ll + sum(ll.w1x1.obs) + sum(ll.w0x0.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(lls.x.1, 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,ppv=0.00001, npv=0.00001),
+ upper=list(sigma_y=Inf, B0=Inf, Bxy=Inf, Bzy=Inf, ppv=0.99999,npv=0.99999),method='L-BFGS-B')
+ return(mlefit)
+}
+
+## this is equivalent to the pseudo-liklihood model from Carolla
+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)
+}
+