Nothing
# General continuous covariates on the shape / location parameters (item 4):
# a predictor (not just a grouping factor) on low / up / log_k (or CTmax / log_z),
# with design-matrix prediction and Wald intervals for the link-scale
# coefficients. Distinct from the grouped (one-hot factor) path in
# test-shape-covariates.R.
# Manual DGP: log_k varies linearly with a continuous covariate x; everything
# else is constant. Counts are binomial from the engine forward map.
make_cont_k_data <- function(seed = 1, n_per = 9, total = 40) {
set.seed(seed)
grid <- expand.grid(temp = c(34, 36, 38), duration = c(0.5, 1, 2, 4))
CTmax <- 36; z <- 4; low <- 0.02; up <- 0.98
b0 <- log(4); b1 <- 0.6 # log_k = b0 + b1 * x
rows <- do.call(rbind, lapply(seq_len(nrow(grid)), function(j) {
data.frame(temp = grid$temp[j], duration = grid$duration[j],
x = seq(-1, 1, length.out = n_per))
}))
k <- exp(b0 + b1 * rows$x)
mid <- log10(1) - (rows$temp - CTmax) / z
p <- low + (up - low) * stats::plogis(-k * (log10(rows$duration) - mid))
rows$total <- total
rows$survived <- stats::rbinom(nrow(rows), total, p)
rows
}
test_that("a decoupled continuous covariate on log_k fits (constraint relaxed)", {
d <- make_cont_k_data(seed = 3)
# log_k ~ x while CTmax / log_z / low / up stay intercept-only: previously a
# hard error ("must use the same grouping factor as CTmax"). Now allowed.
fit <- fit_tls(
tls_bf(survived | trials(total) ~ time(duration) + temp(temp), log_k ~ x),
data = d, family = "binomial", tref = 1
)
expect_identical(fit$convergence$code, 0L)
})
test_that("the continuous log_k slope is recovered on the link (log) scale", {
d <- make_cont_k_data(seed = 3)
fit <- fit_tls(
tls_bf(survived | trials(total) ~ time(duration) + temp(temp), log_k ~ x),
data = d, family = "binomial", tref = 1
)
est <- fit$estimates
slope <- est$estimate[est$parameter == "k:x"]
expect_length(slope, 1L)
# link-scale slope near the true 0.6 (NOT exponentiated)
expect_equal(slope, 0.6, tolerance = 0.3)
})
test_that("predict applies the continuous design to newdata", {
d <- make_cont_k_data(seed = 3)
fit <- fit_tls(
tls_bf(survived | trials(total) ~ time(duration) + temp(temp), log_k ~ x),
data = d, family = "binomial", tref = 1
)
nd <- data.frame(temp = 36, duration = 2, x = c(-1, 0, 1))
p <- predict(fit, nd, type = "survival")
expect_length(p, 3L)
expect_true(all(p > 0 & p < 1))
# x changes k, which changes survival at a fixed temp/duration off the midpoint.
expect_false(isTRUE(all.equal(p[1], p[3])))
})
test_that("a continuous-covariate coefficient gets a finite Wald interval", {
d <- make_cont_k_data(seed = 3)
fit <- fit_tls(
tls_bf(survived | trials(total) ~ time(duration) + temp(temp), log_k ~ x),
data = d, family = "binomial", tref = 1
)
ci <- suppressMessages(confint(fit, "k:x", method = "wald"))
expect_true(is.finite(ci$conf.low) && is.finite(ci$conf.high))
expect_lt(ci$conf.low, ci$conf.high)
# The interval is on the LINK (log) scale and brackets the estimate -- it must
# NOT be back-transformed (a slope is not a natural-scale k).
est_kx <- fit$estimates$estimate[fit$estimates$parameter == "k:x"]
expect_gt(est_kx, ci$conf.low)
expect_lt(est_kx, ci$conf.high)
# profile routes general coefficients to Wald (no group level to profile).
cp <- suppressMessages(confint(fit, "k:x", method = "profile"))
expect_identical(cp$method, "wald")
})
test_that("a single nonconstant shape column is a link-scale coefficient", {
d <- make_cont_k_data(seed = 3)
d$x_shift <- d$x + 2
fit <- fit_tls(
tls_bf(survived | trials(total) ~ time(duration) + temp(temp),
low ~ 0 + x_shift, up ~ 0 + x_shift, log_k ~ 0 + x_shift),
data = d, family = "binomial", tref = 1
)
expect_identical(colnames(fit$tmb_inputs$data$X_low), "x_shift")
expect_identical(colnames(fit$tmb_inputs$data$X_up), "x_shift")
expect_identical(colnames(fit$tmb_inputs$data$X_logk), "x_shift")
shape <- get_shape(fit, method = "wald")
row <- shape[shape$parameter == "k:x_shift", ]
expect_equal(row$scale, "log")
expect_true(is.finite(row$conf.low) && is.finite(row$conf.high))
printed <- capture.output(summary(fit))
expect_true(any(grepl("scale shown per row", printed, fixed = TRUE)))
expect_true(any(grepl("k:x_shift", printed, fixed = TRUE)))
expect_true(any(grepl("log link", printed, fixed = TRUE)))
nd <- data.frame(temp = 36, duration = 2, x_shift = c(1, 2, 3))
pred <- predict(fit, nd, type = "survival")
expect_equal(length(pred), 3L)
expect_false(isTRUE(all.equal(pred[1L], pred[3L])))
fitted_p <- as.numeric(fit$obj$report(fit$par)$p_fitted)
expect_equal(predict(fit, d, type = "survival"), fitted_p, tolerance = 1e-8)
reps <- matrix(-1, nrow = 3, ncol = nrow(fit$estimates),
dimnames = list(NULL, fit$estimates$parameter))
k_boot <- freqTLS:::tls_boot_target("k:x_shift", fit, reps)
low_boot <- freqTLS:::tls_boot_target("low:x_shift", fit, reps)
expect_equal(k_boot$values, rep(-1, 3))
expect_equal(low_boot$values, rep(-1, 3))
expect_equal(k_boot$backtransform(-1), -1)
expect_equal(low_boot$backtransform(-1), -1)
})
test_that("fitted transformations are reused for single-row and batched predictions", {
d <- make_cont_k_data(seed = 9)
fit <- fit_tls(
tls_bf(survived | trials(total) ~ time(duration) + temp(temp),
log_k ~ scale(temp)),
data = d, family = "binomial", tref = 1
)
one <- data.frame(temp = 37, duration = 2)
batch <- rbind(one, data.frame(temp = 48, duration = 2))
p_one <- predict(fit, one, type = "survival")
p_batch <- predict(fit, batch, type = "survival")
expect_equal(p_one, p_batch[1L], tolerance = 1e-12)
expect_equal(predict(fit, d, type = "survival"),
as.numeric(fit$obj$report(fit$par)$p_fitted), tolerance = 1e-8)
})
test_that("independent one-hot shape groupings are predicted by their own rows", {
d <- simulate_tls(family = "binomial", group = c("A", "B"),
CTmax = c(35, 38), z = c(4, 5), seed = 14)
d$site <- factor(ifelse(d$group == "A", "B", "A"), levels = c("A", "B"))
std <- standardize_data(d, temp = "temp", duration = "duration",
n_total = "total", n_surv = "survived",
duration_unit = "minutes")
fit <- fit_4pl(std, ctmax = ~ 0 + group, z = ~ 0 + group,
low = ~ 0 + site, t_ref = 1, family = "binomial", quiet = TRUE)
grouped_low <- get_shape(fit, method = "wald")
grouped_low <- grouped_low[startsWith(grouped_low$parameter, "low:"), ]
expect_equal(nrow(grouped_low), 2L)
expect_true(all(grouped_low$scale == "natural"))
expect_true(all(grouped_low$estimate > 0 & grouped_low$estimate < 1))
expect_equal(predict(fit$fit, std, type = "survival"),
as.numeric(fit$fit$obj$report(fit$fit$par)$p_fitted),
tolerance = 1e-8)
})
test_that("canonical group predictions work for a shared non-group factor design", {
d <- simulate_tls(family = "binomial", group = c("A", "B"),
CTmax = c(35, 38), z = c(4, 5), seed = 141)
d$population <- d$group
std <- standardize_data(d, temp = "temp", duration = "duration",
n_total = "total", n_surv = "survived",
duration_unit = "minutes")
fit <- suppressWarnings(fit_4pl(
std, by = "population", ctmax = ~ 0 + population,
z = ~ 0 + population, low = ~ population, t_ref = 1,
family = "binomial", quiet = TRUE
))
nd <- data.frame(temp = 36, duration = 1, group = "A")
expect_true(is.finite(predict(fit$fit, nd, type = "survival")))
nd_explicit <- transform(nd, population = "B")
pars_explicit <- predict(fit$fit, nd_explicit, type = "parameters")
low_b <- fit$fit$estimates$estimate[fit$fit$estimates$parameter == "low:B"]
expect_equal(pars_explicit$low, low_b)
lt <- derive_lt(fit$fit, p = 0.6, temp = 36, group = "A")
expect_equal(predict(fit$fit, transform(nd, duration = lt),
type = "survival"), 0.6, tolerance = 1e-6)
expect_true(is.finite(derive_tcrit(fit$fit, rate = 100, group = "A",
target_surv = 0.6)))
expect_true(is.finite(derive_ctmax(fit$fit, surv = 0.6, group = "A")))
trace <- data.frame(time = c(0, 0.1), temp = c(36, 36))
expect_true(all(is.finite(predict_heat_injury(fit, trace, group = "A",
target_surv = 0.6)$survival)))
})
test_that("tidy_parameters labels exponentiated z coefficients naturally", {
d <- make_cont_k_data(seed = 142)
fit <- suppressWarnings(fit_tls(
tls_bf(survived | trials(total) ~ time(duration) + temp(temp),
CTmax ~ x, log_z ~ x),
data = d, family = "binomial", tref = 1
))
z_row <- get_z(fit, conf.int = FALSE)
expect_true(any(z_row$parameter == "z:x"))
expect_equal(z_row$scale[z_row$parameter == "z:x"], "natural")
expect_true(z_row$estimate[z_row$parameter == "z:x"] > 0)
})
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.