tests/testthat/test-fit-4pl.R

# fit_4pl() — the bayesTLS-twin fitting facade over the TMB engine.

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

test_that("make_4pl_formula builds the engine tls_formula from direct args", {
  ff <- make_4pl_formula(ctmax = ~ 0 + grp, z = ~ 0 + grp, family = "binomial")
  expect_s3_class(ff, "tls_formula")
  expect_match(deparse1(ff$response), "n_surv \\| trials\\(n_total\\)")
  expect_match(deparse1(ff$sub_formulas$CTmax), "0 \\+ grp")
  expect_match(deparse1(ff$sub_formulas$log_z), "0 \\+ grp")
  # constant-shape invariant: shapes default to ~ 1
  expect_match(deparse1(ff$sub_formulas$low), "~ *1$")
  expect_match(deparse1(ff$sub_formulas$up), "~ *1$")
  # beta family uses the bare-name response
  fb <- make_4pl_formula(family = "beta")
  expect_match(deparse1(fb$response), "survival ~")
  # `by` is shorthand for grouping CTmax and z
  fby <- make_4pl_formula(by = "sex", family = "binomial")
  expect_match(deparse1(fby$sub_formulas$CTmax), "0 \\+ sex")
})

test_that("make_4pl_formula requires one explicit by moderator", {
  expect_error(
    make_4pl_formula(by = c("sex", "population")),
    "one non-missing column name"
  )
  expect_error(make_4pl_formula(by = NA_character_), "one non-missing column name")
})

test_that("fit_4pl recovers the simulating truth (ungrouped) and returns freq_tls", {
  s <- std_sim(seed = 1, CTmax = 36, z = 4)
  f <- fit_4pl(s, t_ref = 1, family = "binomial", quiet = TRUE)
  expect_s3_class(f, "freq_tls")
  printed <- paste(capture.output(print(f)), collapse = " ")
  expect_match(printed, "low ~ 1")
  expect_match(printed, "up ~ 1")
  expect_match(printed, "k ~ 1")
  expect_named(f, c("fit", "data", "formula", "meta"))
  expect_identical(f$fit$convergence$code, 0L)
  e <- f$fit$estimates
  expect_equal(e$estimate[e$parameter == "CTmax"], 36, tolerance = 0.3)
  expect_equal(e$estimate[e$parameter == "z"], 4, tolerance = 0.4)
  # meta carries the twin contract
  expect_identical(f$meta$threshold, "relative")
  expect_identical(f$meta$t_ref, 1)
  expect_equal(f$meta$temp_mean, attr(s, "tdt_meta")$temp_mean)
  expect_false(f$meta$grouped)
})

test_that("new asymptote controls preserve legacy positional fit_4pl calls", {
  s <- std_sim(seed = 29, CTmax = 36, z = 4)
  positional <- suppressWarnings(fit_4pl(
    s, NULL, NULL, NULL, NULL, NULL, NULL, "relative", 0.5, 60,
    c(0, 1), "binomial", "wald", NULL, list(), FALSE, TRUE
  ))
  named <- suppressWarnings(fit_4pl(
    s, t_ref = 60, family = "binomial", method = "wald", quiet = TRUE
  ))
  expect_equal(positional$fit$estimates$estimate,
               named$fit$estimates$estimate, tolerance = 1e-10)
  expect_equal(as.numeric(logLik(positional)), as.numeric(logLik(named)),
               tolerance = 1e-10)
})

test_that("standardize_data makes fits invariant to the input duration unit", {
  d_hours <- simulate_tls(family = "binomial", CTmax = 36, z = 4, seed = 17)
  d_minutes <- d_hours
  d_minutes$duration <- 60 * d_minutes$duration
  h <- standardize_data(d_hours, temp = "temp", duration = "duration",
                        n_total = "total", n_surv = "survived",
                        duration_unit = "hours")
  m <- standardize_data(d_minutes, temp = "temp", duration = "duration",
                        n_total = "total", n_surv = "survived",
                        duration_unit = "minutes")
  fh <- suppressWarnings(fit_4pl(h, t_ref = 60, family = "binomial", quiet = TRUE))
  fm <- suppressWarnings(fit_4pl(m, t_ref = 60, family = "binomial", quiet = TRUE))
  expect_equal(as.numeric(logLik(fh)), as.numeric(logLik(fm)), tolerance = 1e-8)
  expect_equal(get_ctmax(fh)$estimate, get_ctmax(fm)$estimate, tolerance = 1e-8)
  expect_equal(get_z(fh)$estimate, get_z(fm)$estimate, tolerance = 1e-8)
  expect_identical(fh$meta$t_ref, 60)
})

