Nothing
# Profile-likelihood tests (SPEC.md S11, test-profile). Each test does one fit
# on a fixed-seed simulation, so the suite stays fast. The headline checks are:
# D(MLE) ~ 0, finite closed CIs that bracket the estimate for CTmax and z, exact
# equivariance (ci_z == exp(ci_log_z)), preserved asymmetry, and an honest
# non-closing case that warns and returns NA without crashing.
fit_binom <- function(seed = 1) {
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)
)
}
# Exercise the profile-warning branches with a small synthetic fit. Mocking the
# inner NLL keeps these diagnostics deterministic and avoids extra TMB fits.
profile_warning_fixture <- function(profile_deviance) {
fit <- list(
logLik = 0,
diag_data = list(y = seq_len(40)),
par = 1
)
target <- list(
parm = "CTmax",
theta_hat = 0,
estimate = 0,
scale = "internal",
transformation = "identity"
)
testthat::local_mocked_bindings(
tls_ci_df = function(fit) 30,
tls_target_curvature_se = function(fit, target) 1,
tls_profile_nll_fun = function(fit, target, trace) {
function(theta) profile_deviance(theta) / 2
},
.package = "freqTLS",
.env = parent.frame()
)
list(fit = fit, target = target)
}
test_that("non-monotone profile curves warn", {
# The right side rises from zero, dips at the second local minimum, then rises
# again; this isolates the profile-geometry warning from model fitting.
fixture <- profile_warning_fixture(function(theta) theta^2 * (theta - 2)^2)
expect_warning(
result <- tls_profile_ci_curve(fixture$fit, fixture$target, 0.95, 31, FALSE),
"non-monotone"
)
expect_false(identical(result$conf.status, "ok"))
expect_match(plot(result)$labels$caption, "non-monotone")
})
test_that("profile rejects a non-zero deviance at the fitted estimate", {
fixture <- profile_warning_fixture(function(theta) 1 + theta^2)
expect_warning(
result <- tls_profile_ci_curve(fixture$fit, fixture$target, 0.95, 31, FALSE),
"not approximately zero"
)
expect_identical(result$conf.status, "invalid_center")
expect_true(is.na(result$conf.low) && is.na(result$conf.high))
})
test_that("profile requires the fitted center to lie inside the confidence set", {
fixture <- profile_warning_fixture(function(theta) 0.0005 + theta^2)
expect_warning(
result <- tls_profile_ci_curve(
fixture$fit, fixture$target, level = 0.001, npoints = 31, trace = FALSE
),
"not approximately zero"
)
expect_identical(result$conf.status, "invalid_center")
expect_true(is.na(result$conf.low) && is.na(result$conf.high))
expect_gt(result$deviance[which.min(abs(result$profile_value))], result$cutoff)
})
test_that("invalid profile centers do not get hidden by bootstrap fallback", {
fit <- fit_binom()
testthat::local_mocked_bindings(
profile = function(object, parm, ...) {
list(conf.low = NA_real_, conf.high = NA_real_, estimate = 36,
scale = "identity", conf.status = "invalid_center")
},
tls_confint_bootstrap = function(...) stop("bootstrap fallback must not run"),
.package = "freqTLS",
.env = parent.frame()
)
ci <- confint(fit, parm = "CTmax", method = "profile", fallback = TRUE)
expect_identical(ci$conf.status, "invalid_center")
expect_true(is.na(ci$conf.low) && is.na(ci$conf.high))
})
test_that("random-effects fallback preserves an invalid profile center", {
estimates <- c(CTmax = 36, z = 4)
fit <- structure(
list(estimates = tibble::tibble(parameter = names(estimates)),
convergence = list(pdHess = TRUE), re = list()),
class = "profile_tls"
)
testthat::local_mocked_bindings(
profile = function(object, parm, ...) {
invalid <- parm == "CTmax"
list(
conf.low = if (invalid) NA_real_ else 3,
conf.high = NA_real_,
estimate = estimates[[parm]],
scale = "identity",
conf.status = if (invalid) "invalid_center" else "open_upper"
)
},
tls_confint_wald = function(fit, parm, level) {
estimate <- unname(estimates[parm])
tibble::tibble(
parameter = parm, conf.low = estimate - 1, conf.high = estimate + 1,
estimate = estimate, level = level, method = "wald", scale = "identity",
conf.status = "ok"
)
},
.package = "freqTLS",
.env = parent.frame()
)
ci <- suppressMessages(
confint(fit, parm = names(estimates), method = "profile", fallback = TRUE)
)
expect_identical(ci$parameter, names(estimates))
expect_identical(ci$conf.status, c("invalid_center", "ok"))
expect_identical(ci$method, c("profile", "wald"))
expect_identical(ci$conf.low, c(NA_real_, 3))
expect_identical(ci$conf.high, c(NA_real_, 5))
})
test_that("negative deviance away from the MLE warns about an optimizer mismatch", {
fixture <- profile_warning_fixture(function(theta) {
theta^2 * (1 - 2 * exp(-((theta - 1)^2) / 0.1))
})
warnings <- character()
withCallingHandlers(
result <- tls_profile_ci_curve(
fixture$fit, fixture$target, 0.95, 31, FALSE
),
warning = function(w) {
warnings <<- c(warnings, conditionMessage(w))
invokeRestart("muffleWarning")
}
)
expect_true(any(grepl("dips below zero away from the reported MLE", warnings)))
expect_true(any(grepl("reported optimum may be on a boundary or not a true interior maximum", warnings)))
expect_true(any(grepl("profile-t approximation may be unreliable", warnings)))
expect_identical(result$conf.status, "optimizer_mismatch")
mle_index <- which.min(abs(result$profile_value - fixture$target$estimate))
expect_equal(result$deviance[[mle_index]], 0, tolerance = 1e-12)
expect_lt(min(result$deviance, na.rm = TRUE), -1e-3)
})
test_that("inner profile re-optimization failures become NA and warn", {
fixture <- profile_warning_fixture(function(theta) {
if (abs(theta) > 0.1) return(NA_real_)
theta^2
})
warnings <- character()
withCallingHandlers(
result <- tls_profile_ci_curve(
fixture$fit, fixture$target, 0.95, 31, FALSE
),
warning = function(w) {
warnings <<- c(warnings, conditionMessage(w))
invokeRestart("muffleWarning")
}
)
expect_true(any(grepl("Inner re-optimisation did not converge", warnings)))
expect_true(anyNA(result$deviance))
expect_identical(result$conf.status, "open_both")
})
test_that("the profile deviance is ~0 at the MLE", {
fit <- fit_binom()
pc <- suppressWarnings(profile(fit, "CTmax"))
expect_lt(abs(min(pc$deviance, na.rm = TRUE)), 1e-4)
pz <- suppressWarnings(profile(fit, "z"))
expect_lt(abs(min(pz$deviance, na.rm = TRUE)), 1e-4)
})
test_that("CTmax and z profile CIs are finite, closed, and bracket the estimate", {
fit <- fit_binom()
ciC <- suppressWarnings(confint(fit, "CTmax", 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(fit, "z", 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)
# z is strictly positive.
expect_gt(ciZ$conf.low, 0)
})
test_that("the z profile CI equals exp() of the log_z profile CI (equivariance)", {
fit <- fit_binom()
ciZ <- suppressWarnings(confint(fit, "z", method = "profile"))
ciLz <- suppressWarnings(confint(fit, "log_z", method = "profile"))
expect_equal(
c(ciZ$conf.low, ciZ$conf.high),
exp(c(ciLz$conf.low, ciLz$conf.high)),
tolerance = 1e-6
)
})
test_that("profile CIs may be asymmetric and we do not symmetrise them", {
# The phi profile on a beta-binomial fit is markedly asymmetric on the natural
# scale (heavier upper tail), which the profile should preserve.
d <- simulate_tls(family = "beta_binomial", CTmax = 36, z = 3, phi = 50,
seed = 1)
fit <- suppressWarnings(
fit_tls(d, y = survived, n = total, time = duration, temp = temp,
family = "beta_binomial", tref = 1)
)
ci <- suppressWarnings(confint(fit, "phi", method = "profile"))
expect_true(is.finite(ci$conf.low) && is.finite(ci$conf.high))
lower_gap <- ci$estimate - ci$conf.low
upper_gap <- ci$conf.high - ci$estimate
# Not symmetric: the two half-widths differ substantially.
expect_false(isTRUE(all.equal(lower_gap, upper_gap, tolerance = 0.05)))
})
test_that("a sparse / degenerate design warns and returns NA without crashing", {
ds <- simulate_tls(family = "binomial", temps = c(35, 36), times = c(1, 2),
reps = 2, n = 10, CTmax = 36, z = 4, seed = 9)
fs <- suppressWarnings(
fit_tls(ds, y = survived, n = total, time = duration, temp = temp,
family = "binomial", tref = 1)
)
# fallback = FALSE keeps the strict profile behaviour (the parametric-bootstrap
# fallback is exercised in test-bootstrap.R).
expect_warning(
confint(fs, "z", method = "profile", fallback = FALSE),
"did not close|weakly identified"
)
ci <- suppressWarnings(confint(fs, "z", method = "profile", fallback = FALSE))
# An open side is NA (never a fabricated bound) and the status records it.
expect_true(is.na(ci$conf.low) || is.na(ci$conf.high))
expect_match(ci$conf.status, "open")
eye <- suppressWarnings(
plot_confidence_eye(
fs, parm = "z", method = "profile", raw_data = FALSE,
fallback = FALSE
)
)
layer_classes <- vapply(
eye$layers,
function(layer) class(layer$geom)[1L],
character(1)
)
expect_false(any(layer_classes == "GeomRibbon"))
expect_true(any(layer_classes == "GeomPoint"))
expect_match(eye$labels$subtitle, "hollow points only|without a lens")
})
test_that("up uses the delta/Wald fallback with a message and no profile curve", {
fit <- fit_binom()
expect_message(
suppressWarnings(confint(fit, "up", method = "profile")),
"delta-method Wald"
)
ci <- suppressMessages(suppressWarnings(confint(fit, "up", method = "profile")))
expect_identical(ci$method, "wald")
expect_identical(ci$conf.status, "wald_fallback")
expect_true(is.finite(ci$conf.low) && is.finite(ci$conf.high))
})
test_that("plot.profile_tls_profile returns a ggplot with confidence wording", {
skip_if_not_installed("ggplot2")
fit <- fit_binom()
pc <- suppressWarnings(profile(fit, "CTmax"))
p <- plot(pc)
expect_s3_class(p, "ggplot")
# Confidence-Eye contract: confidence language, never posterior.
expect_match(p$labels$caption, "confidence")
expect_false(grepl("posterior|credible", tolower(p$labels$caption)))
})
test_that("the default Confidence Eye is minimal, scaled, and rug-free", {
skip_if_not_installed("ggplot2")
eye <- suppressWarnings(plot_confidence_eye(fit_binom(), method = "wald"))
layer_classes <- vapply(eye$layers, function(layer) class(layer$geom)[1L],
character(1))
expect_s3_class(eye, "ggplot")
expect_null(eye$labels$title)
expect_null(eye$labels$subtitle)
expect_false(any(layer_classes == "GeomRug"))
expect_true(any(layer_classes == "GeomRibbon"))
expect_gte(sum(layer_classes == "GeomPoint"), 2L)
expect_identical(eye$facet$params$free$x, TRUE)
expect_match(eye$labels$caption, "confidence")
expect_false(grepl("posterior|credible", eye$labels$caption, ignore.case = TRUE))
})
test_that("the Confidence Eye flags finite irregular profile intervals", {
fit <- fit_binom()
testthat::local_mocked_bindings(
confint = function(object, parm, ...) {
tibble::tibble(
parameter = parm, conf.low = 35, conf.high = 37, estimate = 36,
level = 0.95, method = "profile", scale = "identity",
conf.status = "non_monotone"
)
},
.package = "freqTLS",
.env = parent.frame()
)
eye <- plot_confidence_eye(fit, parm = "CTmax", method = "profile")
expect_identical(as.character(eye$data$.reliability),
"profile needs inspection")
expect_match(eye$labels$subtitle, "non-success profile status")
})
test_that("the Confidence Eye does not label an invalid center as open", {
fit <- fit_binom()
testthat::local_mocked_bindings(
confint = function(object, parm, ...) {
tibble::tibble(
parameter = parm, conf.low = NA_real_, conf.high = NA_real_, estimate = 36,
level = 0.95, method = "profile", scale = "identity",
conf.status = "invalid_center"
)
},
.package = "freqTLS",
.env = parent.frame()
)
eye <- plot_confidence_eye(fit, parm = "CTmax", method = "profile")
expect_identical(as.character(eye$data$.reliability),
"profile needs inspection")
expect_match(eye$labels$subtitle, "Profile needs inspection")
expect_false(grepl("Profile open", eye$labels$subtitle))
})
test_that("tidy_parameters(method = 'profile') fills the 8-column shape", {
fit <- fit_binom()
# tidy over all params profiles `up` too, which emits an info message.
tp <- suppressMessages(suppressWarnings(tidy_parameters(fit, method = "profile")))
expect_identical(
names(tp),
c("parameter", "group", "estimate", "std.error",
"conf.low", "conf.high", "interval_type", "scale")
)
# Profile for all rows except `up`, which is honestly labelled Wald/delta
# (no single internal coordinate under the nested-gap reparameterisation).
expect_true(all(tp$interval_type[tp$parameter != "up"] == "profile"))
expect_identical(tp$interval_type[tp$parameter == "up"], "wald")
# CTmax and z rows are finite and bracket the estimate.
for (p in c("CTmax", "z")) {
row <- tp[tp$parameter == p, ]
expect_true(is.finite(row$conf.low) && is.finite(row$conf.high))
expect_lt(row$conf.low, row$estimate)
expect_gt(row$conf.high, row$estimate)
}
})
test_that("parameter getters pass profile intervals through without changing rows", {
fit <- fit_binom()
ct <- suppressMessages(suppressWarnings(get_ctmax(fit, method = "profile")))
zz <- suppressMessages(suppressWarnings(get_z(fit, method = "profile")))
sh <- suppressMessages(suppressWarnings(get_shape(fit, method = "profile")))
expect_true(all(ct$interval_type == "profile"))
expect_true(all(zz$interval_type == "profile"))
expect_true(all(sh$parameter %in% c("low", "up", "k", "phi")))
expect_identical(sh$interval_type[sh$parameter == "up"], "wald")
})
test_that("check_tls fires data-adequacy warnings on a sparse fit", {
ds <- simulate_tls(family = "binomial", temps = c(35, 36), times = c(1, 2),
reps = 2, n = 10, CTmax = 36, z = 4, seed = 9)
fs <- suppressWarnings(
fit_tls(ds, y = survived, n = total, time = duration, temp = temp,
family = "binomial", tref = 1)
)
# check_tls emits several warnings; collect them all and assert on the codes.
codes <- character(0)
msgs <- character(0)
withCallingHandlers(
codes <- check_tls(fs),
warning = function(w) {
msgs <<- c(msgs, conditionMessage(w))
invokeRestart("muffleWarning")
}
)
expect_true(any(grepl("Fewer than 3 unique temperatures", msgs)))
expect_true("temps" %in% codes)
expect_true("durations" %in% codes)
})
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.