+ 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)