test_that("fit_4pl converts legacy standardized non-minute durations", {
  d <- simulate_tls(family = "binomial", CTmax = 36, z = 4, seed = 63)
  std <- standardize_data(d, temp = "temp", duration = "duration",
                          n_total = "total", n_surv = "survived",
                          duration_unit = "minutes")
  legacy <- std
  legacy$duration <- legacy$duration / 60
  legacy$logd <- log10(legacy$duration)
  attr(legacy, "tdt_meta")$duration_unit <- "hours"
  attr(legacy, "tdt_meta")$input_duration_unit <- "hours"
  expect_message(
    fit_hours <- fit_4pl(legacy, t_ref = 60, family = "binomial", quiet = FALSE),
    'Converting standardized "hours" durations to minutes'
  )
  fit_minutes <- suppressWarnings(fit_4pl(std, t_ref = 60,
                                           family = "binomial", quiet = TRUE))
  expect_equal(as.numeric(logLik(fit_hours)), as.numeric(logLik(fit_minutes)),
               tolerance = 1e-8)
  expect_equal(get_ctmax(fit_hours)$estimate, get_ctmax(fit_minutes)$estimate,
               tolerance = 1e-8)
  expect_equal(get_z(fit_hours)$estimate, get_z(fit_minutes)$estimate,
               tolerance = 1e-8)
  expect_identical(fit_hours$meta$duration_unit, "minutes")
  expect_identical(fit_hours$meta$input_duration_unit, "hours")
})

test_that("fit_4pl direct grouping equals the engine column-interface grouped fit", {
  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_direct <- suppressWarnings(fit_4pl(s, ctmax = ~ 0 + group, z = ~ 0 + group,
                                       t_ref = 60, family = "binomial", quiet = TRUE))
  f_col <- suppressWarnings(fit_tls(d, y = survived, n = total, time = duration,
                                    temp = temp, group = group,
                                    family = "binomial", tref = 60, quiet = TRUE))
  expect_equal(as.numeric(logLik(f_direct$fit)), as.numeric(logLik(f_col)),
               tolerance = 1e-5)
  expect_true(f_direct$meta$grouped)
  expect_identical(f_direct$meta$moderators, "group")
  # The direct cell-means design (~ 0 + group) now carries the SAME clean group
  # labels as the column interface (CTmax:A, not CTmax:groupA), end to end.
  expect_identical(f_direct$fit$group_levels, f_col$group_levels)
  expect_identical(get_ctmax(f_direct)$parameter, get_ctmax(f_col)$parameter)
  expect_equal(tls(f_direct)$summary, tls(f_col)$summary, tolerance = 1e-5)
  # by="group" is the shorthand for ~ 0 + group and gives identical labels.
  f_by <- suppressWarnings(fit_4pl(s, by = "group", t_ref = 1,
                                   family = "binomial", quiet = TRUE))
  expect_identical(get_ctmax(f_by)$parameter, get_ctmax(f_col)$parameter)
})

test_that("family defaults from the standardized response type", {
  sp <- standardize_data(
    data.frame(t = c(30, 32, 34), e = c(1, 2, 4), fv = c(0.9, 0.5, 0.1)),
    temp = "t", duration = "e", proportion = "fv", duration_unit = "hours")
  expect_identical(attr(sp, "tdt_meta")$response_type, "proportion")
  ff <- make_4pl_formula(family = "beta")
  expect_match(deparse1(ff$response), "survival ~")
})

