tests/testthat/test-alternative-curves.R

test_that("alternative mean curves share one descending midpoint contract", {
  curves <- c("loglogistic", "lognormal", "weibull1", "weibull2")
  eta <- seq(-8, 8, length.out = 101)

  for (curve in curves) {
    q <- tls_curve_fraction(eta, curve)
    expect_equal(q[51], 0.5, tolerance = 1e-12, info = curve)
    expect_true(all(diff(q) <= 0), info = curve)
    expect_true(any(diff(q) < 0), info = curve)
    expect_true(q[1] > 0.999, info = curve)
    expect_true(q[length(q)] < 0.001, info = curve)
  }
})

test_that("each curve inverse returns the requested survival fraction", {
  curves <- c("loglogistic", "lognormal", "weibull1", "weibull2")
  q <- c(0.02, 0.2, 0.5, 0.8, 0.98)

  for (curve in curves) {
    eta <- tls_curve_eta(q, curve)
    expect_equal(tls_curve_fraction(eta, curve), q, tolerance = 1e-10,
                 info = curve)
  }
})

test_that("curve inverses stay finite when normalized fractions round to one", {
  low <- 0.125 + 2^-54
  up <- 0.875 + 2^-53
  target <- 0.875
  expect_identical((target - low) / (up - low), 1)
  tail_fraction <- (up - target) / (up - low)
  expected <- c(
    loglogistic = log(up - target) - log(target - low),
    lognormal = stats::qnorm(tail_fraction),
    weibull1 = log(-log1p(-tail_fraction) / log(2)),
    weibull2 = -log(-log(tail_fraction) / log(2))
  )
  for (curve in names(expected)) {
    eta <- tls_curve_eta_between(target, low, up, curve)
    expect_true(is.finite(eta), info = curve)
    expect_equal(eta, expected[[curve]], tolerance = 1e-12, info = curve)
  }
})

test_that("curve kernels match their midpoint equations", {
  eta <- c(-1.1, -0.3, 0, 0.4, 1.2)
  expected <- list(
    loglogistic = stats::plogis(-eta),
    lognormal = stats::pnorm(-eta),
    weibull1 = exp(-log(2) * exp(eta)),
    weibull2 = -expm1(-log(2) * exp(-eta))
  )

  for (curve in names(expected)) {
    expect_equal(tls_curve_fraction(eta, curve), expected[[curve]],
                 tolerance = 1e-14, info = curve)
  }
})

test_that("curve survival scales the normalized kernel between asymptotes", {
  eta <- c(-2, -0.5, 0, 0.7, 2)
  low <- 0.12
  up <- 0.86

  for (curve in c("loglogistic", "lognormal", "weibull1", "weibull2")) {
    p <- tls_curve_mean(eta, low = low, up = up, curve = curve)
    expect_equal(p, low + (up - low) * tls_curve_fraction(eta, curve),
                 tolerance = 1e-14, info = curve)
    expect_equal(p[3], (low + up) / 2, tolerance = 1e-14, info = curve)
  }
})

test_that("each fitted curve recovers the midpoint model and downstream transforms", {
  curves <- c("loglogistic", "lognormal", "weibull1", "weibull2")
  fit_curve <- function(dat, curve) {
    if (curve == "lognormal") {
      fit_lognormal(dat, y = survived, n = total, time = duration, temp = temp,
                    family = "binomial", tref = 60, quiet = TRUE)
    } else if (curve == "weibull2") {
      fit_weibull(dat, type = 2, y = survived, n = total, time = duration,
                  temp = temp, family = "binomial", tref = 60, quiet = TRUE)
    } else {
      fit_tls(dat, y = survived, n = total, time = duration, temp = temp,
              family = "binomial", tref = 60, quiet = TRUE, curve = curve)
    }
  }

  for (curve in curves) {
    dat <- simulate_tls(
      family = "binomial", curve = curve,
      temps = seq(32, 40, by = 2), times = c(15, 30, 60, 120, 240),
      reps = 4, n = 80, low = 0.04, up = 0.96, k = 5,
      CTmax = 36, z = 4, tref = 60, seed = 8101
    )
    fit <- fit_curve(dat, curve)
    expect_identical(fit$curve, curve, info = curve)
    expect_identical(fit$tmb_inputs$data$curve_code,
                     match(curve, curves) - 1L, info = curve)
    expect_identical(fit$convergence$code, 0L, info = curve)
    expect_true(fit$convergence$pdHess, info = curve)

    est <- fit$estimates
    get_est <- function(nm) est$estimate[match(nm, est$parameter)]
    expect_true(abs(get_est("CTmax") - 36) < 1.0, info = curve)
    expect_true(abs(get_est("z") - 4) < 1.5, info = curve)
    expect_true(abs(get_est("low") - 0.04) < 0.12, info = curve)
    expect_true(abs(get_est("up") - 0.96) < 0.12, info = curve)
    expect_true(abs(get_est("k") - 5) / 5 < 0.5, info = curve)

    target <- (get_est("low") + get_est("up")) / 2
    ct <- get_est("CTmax")
    expect_equal(predict(fit, data.frame(temp = ct, duration = 60)),
                 target, tolerance = 1e-10, info = curve)
    expect_equal(derive_lt(fit, p = target, temp = ct), 60,
                 tolerance = 1e-10, info = curve)
    expect_equal(derive_ctmax(fit, surv = target, duration = 60), ct,
                 tolerance = 1e-10, info = curve)
    expect_equal(derive_tcrit(fit, rate = 100, target_surv = target), ct,
                 tolerance = 1e-10, info = curve)

    trace <- data.frame(time = c(0, 60), temp = c(ct, ct))
    injury <- predict_heat_injury(fit, trace)
    expect_equal(tail(injury$dose, 1), 1, tolerance = 1e-10, info = curve)
    expect_equal(tail(injury$survival, 1), target,
                 tolerance = 1e-10, info = curve)
  }
})

