tests/testthat/test-extract-tdt.R

# extract_tdt() + accessors — the bayesTLS-twin comprehensive extractor
# (parametric-bootstrap replicates as the per-draw analogue). Small nboot for
# speed; these check structure, the column contract, and basic recovery.

std_sim <- function(seed = 1, ...) {
  standardize_data(simulate_tls(family = "binomial", seed = seed, ...),
                   temp = "temp", duration = "duration",
                   n_total = "total", n_surv = "survived", duration_unit = "minutes")
}

test_that("extract_tdt returns nested z/CTmax with the bayesTLS column contract", {
  f <- fit_4pl(std_sim(1, CTmax = 36, z = 4), t_ref = 1, family = "binomial",
               quiet = TRUE)
  et <- suppressWarnings(extract_tdt(f, nboot = 60, seed = 1))
  expect_s3_class(et, "freq_tdt")
  expect_named(et, c("z", "CTmax", "T_crit", "meta"))
  expect_null(et$T_crit)
  expect_setequal(names(get_z_summary(et)), c("z_median", "z_lower", "z_upper"))
  expect_setequal(names(get_ctmax_summary(et)),
                  c("temp_median", "temp_lower", "temp_upper"))
  expect_true("z" %in% names(get_z_draws(et)))
  expect_true("CTmax" %in% names(get_ctmax_draws(et)))   # temp -> CTmax rename
  s <- get_ctmax_summary(et)
  expect_equal(s$temp_median, 36, tolerance = 0.3)
  expect_true(s$temp_lower < s$temp_median && s$temp_median < s$temp_upper)
  expect_equal(nrow(get_z_draws(et)), et$meta$nboot)
  expect_identical(et$meta$tref, 1)
  expect_identical(et$meta$duration_unit, "minutes")
  expect_identical(et$meta$input_duration_unit, "minutes")
})

test_that("extract_tdt groups and adds T_crit (below CTmax) when lethal", {
  d <- simulate_tls(family = "binomial", group = c("A", "B"),
                    CTmax = c(34, 38), z = c(3, 5), seed = 3)
  s <- standardize_data(d, temp = "temp", duration = "duration",
                        n_total = "total", n_surv = "survived", duration_unit = "minutes")
  f <- suppressWarnings(fit_4pl(s, ctmax = ~ 0 + group, z = ~ 0 + group,
                                t_ref = 1, family = "binomial", quiet = TRUE))
  et <- suppressWarnings(extract_tdt(f, lethal = TRUE, nboot = 60, seed = 1))
  zs <- get_z_summary(et)
  expect_true("group" %in% names(zs))
  expect_setequal(unique(zs$group), c("A", "B"))
  expect_equal(nrow(zs), 2L)
  expect_true(all(get_tcrit_summary(et)$temp_median <
                  get_ctmax_summary(et)$temp_median))   # T_crit < CTmax
  expect_true("T_crit" %in% names(get_tcrit_draws(et))) # temp -> T_crit rename
})

test_that("extract_tdt rejects contrast-coded interactions as group means", {
  d <- simulate_tls(family = "binomial", group = c("A", "B"), seed = 72)
  d$site <- factor(rep(c("north", "south"), length.out = nrow(d)))
  std <- standardize_data(d, temp = "temp", duration = "duration",
                          n_total = "total", n_surv = "survived",
                          duration_unit = "minutes")
  fit <- suppressWarnings(fit_4pl(
    std, ctmax = ~ group * site, z = ~ group * site,
    family = "binomial", t_ref = 1, quiet = TRUE
  ))
  expect_error(extract_tdt(fit, nboot = 20, seed = 1),
               "matching represented cell-mean designs")
})

test_that("extract_tdt T_crit draws use the one-hour absolute target", {
  d <- simulate_tls(family = "binomial", CTmax = 36, z = 4, seed = 82)
  std <- standardize_data(d, temp = "temp", duration = "duration",
                          n_total = "total", n_surv = "survived",
                          duration_unit = "minutes")
  fit <- suppressWarnings(fit_4pl(std, family = "binomial", t_ref = 240,
                                  quiet = TRUE))
  out <- suppressWarnings(extract_tdt(fit, target_surv = 0.5, lethal = TRUE,
                                     nboot = 40, seed = 83))
  boot <- tls_bootstrap_replicates(fit$fit, nboot = 40, seed = 83)
  reps <- boot$replicates[boot$converged, , drop = FALSE]
  zdraw <- reps[, "z"]
  cdraw <- reps[, "CTmax"]
  q <- stats::qlogis((0.5 - reps[, "low"]) / (reps[, "up"] - reps[, "low"]))
  expected <- cdraw - zdraw * log10(60 / fit$fit$tref) -
    zdraw * q / reps[, "k"] + zdraw * (out$T_crit$draws$log10_rate - 2)
  expect_equal(out$T_crit$draws$temp, expected, tolerance = 1e-8)
})

