The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

Guide for using threshold2 function

Duc-Khanh To

2026-11-05

Simulated data

Gamma and Log-Normal data

Obtaining the true value for optimal threshold.

YI_logGamma <- function(mu, sigma, start = NULL, method.optim = "L-BFGS-B",
                        maxit = 200, lower = 0, upper = Inf){
  if(is.null(start)) start <- mean(mu)
  YI_logGamma_ff <- function(par, mu, sigma){
    sp <- plnorm(par, meanlog = mu[1], sdlog = sigma[1])
    se <- 1 - pgamma(par, shape = mu[2], rate = sigma[2])
    return(se + sp - 1)
  }
  YI_optim <- optim(par = start, fn = YI_logGamma_ff, mu = mu, sigma = sigma,
                    method = method.optim,
                    control = list(maxit = maxit, fnscale = -1),
                    lower = lower, upper = upper)
  return(YI_optim)
}

CtP_logGamma <- function(mu, sigma, start = NULL, method.optim = "L-BFGS-B",
                         maxit = 200, lower = 0, upper = Inf){
  if(is.null(start)) start <- mean(mu)
  CtP_logGamma_ff <- function(par, mu, sigma){
    sp <- plnorm(par, meanlog = mu[1], sdlog = sigma[1])
    se <- 1 - pgamma(par, shape = mu[2], rate = sigma[2])
    return((1 - sp)^2 + (1 - se)^2)
  }
  CtP_optim <- optim(par = start, fn = CtP_logGamma_ff, mu = mu, sigma = sigma,
                    method = method.optim,
                    control = list(maxit = maxit),
                    lower = lower, upper = upper)
  return(CtP_optim)
}

MA_logGamma <- function(mu, sigma, start = NULL, method.optim = "L-BFGS-B",
                         maxit = 200, lower = 0, upper = Inf){
  if(is.null(start)) start <- mean(mu)
  MA_logGamma_ff <- function(par, mu, sigma){
    sp <- plnorm(par, meanlog = mu[1], sdlog = sigma[1])
    se <- 1 - pgamma(par, shape = mu[2], rate = sigma[2])
    return(log(sp) + log(se))
  }
  MA_optim <- optim(par = start, fn = MA_logGamma_ff, mu = mu, sigma = sigma,
                     method = method.optim,
                     control = list(maxit = maxit, fnscale = -1),
                     lower = lower, upper = upper)
  return(MA_optim)
}
mu_logG_true_2 <- c(0, 3.6)
sigma_logG_true_2 <- c(0.5, 1)
tau_logG_true_2 <- YI_logGamma(mu = mu_logG_true_2,
                               sigma = sigma_logG_true_2)$par
tau_logG_true_2
#> [1] 1.834898
tau_CtP_logG_true_2 <- CtP_logGamma(mu = mu_logG_true_2,
                                    sigma = sigma_logG_true_2)$par
tau_CtP_logG_true_2
#> [1] 1.761806
tau_MA_logG_true_2 <- MA_logGamma(mu = mu_logG_true_2,
                                  sigma = sigma_logG_true_2)$par
tau_MA_logG_true_2
#> [1] 1.811932
sp_logG_true_2 <- plnorm(tau_logG_true_2, meanlog = mu_logG_true_2[1],
                         sdlog = sigma_logG_true_2[1])
se_logG_true_2 <- 1 - pgamma(tau_logG_true_2, shape = mu_logG_true_2[2],
                             rate = sigma_logG_true_2[2])
sp_logG_true_2
#> [1] 0.8876219
se_logG_true_2
#> [1] 0.832717
sp_MA_logG_true_2 <- plnorm(tau_MA_logG_true_2, meanlog = mu_logG_true_2[1],
                             sdlog = sigma_logG_true_2[1])
se_MA_logG_true_2 <- 1 - pgamma(tau_MA_logG_true_2, shape = mu_logG_true_2[2],
                                rate = sigma_logG_true_2[2])
sp_MA_logG_true_2
#> [1] 0.8827383
se_MA_logG_true_2
#> [1] 0.8374735
n1 <- 50
n2 <- 50
X1 <- rlnorm(n1, meanlog = mu_logG_true_2[1], sdlog = sigma_logG_true_2[1])
X2 <- rgamma(n2, shape = mu_logG_true_2[2], rate = sigma_logG_true_2[2])

To obtain the optimal threshold and plot the confidence region for optimal sensitivity and specificity, we perform the following command:

out1 <- threshold2(x = X1, y = X2, method = "YI", kernel_type = "gauss", 
                   bwd_type = "AZZ", plot = TRUE)

out2 <- threshold2(x = X1, y = X2, method = "YI", kernel_type = "gauss", 
                   bwd_type = "CV", plot = FALSE)
plot(out2)

out3 <- threshold2(x = X1, y = X2, method = "MA", kernel_type = "gauss",
                   bwd_type = "CV", plot = TRUE)

Using formula

data_test <- data.frame(biom = c(X1, X2), diag = rep(c("1", "2"), c(n1, n2)))
out4 <- threshold2(formula = biom ~ diag, data = data_test, method = "MA", 
                   kernel_type = "gauss", bwd_type = "CV", plot = TRUE)

out4$ci_tau
#> [1] 1.552498 2.054105

The following codes are for verifying the coverage probability of EL confidence region for sensitivity and specificity at optimal threshold.

term1 <- emplikDTA:::empi_llike_prepare(x = X1, y = X2, 
                                        tau = tau_logG_true_2, 
                                        type_F = "empi")
term2 <- emplikDTA:::empi_llike_2C(term1, spe = sp_logG_true_2,
                                   sen = se_logG_true_2)
term2 < out1$c_alp
#> [1] FALSE

Run the following chunks interactively for the 1,000-replicate coverage study.

out_cp_1 <- sapply(1:1000, function(i, n1, n2){
  X1 <- rlnorm(n1, meanlog = mu_logG_true_2[1], sdlog = sigma_logG_true_2[1])
  X2 <- rgamma(n2, shape = mu_logG_true_2[2], rate = sigma_logG_true_2[2])
  term1 <- emplikDTA:::empi_llike_prepare(x = X1, y = X2, 
                                          tau = tau_logG_true_2, 
                                          type_F = "empi")
  term2 <- emplikDTA:::empi_llike_2C(term1, spe = sp_logG_true_2,
                                     sen = se_logG_true_2)
  out1 <- threshold2(x = X1, y = X2, method = "YI", kernel_type = "gauss", 
                     bwd_type = "AZZ", plot = FALSE, seed = i)
  return(c(term2, out1$c_alp, term2 < out1$c_alp))
}, n1 = 50, n2 = 50)
mean(out_cp_1[3,])

Application to data

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.