R/curves.R

Defines functions tls_curve_log_duration tls_curve_mean tls_curve_eta_between tls_curve_eta tls_curve_fraction tls_curve_midpoint_slope_multiplier tls_curve_code tls_validate_curve tls_curve_names

# Internal mean-curve registry. The response family (binomial,
# beta-binomial, or Beta) remains independent of this choice.

tls_curve_names <- function() {
  c("loglogistic", "lognormal", "weibull1", "weibull2")
}

tls_validate_curve <- function(curve) {
  if (!is.character(curve) || length(curve) != 1L || is.na(curve) ||
      !curve %in% tls_curve_names()) {
    cli::cli_abort(c(
      "{.arg curve} must be one of {.val {tls_curve_names()}}.",
      i = "The default {.val loglogistic} curve preserves the matched 4PL benchmark."
    ))
  }
  curve
}

tls_curve_code <- function(curve) {
  match(tls_validate_curve(curve), tls_curve_names()) - 1L
}

tls_curve_midpoint_slope_multiplier <- function(curve) {
  curve <- tls_validate_curve(curve)
  switch(curve,
    loglogistic = 0.25,
    lognormal = 1 / sqrt(2 * pi),
    weibull1 = log(2) / 2,
    weibull2 = log(2) / 2
  )
}

#' Normalized thermal-survival mean curve
#'
#' @param eta Numeric curve coordinate `k * (log10(duration) - mid)`.
#' @param curve Mean-curve family.
#' @return Survival fraction between zero and one.
#' @keywords internal
#' @noRd
tls_curve_fraction <- function(eta, curve = "loglogistic") {
  curve <- tls_validate_curve(curve)
  switch(curve,
    loglogistic = stats::plogis(-eta),
    lognormal = stats::pnorm(-eta),
    weibull1 = exp(-log(2) * exp(pmin(eta, 8))),
    weibull2 = -expm1(-log(2) * exp(pmin(-eta, 8)))
  )
}

#' Inverse normalized thermal-survival mean curve
#'
#' @param q Survival fraction strictly between zero and one.
#' @param curve Mean-curve family.
#' @return `eta` values for the requested fractions.
#' @keywords internal
#' @noRd
tls_curve_eta <- function(q, curve = "loglogistic") {
  curve <- tls_validate_curve(curve)
  if (!is.numeric(q) || anyNA(q) || any(!is.finite(q)) ||
      any(q <= 0 | q >= 1)) {
    cli::cli_abort("The normalized survival fraction must be finite and strictly between 0 and 1.")
  }
  switch(curve,
    loglogistic = -stats::qlogis(q),
    lognormal = -stats::qnorm(q),
    weibull1 = log(-log(q) / log(2)),
    weibull2 = -log(-log1p(-q) / log(2))
  )
}

# Invert survival values against their asymptotes without first forming
# q = (target - low) / (up - low), which can round to 0 or 1 near a boundary.
tls_curve_eta_between <- function(target, low, up, curve = "loglogistic") {
  curve <- tls_validate_curve(curve)
  if (!is.numeric(target) || !is.numeric(low) || !is.numeric(up) ||
      !length(target) || !length(low) || !length(up) ||
      any(!is.finite(target)) || any(!is.finite(low)) || any(!is.finite(up))) {
    cli::cli_abort("Curve inversion requires finite numeric targets and asymptotes.")
  }
  n <- max(length(target), length(low), length(up))
  target <- rep_len(target, n)
  low <- rep_len(low, n)
  up <- rep_len(up, n)
  if (any(low >= up) || any(target <= low | target >= up)) {
    cli::cli_abort("Curve inversion requires low < target < up.")
  }

  log_q <- log(target - low) - log(up - low)
  log_complement <- log(up - target) - log(up - low)
  log_neg_log_probability <- function(log_probability, log_complement) {
    use_complement <- log_complement < log(0.5)
    ans <- numeric(length(log_probability))
    direct <- !use_complement
    ans[direct] <- log(-log_probability[direct])
    idx <- which(use_complement)
    if (length(idx)) {
      lp <- log_complement[idx]
      too_small <- lp < log(.Machine$double.xmin)
      ans[idx[too_small]] <- lp[too_small]
      ok <- !too_small
      ans[idx[ok]] <- log(-log1p(-exp(lp[ok])))
    }
    ans
  }

  switch(curve,
    loglogistic = log(up - target) - log(target - low),
    lognormal = {
      q_small <- log_q <= log_complement
      ans <- numeric(n)
      ans[q_small] <- -stats::qnorm(log_q[q_small], log.p = TRUE)
      ans[!q_small] <- stats::qnorm(log_complement[!q_small], log.p = TRUE)
      ans
    },
    weibull1 = log_neg_log_probability(log_q, log_complement) - log(log(2)),
    weibull2 = -(log_neg_log_probability(log_complement, log_q) - log(log(2)))
  )
}

#' Scale a normalized thermal-survival curve to its asymptotes
#'
#' @param eta Numeric curve coordinate `k * (log10(duration) - mid)`.
#' @param low,up Lower and upper survival asymptotes.
#' @param curve Mean-curve family.
#' @return Survival probabilities.
#' @keywords internal
#' @noRd
tls_curve_mean <- function(eta, low = 0, up = 1, curve = "loglogistic") {
  low + (up - low) * tls_curve_fraction(eta, curve)
}

#' Invert a thermal-survival curve to log10 duration
#'
#' @param target Survival probability.
#' @param low,up Lower and upper survival asymptotes.
#' @param mid Midpoint on the log10 duration scale.
#' @param k Positive steepness parameter.
#' @param curve Mean-curve family.
#' @return Log10 duration at which fitted survival equals `target`.
#' @keywords internal
#' @noRd
tls_curve_log_duration <- function(target, low, up, mid, k,
                                   curve = "loglogistic") {
  if (!is.numeric(target) || !is.numeric(low) || !is.numeric(up) ||
      !is.numeric(mid) || !is.numeric(k) ||
      any(!is.finite(c(target, low, up, mid, k))) ||
      any(k <= 0) || any(target <= low | target >= up)) {
    cli::cli_abort("Curve inversion requires finite values, k > 0, and low < target < up.")
  }
  mid + tls_curve_eta_between(target, low, up, curve) / k
}

Try the freqTLS package in your browser

Any scripts or data that you put into this service are public.

freqTLS documentation built on Dec. 4, 2026, 5:11 p.m.