test_that("relative extraction and T_crit do not require shared shape coefficients", {
  d <- simulate_tls(family = "binomial", temps = c(34, 36, 38),
                    times = c(1, 2, 4, 8), CTmax = 36, z = 4, seed = 84)
  std <- standardize_data(d, temp = "temp", duration = "duration",
                          n_total = "total", n_surv = "survived",
                          duration_unit = "minutes")
  fit <- suppressWarnings(fit_4pl(std, k = ~ temp_c, t_ref = 60,
                                  family = "binomial", quiet = TRUE))
  expect_true(is.finite(suppressMessages(derive_tcrit(fit, rate = 1))))
  out <- suppressWarnings(extract_tdt(fit, lethal = TRUE, nboot = 40, seed = 85))
  expect_true(all(is.finite(out$T_crit$draws$temp)))
})

test_that("absolute extraction rejects shapes grouped independently of CTmax", {
  d <- simulate_tls(family = "binomial", temps = c(34, 36, 38),
                    times = c(0.5, 1, 2, 4), CTmax = 36, z = 4,
                    low = 0.02, up = 0.98, seed = 122)
  d$group <- factor(rep(c("A", "B"), length.out = nrow(d)))
  d$site <- factor(ifelse(d$group == "A", "B", "A"), levels = c("A", "B"))
  fit <- suppressWarnings(fit_tls(
    tls_bf(survived | trials(total) ~ time(duration) + temp(temp),
           CTmax ~ group, log_z ~ group, low ~ site),
    data = d, family = "binomial", tref = 1
  ))
  expect_error(
    extract_tdt(fit, target_surv = 0.6, lethal = TRUE, nboot = 6, seed = 123),
    "shared shapes or shapes matched to the CTmax cells"
  )
})

test_that("absolute extraction accepts equivalent single-factor cell means", {
  d <- simulate_tls(family = "binomial", group = c("A", "B"),
                    CTmax = c(35, 38), z = c(4, 5), seed = 124)
  d$population <- d$group
  std <- standardize_data(d, temp = "temp", duration = "duration",
                          n_total = "total", n_surv = "survived",
                          duration_unit = "minutes")
  fit <- suppressWarnings(fit_4pl(
    std, by = "population", ctmax = ~ 0 + population,
    z = ~ 0 + population, low = ~ population, t_ref = 1,
    family = "binomial", quiet = TRUE
  ))
  out <- suppressWarnings(extract_tdt(fit, target_surv = 0.6,
                                      nboot = 8, seed = 125))
  expect_setequal(unique(out$CTmax$summary$population), c("A", "B"))
  expect_true(all(is.finite(out$CTmax$summary$temp_median)))
})

test_that("extract_tdt seed controls rate draws without changing caller RNG", {
  f <- suppressWarnings(fit_4pl(std_sim(seed = 86), t_ref = 240,
                                family = "binomial", quiet = TRUE))
  set.seed(17)
  before <- .Random.seed
  x <- suppressWarnings(extract_tdt(f, lethal = TRUE, nboot = 30, seed = 87))
  expect_identical(.Random.seed, before)
  set.seed(91)
  y <- suppressWarnings(extract_tdt(f, lethal = TRUE, nboot = 30, seed = 87))
  expect_equal(x$T_crit$draws$temp, y$T_crit$draws$temp, tolerance = 0)
})

test_that("get_tcrit errors without lethal; absolute ~= relative for symmetric data", {
  f <- fit_4pl(std_sim(2, CTmax = 36, z = 4, low = 0.02, up = 0.98), t_ref = 1,
               family = "binomial", quiet = TRUE)
  et <- suppressWarnings(extract_tdt(f, nboot = 40, seed = 1))
  expect_error(get_tcrit_summary(et), "lethal")
  eta <- suppressWarnings(extract_tdt(f, target_surv = "absolute",
                                      nboot = 40, seed = 1))
  expect_equal(get_ctmax_summary(eta)$temp_median,
               get_ctmax_summary(et)$temp_median, tolerance = 0.2)
  expect_match(eta$meta$target_surv, "p=0.500")
  expect_identical(eta$meta$z_definition,
                   "relative-midpoint thermal sensitivity")
})

test_that("absolute extraction rejects a target outside the fitted asymptotes", {
  f <- fit_4pl(std_sim(2, CTmax = 36, z = 4), t_ref = 1,
               family = "binomial", quiet = TRUE)
  expect_error(
    suppressWarnings(extract_tdt(f, target_surv = 0.999, nboot = 2, seed = 1)),
    "not attainable in the fitted curve"
  )
})

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.