tests/testthat/test-multiple-ctmax-random-intercepts.R

test_that("CTmax accepts crossed independent plant and measurement-day intercepts", {
  set.seed(20261130)
  plant <- factor(rep(sprintf("p%02d", 1:12), each = 8 * 6 * 4))
  meas_day <- factor(rep(rep(sprintf("d%02d", 1:8), each = 6 * 4), times = 12))
  temp <- rep(rep(seq(30, 40, by = 2), each = 4), times = 12 * 8)
  duration <- rep(rep(c(0.25, 0.5, 1, 2), times = 6), times = 12 * 8)
  b_plant <- stats::setNames(stats::rnorm(12, 0, 1.0), levels(plant))
  b_day <- stats::setNames(stats::rnorm(8, 0, 0.5), levels(meas_day))
  mid <- 0 - (temp - (36 + b_plant[plant] + b_day[meas_day])) / 4
  p <- 0.02 + 0.96 / (1 + exp(5 * (log10(duration) - mid)))
  total <- rep(40, length(p))
  d <- data.frame(
    survived = stats::rbinom(length(p), total, p), total = total,
    temp = temp, duration = duration, plant = plant, meas_day = meas_day
  )

  f <- tls_bf(survived | trials(total) ~ time(duration) + temp(temp),
              CTmax ~ 1 + (1 | plant) + (1 | meas_day))
  fit <- suppressWarnings(fit_tls(f, data = d, family = "binomial", tref = 1,
                                  quiet = TRUE))
  expect_identical(fit$convergence$code, 0L)
  expect_true(isTRUE(fit$convergence$pdHess))
  expect_length(fit$re_ct_terms, 2L)
  expect_identical(fit$tmb_inputs$random, "b_CT")
  expect_setequal(fit$estimates$parameter[startsWith(fit$estimates$parameter,
                                                     "sigma_CTmax")],
                  c("sigma_CTmax:plant", "sigma_CTmax:meas_day"))
  expect_true(all(is.finite(fit$estimates$estimate[
    startsWith(fit$estimates$parameter, "sigma_CTmax")])) )
  sigma_names <- fit$estimates$parameter[startsWith(fit$estimates$parameter,
                                                     "sigma_CTmax")]
  sigma_hat <- stats::setNames(
    fit$estimates$estimate[match(sigma_names, fit$estimates$parameter)],
    sub("^sigma_CTmax:", "", sigma_names)
  )
  # With 12 plant and 8 day levels, these broad bands allow downward ML bias
  # while still recovering both generating variance scales.
  expect_true(sigma_hat[["plant"]] > 0.4 && sigma_hat[["plant"]] < 1.6)
  expect_true(sigma_hat[["meas_day"]] > 0.2 && sigma_hat[["meas_day"]] < 0.9)
  ci <- suppressMessages(confint(fit, parm = sigma_names, method = "wald"))
  expect_identical(ci$parameter, sigma_names)
  expect_true(all(is.finite(ci$conf.low) & is.finite(ci$conf.high)))
  ci_profile <- suppressMessages(confint(fit, parm = sigma_names, method = "profile"))
  expect_identical(ci_profile$method, rep("wald", 2L))
  tidy <- tidy_parameters(fit)
  expect_true(all(is.finite(tidy$conf.low[match(sigma_names, tidy$parameter)])))
  boot <- tls_bootstrap_replicates(fit, nboot = 2, seed = 404)
  expect_identical(colnames(boot$replicates)[startsWith(colnames(boot$replicates),
                                                         "sigma_CTmax")], sigma_names)
  boot_targets <- lapply(sigma_names, tls_boot_target, fit = fit,
                         reps = boot$replicates)
  expect_identical(vapply(boot_targets, `[[`, character(1), "scale"),
                   rep("log", length(sigma_names)))
  re <- ranef(fit)
  expect_equal(sum(re$term == "CTmax:plant"), 12L)
  expect_equal(sum(re$term == "CTmax:meas_day"), 8L)

  duplicate <- tls_bf(survived | trials(total) ~ time(duration) + temp(temp),
                      CTmax ~ 1 + (1 | plant) + (1 | plant))
  expect_error(fit_tls(duplicate, data = d, family = "binomial", tref = 1,
                       quiet = TRUE), "appear only once")

  d$plant_alias <- factor(d$plant, levels = levels(d$plant),
                          labels = paste0("site_", rev(seq_along(levels(d$plant)))))
  duplicate_partition <- tls_bf(
    survived | trials(total) ~ time(duration) + temp(temp),
    CTmax ~ 1 + (1 | plant) + (1 | plant_alias)
  )
  expect_error(
    fit_tls(duplicate_partition, data = d, family = "binomial", tref = 1,
            quiet = TRUE),
    "encode the same grouping partition"
  )
})

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.