tests/testthat/test-bootstrap.R

# Parametric bootstrap confidence intervals (R/bootstrap.R) and the
# auto-fallback wired into confint() (R/confint.R).

fit_boot_binom <- function(seed = 101) {
  d <- simulate_tls(family = "binomial", CTmax = 36, z = 4, seed = seed)
  suppressWarnings(fit_tls(d, y = survived, n = total, time = duration,
                           temp = temp, family = "binomial", tref = 1))
}

sparse_fit <- function(seed = 9) {
  ds <- simulate_tls(family = "binomial", temps = c(35, 36), times = c(1, 2),
                     reps = 2, n = 10, CTmax = 36, z = 4, seed = seed)
  suppressWarnings(fit_tls(ds, y = survived, n = total, time = duration,
                           temp = temp, family = "binomial", tref = 1))
}

test_that("method = 'bootstrap' returns a finite percentile interval around the estimate", {
  fit <- fit_boot_binom()
  ci <- suppressMessages(
    confint(fit, "CTmax", method = "bootstrap", nboot = 200L, boot_seed = 1L)
  )
  expect_s3_class(ci, "tbl_df")
  expect_identical(
    names(ci),
    c("parameter", "conf.low", "conf.high", "estimate", "level", "method",
      "scale", "conf.status")
  )
  expect_identical(ci$method, "bootstrap")
  expect_identical(ci$conf.status, "bootstrap")
  expect_true(is.finite(ci$conf.low) && is.finite(ci$conf.high))
  expect_true(ci$conf.low <= ci$estimate && ci$estimate <= ci$conf.high)
})

test_that("the bootstrap is reproducible with boot_seed and leaves the RNG untouched", {
  fit <- fit_boot_binom()
  set.seed(999)
  before <- .Random.seed
  a <- suppressMessages(
    confint(fit, c("CTmax", "z"), method = "bootstrap", nboot = 200L, boot_seed = 42L)
  )
  b <- suppressMessages(
    confint(fit, c("CTmax", "z"), method = "bootstrap", nboot = 200L, boot_seed = 42L)
  )
  expect_equal(a$conf.low, b$conf.low)
  expect_equal(a$conf.high, b$conf.high)
  # The caller's RNG stream is restored (boot_seed sets the seed locally).
  expect_identical(.Random.seed, before)
})

test_that("bootstrap percentile intervals are exactly equivariant: z == exp(log_z)", {
  fit <- fit_boot_binom()
  cz <- suppressMessages(
    confint(fit, "z", method = "bootstrap", nboot = 300L, boot_seed = 7L)
  )
  clz <- suppressMessages(
    confint(fit, "log_z", method = "bootstrap", nboot = 300L, boot_seed = 7L)
  )
  expect_equal(cz$conf.low, exp(clz$conf.low), tolerance = 1e-10)
  expect_equal(cz$conf.high, exp(clz$conf.high), tolerance = 1e-10)
})

test_that("the bootstrap CTmax interval covers the simulating truth", {
  fit <- fit_boot_binom(seed = 202)
  ci <- suppressMessages(
    confint(fit, "CTmax", method = "bootstrap", nboot = 400L, boot_seed = 3L)
  )
  expect_true(ci$conf.low <= 36 && 36 <= ci$conf.high)
})

test_that("a non-closing profile auto-falls back to a finite bootstrap interval", {
  fs <- sparse_fit()
  expect_message(
    suppressWarnings(
      confint(fs, "z", method = "profile", nboot = 200L, boot_seed = 5L)
    ),
    "parametric bootstrap"
  )
  ci <- suppressWarnings(suppressMessages(
    confint(fs, "z", method = "profile", nboot = 200L, boot_seed = 5L)
  ))
  expect_identical(ci$method, "bootstrap")
  expect_identical(ci$conf.status, "bootstrap")
  expect_true(is.finite(ci$conf.low) && is.finite(ci$conf.high))
})

test_that("bootstrap refits do not leak low-level optimiser warnings", {
  fs <- sparse_fit()
  expect_no_warning(
    suppressMessages(
      tls_bootstrap_replicates(fs, nboot = 20L, seed = 23L, cores = 1L)
    )
  )
})

test_that("fallback = FALSE keeps the strict profile NA on a non-closing side", {
  fs <- sparse_fit()
  ci <- suppressWarnings(
    confint(fs, "z", method = "profile", fallback = FALSE)
  )
  expect_true(is.na(ci$conf.low) || is.na(ci$conf.high))
  expect_match(ci$conf.status, "open")
})

