tests/testthat/test-tls-summary-routing.R

tls_summary_fit <- function(parameter, estimate, cell = NULL) {
  if (is.null(cell)) {
    design <- matrix(1, nrow = 2L)
    group_levels <- NULL
  } else {
    cells <- unique(cell)
    design <- diag(length(cells))
    colnames(design) <- cells
    group_levels <- sub("^group", "", cells)
  }
  structure(
    list(estimates = tibble::tibble(parameter = parameter, estimate = estimate,
                                    group = cell %||% NA_character_),
         convergence = list(pdHess = TRUE),
         group_levels = group_levels, tref = 60,
         tmb_inputs = list(data = list(X_CT = design, X_logz = design))),
    class = "profile_tls"
  )
}

tls_summary_tdt <- function(target_surv) {
  list(
    meta = list(target_surv = target_surv,
                z_definition = "relative-midpoint thermal sensitivity"),
    z = list(summary = data.frame(z_median = 4, z_lower = 3, z_upper = 5),
             draws = data.frame(.draw = 1:2, z = c(3, 5))),
    CTmax = list(summary = data.frame(temp_median = 36, temp_lower = 35,
                                      temp_upper = 37),
                 draws = data.frame(.draw = 1:2, temp = c(35, 37)))
  )
}

test_that("tls keeps each group interval and status when one profile bootstraps", {
  fit <- tls_summary_fit(c("CTmax:groupA", "CTmax:groupB", "z:groupA", "z:groupB"),
                         c(36, 38, 4, 5), rep(c("groupA", "groupB"), 2L))
  ci <- tibble::tibble(
    parameter = c("z:groupB", "CTmax:groupB", "CTmax:groupA", "z:groupA"),
    conf.low = c(4.2, 37, NA_real_, 3), conf.high = c(6.3, 39, NA_real_, 6),
    method = c("profile", "bootstrap", "profile", "profile"),
    conf.status = c("ok", "bootstrap", "invalid_center", "ok")
  )
  bootstrap_args <- NULL
  extra_bootstrap <- FALSE
  testthat::local_mocked_bindings(
    confint = function(object, parm, level, method, nboot = NULL, boot_seed = NULL) {
      bootstrap_args <<- list(nboot = nboot, seed = boot_seed)
      ci
    },
    extract_tdt = function(...) {
      extra_bootstrap <<- TRUE
      tls_summary_tdt("relative")
    },
    .package = "freqTLS"
  )

  out <- tls(fit, nboot = 31, seed = 12)
  expect_identical(out$summary$group, c("A", "B", "A", "B"))
  expect_identical(out$summary$quantity, c("CTmax", "CTmax", "z", "z"))
  expect_identical(out$summary$median, c(36, 38, 4, 5))
  expect_identical(out$summary$lower, c(NA_real_, 37, 3, 4.2))
  expect_identical(out$summary$upper, c(NA_real_, 39, 6, 6.3))
  expect_identical(out$meta$method, "mixed")
  expect_identical(out$meta$methods[["CTmax:groupB"]], "bootstrap")
  expect_identical(out$meta$conf.status[["CTmax:groupA"]], "invalid_center")
  expect_identical(out$meta$z_definition, "relative-midpoint thermal sensitivity")
  expect_null(out$draws)
  expect_false(extra_bootstrap)
  expect_identical(bootstrap_args, list(nboot = 31, seed = 12))
  printed <- paste(capture.output(print(out)), collapse = "\n")
  expect_match(printed, "CTmax:groupA", fixed = TRUE)
  expect_match(printed, "invalid_center", fixed = TRUE)
  expect_match(printed, "CTmax:groupB", fixed = TRUE)
  expect_match(printed, "bootstrap", fixed = TRUE)
})

