fn <- df.obs[(w_pred==0) & (x.obs==1), .N]
npv <- tn / (tn + fn)
+
tp <- df.obs[(w_pred==1) & (x.obs == w_pred),.N]
fp <- df.obs[(w_pred==1) & (x.obs == 0),.N]
ppv <- tp / (tp + fp)
- nll <- function(B0=0, Bxy=0, Bzy=0, sigma_y=0.1){
+ nll <- function(B0=0, Bxy=0, Bzy=0, sigma_y=9){
## 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))
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),
+ mlefit <- mle2(minuslogl = nll, control=list(maxit=1e6), lower=list(sigma_y=0.00001, B0=-Inf, Bxy=-Inf, Bzy=-Inf),
upper=list(sigma_y=Inf, B0=Inf, Bxy=Inf, Bzy=Inf),method='L-BFGS-B')
return(mlefit)
}