test_that("fit summaries expose effective shape formulas", {
  f <- fit_4pl(std_sim(seed = 41), low = ~ temp_c, k = ~ 1 + temp_c,
               t_ref = 1, family = "binomial", quiet = TRUE)
  s <- summary(f)
  expect_identical(unname(s$model_formulas),
                   c("~ temp_c", "~ 1", "~ 1 + temp_c"))
})

test_that("the freq_tls workflow is accepted by the plots and extractors", {
  f <- fit_4pl(std_sim(seed = 1), t_ref = 60, family = "binomial", quiet = TRUE)
  # plots (incl. the Confidence Eye) and extractors take the workflow, not just $fit
  expect_s3_class(plot_confidence_eye(f), "ggplot")
  curves <- plot_survival_curves(f)
  tdt <- plot_tdt_curve(f)
  surface <- plot_survival_surface(f)
  expect_s3_class(curves, "ggplot")
  expect_s3_class(tdt, "ggplot")
  expect_s3_class(surface, "ggplot")
  expect_match(curves$labels$x, "minutes")
  expect_match(tdt$labels$y, "minutes")
  expect_match(surface$labels$y, "minutes")
  expect_s3_class(tidy_parameters(f), "tbl_df")
  expect_s3_class(get_ctmax(f), "data.frame")
  expect_type(derive_ctmax(f), "double")
})

test_that("freq_tls supports the standard S3 generics (delegating to the engine fit)", {
  f <- fit_4pl(std_sim(seed = 1), t_ref = 1, family = "binomial", quiet = TRUE)
  expect_type(coef(f), "double")
  expect_s3_class(logLik(f), "logLik")
  expect_true(is.matrix(vcov(f)))
  expect_identical(nobs(f), nobs(f$fit))
  expect_equal(as.numeric(logLik(f)), as.numeric(logLik(f$fit)))
  # confint() and summary() also delegate to the engine fit (not the broken
  # stats::confint.default fall-through on the list).
  ci <- confint(f, "CTmax", method = "wald")
  expect_true(all(c("estimate", "conf.low", "conf.high") %in% names(ci)))
  expect_equal(confint(f, "z", method = "wald")$estimate,
               confint(f$fit, "z", method = "wald")$estimate)
  expect_identical(summary(f)$coefficients, summary(f$fit)$coefficients)
  expect_identical(unname(summary(f)$model_formulas), c("~ 1", "~ 1", "~ 1"))
  # check_tls() (data-adequacy diagnostic) also takes the workflow object.
  expect_identical(suppressWarnings(check_tls(f)), suppressWarnings(check_tls(f$fit)))
})

test_that("ranef() works on a freq_tls fit with random effects", {
  d <- simulate_tls(family = "binomial", temps = c(34, 36, 38), times = c(1, 4),
                    reps = 1, n = 8, CTmax = 36, z = 4, re_sd = 1.2,
                    n_re_groups = 10, seed = 42)
  std <- standardize_data(d, temp = "temp", duration = "duration",
                          n_total = "total", n_surv = "survived", duration_unit = "minutes")
  wf <- suppressWarnings(fit_4pl(std, ctmax = ~ 1 + (1 | colony),
                                 family = "binomial", t_ref = 1, quiet = TRUE))
  expect_equal(ranef(wf), ranef(wf$fit))
})

test_that("fit_4pl validates standardized data, thresholds, and bounds", {
  raw <- simulate_tls(family = "binomial", seed = 2)
  expect_error(fit_4pl(raw), "standardize_data")
  s <- std_sim(seed = 2)
  expect_error(fit_4pl(s, threshold = "absolute", quiet = TRUE), "absolute")
  expect_error(fit_4pl(s, bounds = c(0.9, 0.1), quiet = TRUE), "bounds")
  expect_error(fit_4pl(s, p = "banana", quiet = TRUE), "p")
  expect_error(fit_4pl(s, p = NA_real_, quiet = TRUE), "p")
  expect_error(fit_4pl(s, p = 1, quiet = TRUE), "p")
})

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.