Nothing
# Grouped-fit tests (SPEC.md S11, test-group). A two-group binomial simulation
# with distinct CTmax and z per group should recover the contrasts within
# tolerance, and the grouped / contrast profile targets should return finite,
# closed intervals. One fit, fixed seed, so the test stays fast.
fit_grouped <- function(seed = 2) {
dg <- simulate_tls(family = "binomial", group = c("A", "B"),
CTmax = c(34, 38), z = c(3, 5), seed = seed)
suppressWarnings(
fit_tls(dg, y = survived, n = total, time = duration, temp = temp,
group = group, family = "binomial", tref = 1)
)
}
test_that("the grouped fit recovers the per-group CTmax and z", {
fg <- fit_grouped()
est <- stats::setNames(fg$estimates$estimate, fg$estimates$parameter)
# Per-group CTmax within 0.6 C, z within 0.8 (absolute).
expect_lt(abs(est[["CTmax:A"]] - 34), 0.6)
expect_lt(abs(est[["CTmax:B"]] - 38), 0.6)
expect_lt(abs(est[["z:A"]] - 3), 0.8)
expect_lt(abs(est[["z:B"]] - 5), 0.8)
})
test_that("the grouped fit recovers the CTmax and z contrasts", {
fg <- fit_grouped()
est <- stats::setNames(fg$estimates$estimate, fg$estimates$parameter)
# Delta CTmax = CTmax_B - CTmax_A ~ 4 (tol 0.6).
expect_lt(abs((est[["CTmax:B"]] - est[["CTmax:A"]]) - 4), 0.6)
# Delta z = z_B - z_A ~ 2 (tol 0.8).
expect_lt(abs((est[["z:B"]] - est[["z:A"]]) - 2), 0.8)
})
test_that("grouped CTmax:grp and z:grp profile to finite closed intervals", {
fg <- fit_grouped()
ciC <- suppressWarnings(confint(fg, "CTmax:A", method = "profile"))
expect_true(is.finite(ciC$conf.low) && is.finite(ciC$conf.high))
expect_lt(ciC$conf.low, ciC$estimate)
expect_gt(ciC$conf.high, ciC$estimate)
expect_identical(ciC$conf.status, "ok")
ciZ <- suppressWarnings(confint(fg, "z:B", method = "profile"))
expect_true(is.finite(ciZ$conf.low) && is.finite(ciZ$conf.high))
expect_lt(ciZ$conf.low, ciZ$estimate)
expect_gt(ciZ$conf.high, ciZ$estimate)
expect_gt(ciZ$conf.low, 0)
})
test_that("the dCTmax contrast profile matches the per-group difference and closes", {
fg <- fit_grouped()
est <- stats::setNames(fg$estimates$estimate, fg$estimates$parameter)
cc <- suppressWarnings(confint(fg, "dCTmax:A-B", method = "profile"))
# The written A-B contrast equals CTmax_A - CTmax_B from the
# ~ 0 + group fit (equivariance of the reparameterisation).
expect_equal(cc$estimate, unname(est[["CTmax:A"]] - est[["CTmax:B"]]),
tolerance = 1e-4)
expect_true(is.finite(cc$conf.low) && is.finite(cc$conf.high))
expect_lt(cc$conf.low, cc$estimate)
expect_gt(cc$conf.high, cc$estimate)
})
test_that("the dlog_z contrast profile matches the per-group log-z difference", {
fg <- fit_grouped()
est <- stats::setNames(fg$estimates$estimate, fg$estimates$parameter)
cl <- suppressWarnings(confint(fg, "dlog_z:A-B", method = "profile"))
expect_equal(cl$estimate, unname(log(est[["z:A"]]) - log(est[["z:B"]])),
tolerance = 1e-4)
expect_true(is.finite(cl$conf.low) && is.finite(cl$conf.high))
# The z ratio is exp(dlog_z); recovers z_A / z_B ~ 3/5 within tolerance.
expect_lt(abs(exp(cl$estimate) - 3 / 5), 0.25)
})
test_that("contrast refits preserve fixed asymptotes and response bounds", {
dg <- simulate_tls(family = "binomial", group = c("A", "B"),
CTmax = c(34, 38), z = c(3, 5), low = 0.02,
up = 0.98, seed = 27)
fg <- fit_tls(dg, y = survived, n = total, time = duration, temp = temp,
group = group, family = "binomial", tref = 1,
bounds = c(0.01, 0.99), fixed_low = 0.02,
fixed_up = 0.98, quiet = TRUE)
target <- list(ref = "A", alt = "B", which_par = "dCTmax",
parm = "dCTmax:A-B")
contrast <- tls_contrast_refit(fg, target)
refit_data <- contrast$fit$tmb_inputs$data
expect_identical(refit_data$fixed_low, 1L)
expect_identical(refit_data$fixed_up, 1L)
expect_equal(refit_data$fixed_low_value, 0.02)
expect_equal(refit_data$fixed_up_value, 0.98)
expect_equal(contrast$fit$logLik, fg$logLik, tolerance = 1e-4)
bad_nuisance <- fg
bad_nuisance$diag_data$y <- bad_nuisance$diag_data$n - bad_nuisance$diag_data$y
expect_error(
suppressWarnings(tls_contrast_refit(bad_nuisance, target)),
"contrast refit does not reproduce the fitted likelihood"
)
})
test_that("contrast refits retain residual df and fixed shape designs", {
dg <- simulate_tls(
temps = seq(30, 42, by = 2), times = c(1, 2, 4, 8), reps = 3, n = 30,
family = "binomial", group = c("A", "B"),
CTmax = c(34, 38), z = c(3, 5), tref = 1, seed = 271
)
dg$x <- rep(c(-1, 1), length.out = nrow(dg))
fg <- fit_tls(
tls_bf(
survived | trials(total) ~ time(duration) + temp(temp),
CTmax ~ group, log_z ~ group,
low ~ x, up ~ x, log_k ~ x
),
data = dg, family = "binomial", tref = 1, quiet = TRUE
)
target <- tls_resolve_contrast(fg, "dCTmax:A-B")
bad_fit <- fg
bad_fit$tmb_inputs$data$X_logz <- bad_fit$tmb_inputs$data$X_logz[
rev(seq_len(nrow(dg))), , drop = FALSE
]
expect_error(
tls_contrast_refit(bad_fit, target),
"row-matched CTmax and log_z"
)
withr::local_options(contrasts = c("contr.sum", "contr.poly"))
recoded <- tls_contrast_refit(fg, target)
contrast <- recoded$fit
expect_equal(tls_ci_df(contrast), tls_ci_df(fg))
expect_identical(contrast$diag_data$y, fg$diag_data$y)
for (shape in c("low", "up", "logk")) {
X <- paste0("X_", shape)
beta <- switch(shape, low = "beta_low", up = "beta_up", logk = "beta_logk")
expect_equal(contrast$tmb_inputs$data[[X]], fg$tmb_inputs$data[[X]])
expect_equal(contrast$par[[beta]], fg$par[[beta]], tolerance = 1e-5)
}
expect_equal(contrast$logLik, fg$logLik, tolerance = 1e-4)
expect_equal(
recoded$target$estimate,
fg$estimates$estimate[match("CTmax:A", fg$estimates$parameter)] -
fg$estimates$estimate[match("CTmax:B", fg$estimates$parameter)],
tolerance = 1e-5
)
})
test_that("bootstrap contrasts follow the written A-minus-B direction", {
fit <- list(estimates = data.frame(
parameter = c("CTmax:A", "CTmax:B", "z:A", "z:B"),
estimate = c(36, 40, 3, 5)
))
reps <- cbind(
`CTmax:A` = c(35, 36), `CTmax:B` = c(39, 41),
`z:A` = c(2.5, 3.5), `z:B` = c(4.5, 5.5)
)
ct <- tls_boot_target("dCTmax:A-B", fit, reps)
lz <- tls_boot_target("dlog_z:A-B", fit, reps)
expect_equal(ct$estimate, -4)
expect_equal(ct$values, c(-4, -5))
expect_equal(lz$estimate, log(3) - log(5))
expect_equal(lz$values, log(reps[, "z:A"]) - log(reps[, "z:B"]))
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.