test_that("tls labels absolute CTmax alongside relative-midpoint z", {
  fit <- tls_summary_fit(c("CTmax", "z"), c(36, 4))
  z_definition <- "relative-midpoint thermal sensitivity"
  testthat::local_mocked_bindings(
    extract_tdt = function(...) tls_summary_tdt("p=0.500"),
    .package = "freqTLS"
  )

  out <- tls(fit, target_surv = "absolute")
  expect_identical(out$meta$mode, "p=0.500")
  expect_identical(out$meta$z_definition, z_definition)
  expect_identical(get_tls_est(out, what = "draws", params = "CTmax")$value,
                   c(35, 37))
  output <- capture.output(print(out))[1L]
  expect_match(output, "CTmax at p=0.500 threshold", fixed = TRUE)
  expect_match(output, paste0("z (", z_definition, ")"), fixed = TRUE)
  expect_false(any(grepl("p=0.500", capture.output(print(
    tls_z(fit, target_surv = "absolute")
  )), fixed = TRUE)))
})

test_that("tls preserves an invalid profile center when the Hessian is not positive definite", {
  fit <- tls_summary_fit(c("CTmax", "z"), c(36, 4))
  fit$convergence$pdHess <- FALSE
  extracted <- FALSE
  testthat::local_mocked_bindings(
    confint = function(object, parm, level, method, nboot = NULL,
                       boot_seed = NULL) {
      tibble::tibble(
        parameter = parm, conf.low = NA_real_, conf.high = NA_real_,
        estimate = c(36, 4), method = "profile", scale = "identity",
        conf.status = "invalid_center"
      )
    },
    extract_tdt = function(...) {
      extracted <<- TRUE
      stop("tls bypassed the profile result", call. = FALSE)
    },
    .package = "freqTLS"
  )

  out <- suppressWarnings(tls(fit, method = "profile", nboot = 20))

  expect_false(extracted)
  expect_true(all(is.na(out$summary$lower) & is.na(out$summary$upper)))
  expect_identical(out$meta$conf.status[["CTmax"]], "invalid_center")
  expect_null(out$draws)
})

test_that("tls bootstrap metadata lists only requested quantities", {
  fit <- tls_summary_fit(c("CTmax", "z"), c(36, 4))
  extracted <- tls_summary_tdt("p=0.500")
  extracted$T_crit <- list(
    summary = data.frame(temp_median = 31, temp_lower = NA_real_,
                         temp_upper = NA_real_),
    draws = data.frame(.draw = 1:2, temp = c(30, 32))
  )
  extracted$meta$interval_counts <- data.frame(
    quantity = c("z", "CTmax", "T_crit"), group = NA_character_,
    n_requested = 30L, n_usable = 10L,
    conf.status = "bootstrap_unstable"
  )
  extracted$meta$inversion_counts <- data.frame(
    quantity = c("CTmax", "T_crit"), group = "all", n_unusable = c(1L, 1L)
  )
  extracted$meta$nboot_requested <- 30L
  extracted$meta$n_converged <- 29L
  extracted$meta$n_failed <- 1L
  extracted$meta$min_usable_bootstrap <- 20L
  testthat::local_mocked_bindings(
    extract_tdt = function(...) extracted,
    .package = "freqTLS"
  )

  out <- tls(fit, params = "CTmax", target_surv = "absolute",
             lethal = TRUE, nboot = 30)
  expect_identical(out$summary$quantity, "CTmax")
  expect_identical(out$meta$interval_counts$quantity, "CTmax")
  expect_identical(out$meta$inversion_counts$quantity, "CTmax")
  printed <- paste(capture.output(print(out)), collapse = "\n")
  expect_match(printed, "CTmax")
  expect_false(grepl("Tcrit", printed, fixed = TRUE))
  expect_false(grepl("z", printed, fixed = TRUE))

  all_lethal <- tls(fit, params = "all", target_surv = "absolute",
                    lethal = TRUE, nboot = 30)
  expect_setequal(all_lethal$summary$quantity, c("z", "CTmax", "Tcrit"))
  expect_setequal(all_lethal$meta$interval_counts$quantity,
                  c("z", "CTmax", "T_crit"))
  expect_setequal(all_lethal$meta$inversion_counts$quantity,
                  c("CTmax", "T_crit"))

  only_tcrit <- tls_tcrit(fit, target_surv = "absolute", nboot = 30)
  expect_identical(only_tcrit$summary$quantity, "Tcrit")
  expect_identical(only_tcrit$meta$interval_counts$quantity, "T_crit")
  expect_identical(only_tcrit$meta$inversion_counts$quantity, "T_crit")
})

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.