test_that("bootstrap works per group on a grouped fit", {
  d <- simulate_tls(group = c("A", "B"), CTmax = c(35, 38), z = c(4, 3),
                    family = "binomial", seed = 11)
  fg <- suppressWarnings(fit_tls(d, y = survived, n = total, time = duration,
                                 temp = temp, group = group,
                                 family = "binomial", tref = 1))
  ci <- suppressMessages(
    confint(fg, c("CTmax:A", "CTmax:B"), method = "bootstrap",
            nboot = 200L, boot_seed = 2L)
  )
  expect_identical(nrow(ci), 2L)
  expect_true(all(is.finite(ci$conf.low)) && all(is.finite(ci$conf.high)))
  # The per-group point estimates are ordered A < B (truth 35 < 38).
  expect_lt(ci$estimate[ci$parameter == "CTmax:A"],
            ci$estimate[ci$parameter == "CTmax:B"])
})

test_that("bootstrap retains every varying fixed-effect shape coefficient", {
  d <- simulate_tls(family = "binomial", CTmax = 36, z = 4, seed = 71)
  d$temp_c <- d$temp - mean(d$temp)
  fit <- suppressWarnings(fit_tls(
    tls_bf(
      survived | trials(total) ~ time(duration) + temp(temp),
      low ~ temp_c, up ~ temp_c, log_k ~ temp_c
    ),
    data = d, family = "binomial", tref = 1
  ))
  boot <- tls_bootstrap_replicates(fit, nboot = 12, seed = 1)
  expect_gt(boot$n_converged, 1L)
  expect_true(all(c("low:temp_c", "up:temp_c", "k:temp_c") %in%
                  colnames(boot$replicates)))
  expect_true(all(is.finite(boot$replicates[boot$converged, "low:temp_c"])))
})

test_that("bootstrap retains grouped shape coefficients independently of CTmax", {
  d <- simulate_tls(family = "binomial", CTmax = 36, z = 4, seed = 72)
  d$shape_group <- rep(c("A", "B"), length.out = nrow(d))
  fit <- suppressWarnings(fit_tls(
    tls_bf(
      survived | trials(total) ~ time(duration) + temp(temp),
      low ~ 0 + shape_group, up ~ 0 + shape_group, log_k ~ 0 + shape_group
    ),
    data = d, family = "binomial", tref = 1
  ))
  boot <- tls_bootstrap_replicates(fit, nboot = 12, seed = 1)
  expect_gt(boot$n_converged, 1L)
  expect_true(all(c("low:A", "up:B", "k:A") %in% colnames(boot$replicates)))
})

test_that("beta-binomial bootstrap draws and refits without error", {
  d <- simulate_tls(family = "beta_binomial", phi = 50, CTmax = 36, z = 4,
                    seed = 13)
  fit <- suppressWarnings(fit_tls(d, y = survived, n = total, time = duration,
                                  temp = temp, family = "beta_binomial", tref = 1))
  ci <- suppressMessages(
    confint(fit, "CTmax", method = "bootstrap", nboot = 200L, boot_seed = 4L)
  )
  expect_true(is.finite(ci$conf.low) && is.finite(ci$conf.high))
  expect_true(ci$conf.low <= ci$estimate && ci$estimate <= ci$conf.high)
})

test_that("multicore bootstrap matches single-core for the same seed", {
  fit <- fit_boot_binom()
  one <- suppressMessages(confint(fit, c("CTmax", "z"), method = "bootstrap",
                                  nboot = 200L, boot_seed = 8L, cores = 1L))
  two <- suppressMessages(confint(fit, c("CTmax", "z"), method = "bootstrap",
                                  nboot = 200L, boot_seed = 8L, cores = 2L))
  # Responses are pre-drawn under the seed and refits are deterministic, so the
  # interval is identical regardless of cores (forking on Unix; sequential on
  # Windows, which still matches).
  expect_equal(one$conf.low, two$conf.low)
  expect_equal(one$conf.high, two$conf.high)
})

test_that("bootstrap requests above two cores warn and use two deterministically", {
  fit <- fit_boot_binom()
  two <- suppressMessages(
    tls_bootstrap_replicates(fit, nboot = 20L, seed = 19L, cores = 2L)
  )
  expect_warning(
    capped <- suppressMessages(
      tls_bootstrap_replicates(fit, nboot = 20L, seed = 19L, cores = 8L)
    ),
    "limited to 2 cores"
  )

  expect_identical(capped$converged, two$converged)
  expect_equal(capped$replicates, two$replicates)
})

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.