X-Git-Url: https://code.communitydata.science/ml_measurement_error_public.git/blobdiff_plain/bb6f5e4731c603b336afb1a900bc9083d1b175bf..refs/heads/main:/simulations/pl_methods.R diff --git a/simulations/pl_methods.R b/simulations/pl_methods.R index f014eec..2099f1a 100644 --- a/simulations/pl_methods.R +++ b/simulations/pl_methods.R @@ -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) }