tests/testthat/test-grouped-diagnostics.R

test_that("threshold diagnostics are evaluated within fitted groups", {
  group <- rep(c("A", "B"), each = 12)
  expect_warning(
    codes <- freqTLS:::check_tls_data(
      y = c(rep(c(10, 9, 8), 4), rep(c(4, 3, 2), 4)), n = rep(10, 24),
      time = rep(c(30, 60, 120), 8),
      temp = rep(rep(c(30, 34, 38, 42), each = 3), 2), group = group
    ),
    "A.*B"
  )

  expect_true("threshold" %in% codes)
})

test_that("threshold diagnostics respect each group's observed transition", {
  group <- rep(c("B", "A"), each = 4)
  y <- c(9, 7, 3, 1, 9, 7, 3, 1)
  codes <- suppressWarnings(freqTLS:::check_tls_data(
    y = y, n = rep(10, 8), time = rep(c(30, 60, 120, 240), 2),
    temp = rep(c(30, 34, 38, 42), 2), group = group
  ))

  expect_false("threshold" %in% codes)
})

test_that("the ungrouped threshold warning remains available", {
  codes <- suppressWarnings(freqTLS:::check_tls_data(
    y = c(4, 3, 2, 1), n = rep(10, 4), time = c(30, 60, 120, 240),
    temp = c(30, 34, 38, 42)
  ))

  expect_true("threshold" %in% codes)
})

test_that("an all-censored fitted group gets its own adequacy warning", {
  group <- rep(c("A", "B"), each = 12)
  y_group_b <- rep(c(9, 7, 3, 1), each = 3)
  expect_warning(
    codes <- freqTLS:::check_tls_data(
      y = c(rep(0, 12), y_group_b), n = rep(10, 24),
      time = rep(c(30, 60, 120, 240), 6),
      temp = rep(rep(c(30, 34, 38, 42), each = 3), 2), group = group,
      left_censored = c(rep(TRUE, 12), rep(FALSE, 12)),
      censor_limit = c(rep(0.2, 12), rep(0, 12))
    ),
    "left-censored"
  )

  expect_true("all_left_censored_group" %in% codes)
})

test_that("the absolute 50% survival warning leaves relative CTmax to its own diagnostic", {
  d <- expand.grid(
    duration = c(6, 20, 60, 180, 600), temp = c(32, 34, 36, 38, 40)
  )
  low <- 0.6
  up <- 0.95
  d$total <- 1000
  d$survived <- round(d$total * (
    low + (up - low) * plogis(-4 * (log10(d$duration / 60) + (d$temp - 36) / 4))
  ))
  survival <- d$survived / d$total
  expect_true(all(survival > 0.5))
  expect_lt(min(survival), (low + up) / 2)
  expect_gt(max(survival), (low + up) / 2)

  fit <- fit_tls(
    d, y = survived, n = total, time = duration, temp = temp, tref = 60,
    family = "binomial", fixed_low = low, fixed_up = up, quiet = TRUE
  )
  warning_text <- character()
  codes <- withCallingHandlers(check_tls(fit), warning = function(w) {
    warning_text <<- c(warning_text, conditionMessage(w))
    invokeRestart("muffleWarning")
  })
  expect_true("threshold" %in% codes)
  expect_false("ctmax_extrapolated" %in% codes)
  expect_match(paste(warning_text, collapse = "\n"), "absolute 50% survival")
  expect_false(any(grepl("CTmax.*extrapolat|extrapolat.*CTmax", warning_text)))
})

test_that("fitted CTmax is checked against each group's own temperature range", {
  d <- expand.grid(
    duration = c(0.3, 1, 3, 10, 30, 100, 300, 1000, 3000),
    temperature_index = 0:2, group = c("A", "B")
  )
  d$temp <- ifelse(d$group == "A", 25, 33) + 2 * d$temperature_index
  d$total <- 1000
  fit_at <- function(ct_a, ct_b) {
    ct <- ifelse(d$group == "A", ct_a, ct_b)
    d$survived <- round(d$total * (
      0.05 + 0.9 * plogis(-4 * (log10(d$duration / 60) + (d$temp - ct) / 4))
    ))
    fit_tls(
      d, y = survived, n = total, time = duration, temp = temp, tref = 60,
      group = group, family = "binomial", fixed_low = 0.05, fixed_up = 0.95,
      quiet = TRUE
    )
  }

  inside <- fit_at(27, 35)
  expect_false("ctmax_extrapolated" %in% suppressWarnings(check_tls(inside)))

  outside <- fit_at(31, 31)
  warning_text <- character()
  codes <- withCallingHandlers(check_tls(outside), warning = function(w) {
    warning_text <<- c(warning_text, conditionMessage(w))
    invokeRestart("muffleWarning")
  })
  expect_equal(sum(codes == "ctmax_extrapolated"), 1L)
  expect_match(paste(warning_text, collapse = "\n"),
               "outside the assayed temperature range.*A.*B")
})

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.