test_that("Weibull fitter validates its family selector", {
  expect_error(fit_weibull(type = 0), "type.*1.*2")
  expect_error(fit_weibull(type = 3), "type.*1.*2")
})

test_that("curve-specific fitters reject a conflicting curve argument clearly", {
  expect_error(fit_lognormal(curve = "weibull1"), "curve.*fixed to.*lognormal")
  expect_error(fit_weibull(curve = "lognormal"), "curve.*fixed by.*type")
})

test_that("bootstrap survival surfaces use the selected mean curve", {
  dat <- simulate_tls(
    family = "binomial", curve = "lognormal",
    temps = seq(32, 40, by = 2), times = c(15, 30, 60, 120, 240),
    reps = 4, n = 80, low = 0.04, up = 0.96, k = 5,
    CTmax = 36, z = 4, tref = 60, seed = 8210
  )
  fit <- fit_lognormal(
    dat, y = survived, n = total, time = duration, temp = temp,
    family = "binomial", tref = 60, quiet = TRUE
  )
  draws <- tls_bootstrap_replicates(fit, nboot = 6, seed = 8211)
  draws <- draws$replicates[draws$converged, , drop = FALSE]
  expect_gte(nrow(draws), 2L)

  temperature <- 37
  duration <- 240
  mid <- log10(fit$tref) -
    (temperature - draws[, "CTmax"]) / draws[, "z"]
  expected <- stats::median(tls_curve_mean(
    draws[, "k"] * (log10(duration) - mid),
    draws[, "low"], draws[, "up"], curve = "lognormal"
  ))
  surface <- predict_survival_curves(
    fit, temps = temperature, durations = duration, nboot = 6,
    seed = 8211
  )
  expect_equal(surface$summary$survival_median, expected, tolerance = 1e-12)
})

test_that("all mean curves recover parameters across Beta likelihoods", {
  curves <- c("loglogistic", "lognormal", "weibull1", "weibull2")
  families <- c("beta_binomial", "beta")
  for (i in seq_along(curves)) {
    curve <- curves[[i]]
    for (j in seq_along(families)) {
      family <- families[[j]]
      dat <- simulate_tls(
        family = family, curve = curve, temps = seq(32, 40, by = 2),
        times = c(15, 30, 60, 120, 240), reps = 4, n = 80,
        low = 0.08, up = 0.92, k = 4, CTmax = 36, z = 4, phi = 20,
        tref = 60, seed = 8220 + (i - 1L) * length(families) + j
      )
      fit <- if (family == "beta") {
        fit_tls(dat, y = prop, time = duration, temp = temp,
                family = family, tref = 60, curve = curve, quiet = TRUE)
      } else {
        fit_tls(dat, y = survived, n = total, time = duration, temp = temp,
                family = family, tref = 60, curve = curve, quiet = TRUE)
      }
      expect_identical(fit$curve, curve, info = paste(curve, family))
      expect_identical(fit$family$family, family, info = paste(curve, family))
      expect_identical(fit$convergence$code, 0L, info = paste(curve, family))
      expect_true(fit$convergence$pdHess, info = paste(curve, family))

      est <- stats::setNames(fit$estimates$estimate, fit$estimates$parameter)
      expect_true(abs(est[["CTmax"]] - 36) < 1.0,
                  info = paste(curve, family, "CTmax"))
      expect_true(abs(est[["z"]] - 4) < 1.5, info = paste(curve, family, "z"))
      expect_true(abs(est[["low"]] - 0.08) < 0.15,
                  info = paste(curve, family, "low"))
      expect_true(abs(est[["up"]] - 0.92) < 0.15,
                  info = paste(curve, family, "up"))
      expect_true(abs(est[["k"]] - 4) / 4 < 0.5,
                  info = paste(curve, family, "k"))
      expect_true(abs(log(est[["phi"]] / 20)) < 1,
                  info = paste(curve, family, "phi"))
    }
  }
})

test_that("extract_tdt inverts an absolute target for an alternative curve", {
  dat <- simulate_tls(
    family = "binomial", curve = "weibull1",
    temps = seq(32, 40, by = 2), times = c(15, 30, 60, 120, 240),
    reps = 4, n = 80, low = 0.04, up = 0.96, k = 5,
    CTmax = 36, z = 4, tref = 60, seed = 8230
  )
  fit <- fit_tls(
    dat, y = survived, n = total, time = duration, temp = temp,
    family = "binomial", tref = 60, curve = "weibull1", quiet = TRUE
  )
  low <- fit$estimates$estimate[match("low", fit$estimates$parameter)]
  up <- fit$estimates$estimate[match("up", fit$estimates$parameter)]
  k <- fit$estimates$estimate[match("k", fit$estimates$parameter)]
  z <- fit$estimates$estimate[match("z", fit$estimates$parameter)]
  ctmax <- fit$estimates$estimate[match("CTmax", fit$estimates$parameter)]
  target <- 0.4
  expected <- ctmax + z * tls_curve_eta((target - low) / (up - low), "weibull1") / k

  extracted <- suppressWarnings(extract_tdt(
    fit, target_surv = target, lethal = TRUE, TC_rate_range = c(100, 100),
    nboot = 4, seed = 8231
  ))
  expect_equal(get_ctmax_summary(extracted)$temp_median, expected,
               tolerance = 1e-10)
  expect_equal(get_tcrit_summary(extracted)$temp_median, expected,
               tolerance = 1e-10)
})

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.