]> code.communitydata.science - ml_measurement_error_public.git/blobdiff - simulations/pl_methods.R
changes from klone
[ml_measurement_error_public.git] / simulations / pl_methods.R
index f014eec1b8f00479417ad9181d6c6bc5127a6af0..2099f1a2ad598524af958bf860ffb91e617fe24e 100644 (file)
@@ -51,19 +51,20 @@ zhang.mle.iv <- function(df){
     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))
@@ -75,10 +76,11 @@ zhang.mle.iv <- function(df){
     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)
 }

Community Data Science Collective || Want to submit a patch?