R/diagnostics.R

Defines functions check_tls check_tls_data

Documented in check_tls

#' Identifiability and data-adequacy diagnostics for thermal-load-sensitivity fits
#'
#' freqTLS emits explicit identifiability warnings rather than letting weak
#' data quietly produce confident-looking estimates. This is the package's
#' clearest value-add over the Bayesian path. The diagnostic contract splits into
#' two groups:
#'
#' * **Data-adequacy (1-8)** depend only on the data and design and are checked by
#'   `check_tls_data()`, which [fit_tls()] runs before optimising. They are also
#'   surfaced after the fact by [check_tls()].
#' * **Profile-geometry (9-12)** depend on the profile likelihood and are emitted
#'   by the profiling code in `R/profile.R` / `R/confint.R`.
#'
#' All warnings use [cli::cli_warn()] so they can be caught with
#' [withCallingHandlers()] / [tryCatch()] and silenced with
#' [suppressWarnings()].
#'
#' @name tls-diagnostics
#' @keywords internal
NULL

#' Run the data-adequacy diagnostics (warnings 1-8)
#'
#' Inspects the raw data and design and emits a [cli::cli_warn()] for each
#' data-adequacy concern it finds. Called by [fit_tls()]
#' on the assembled vectors; also callable directly. Returns a character vector
#' of the warning codes that fired (invisibly when called for its side effects),
#' which [check_tls()] reuses.
#'
#' @param y Numeric vector of successes (survivors).
#' @param n Numeric vector of trials.
#' @param time Numeric vector of exposure durations in minutes.
#' @param temp Numeric vector of assay temperatures.
#' @param group Optional grouping vector (or `NULL`).
#' @param ctmax Optional numeric vector of fitted `CTmax` value(s); when supplied,
#'   the `CTmax`-extrapolation check (item 7) is evaluated. `fit_tls()` does not
#'   pass this (the fit is not yet available), so item 7 is surfaced post-fit by
#'   [check_tls()]. Values aligned to observations are checked within each
#'   fitted group when group labels are available.
#' @param left_censored Optional logical vector identifying observations that
#'   contribute a censored likelihood rather than an exact response density.
#' @param censor_limit Optional numeric vector of upper bounds for the left-
#'   censored response proportions.
#' @param family_code Integer family code (0 binomial, 1 beta-binomial,
#'   2 Beta); the large-`phi` warning is beta-binomial-specific.
#' @param phi Optional fitted `phi` (item 8, beta-binomial only); surfaced post-fit
#'   by [check_tls()].
#' @return Invisibly, a character vector of the warning codes that fired.
#' @keywords internal
#' @noRd
check_tls_data <- function(y, n, time, temp, group = NULL,
                           ctmax = NULL, phi = NULL,
                           left_censored = NULL, censor_limit = NULL,
                           family_code = NULL) {
  fired <- character(0)
  warn <- function(code, ...) {
    fired <<- c(fired, code)
    cli::cli_warn(...)
  }

  # 1. Fewer than 3 unique temperatures: CTmax / z slope is barely identified.
  n_temp <- length(unique(temp))
  if (n_temp < 3L) {
    warn("temps", c(
      "Fewer than 3 unique temperatures ({n_temp} found).",
      i = "CTmax and z are determined by how the survival midpoint shifts with temperature; with < 3 temperatures the slope is weakly identified.",
      i = "Add at least one distinct assay temperature and span the survival transition before interpreting CTmax or z; otherwise report the design limitation and expect wide or open intervals."
    ))
  }

  # 2. Fewer than 3 unique durations, overall and within any temperature.
  n_time <- length(unique(time))
  if (n_time < 3L) {
    warn("durations", c(
      "Fewer than 3 unique durations ({n_time} found) overall.",
      i = "The within-temperature dose-response (low, up, k) is weakly identified with < 3 durations.",
      i = "Add durations on both sides of the survival transition at each temperature; otherwise avoid interpreting the shape parameters."
    ))
  } else {
    per_temp <- tapply(time, temp, function(tt) length(unique(tt)))
    thin <- names(per_temp)[per_temp < 3L]
    if (length(thin) > 0L) {
      warn("durations_per_temp", c(
        "Fewer than 3 unique durations at {cli::qty(length(thin))} temperature{?s} {.val {thin}}.",
        i = "The dose-response curve is weakly identified at those temperatures.",
        i = "Add durations at the listed temperatures so each has at least three distinct exposure times spanning the transition."
      ))
    }
  }

  is_exact <- if (is.null(left_censored)) rep(TRUE, length(y)) else !left_censored
  has_censoring <- !is.null(left_censored) && any(left_censored)
  censor_upper <- if (has_censoring && !is.null(censor_limit)) {
    censor_limit[left_censored]
  } else numeric(0)
  y_exact <- y[is_exact]
  n_exact <- n[is_exact]
  prop_exact <- y_exact / n_exact
  group_rows <- if (is.null(group)) {
    list(`all data` = seq_along(y))
  } else {
    split(seq_along(y), as.character(group), drop = TRUE)
  }

  if (!is.null(left_censored) && length(y) > 0L &&
      length(left_censored) == length(y) && all(left_censored)) {
    warn("all_left_censored", c(
      "Every observation is left-censored; the assay contains no exact response values.",
      i = "The response distribution and its thermal midpoint cannot be checked against observed values.",
      i = "Lower the detection limit or extend the assay until at least some responses are observed exactly, then refit."
    ))
  }
  if (!is.null(group) && length(group) == length(y) && has_censoring &&
      length(left_censored) == length(y) && !all(left_censored)) {
    all_censored_groups <- names(group_rows)[vapply(group_rows, function(rows) {
      isTRUE(all(left_censored[rows]))
    }, logical(1L))]
    if (length(all_censored_groups)) {
      warn("all_left_censored_group", c(
        "All responses in {cli::qty(length(all_censored_groups))} fitted group{?s} {.val {all_censored_groups}} are left-censored.",
        i = "That group's response distribution and thermal midpoint cannot be checked against exact values.",
        i = "Lower the detection limit or extend the assay for the listed group until some responses are observed exactly, then refit."
      ))
    }
  }

  # 3. No mortality anywhere: every individual survived. Censored observations
  #    do not establish an exact response at their placeholder value.
  if (!has_censoring && length(y_exact) > 0L && all(y_exact >= n_exact)) {
    warn("no_mortality", c(
      "No mortality anywhere: every individual survived in every cell.",
      i = "The lower asymptote and the temperature at which survival drops are not identified.",
      i = "Extend temperature or duration until some mortality is observed, then refit; CTmax and z cannot be recovered from the current data."
    ))
  }

  # 4. All mortality anywhere: every individual died.
  if (!has_censoring && length(y_exact) > 0L && all(y_exact <= 0)) {
    warn("all_mortality", c(
      "All mortality anywhere: no individual survived in any cell.",
      i = "The upper asymptote and the temperature at which survival drops are not identified.",
      i = "Include lower temperatures or shorter durations that retain some survivors, then refit."
    ))
  }

  # 5. Check the absolute survival threshold within each fitted group.
  #    The 4PL defines its relative midpoint halfway between low and up.
  if (length(y_exact) > 0L) {
    missing_threshold <- names(group_rows)[vapply(group_rows, function(rows) {
      exact_rows <- rows[is_exact[rows]]
      if (length(exact_rows) == 0L) return(FALSE)
      p <- y[exact_rows] / n[exact_rows]
      censored_rows <- rows[!is_exact[rows]]
      upper <- if (length(censored_rows) && !is.null(censor_limit)) {
        censor_limit[censored_rows]
      } else numeric(0)
      certain_below <- any(p < 0.5) || any(upper <= 0.5)
      certain_above <- any(p > 0.5)
      uncertain_side <- any(upper > 0.5)
      on_threshold <- any(p == 0.5)
      !uncertain_side && !on_threshold &&
        !(certain_below && certain_above) &&
        (all(p > 0.5) || all(p < 0.5))
    }, logical(1L))]
    if (length(missing_threshold) > 0L) {
      grouped <- !is.null(group)
      warn("threshold", c(
        if (grouped) {
          "The absolute 50% survival threshold is not crossed within {cli::qty(length(missing_threshold))} fitted group{?s} {.val {missing_threshold}}."
        } else {
          "The absolute 50% survival threshold is never crossed in the observed data."
        },
        i = "The 4PL midpoint occurs halfway between the lower and upper asymptotes.",
        i = "If absolute LT50 is your target, extend the temperature-duration design{if (grouped) ' within each listed group'} until observed survival lies on both sides of 0.5."
      ))
    }
  }

  # 6. An asymptote is never approached: survival never reaches near 0 or near 1,
  #    so the corresponding asymptote (low / up) is poorly determined.
  hi_candidates <- c(prop_exact, censor_upper)
  hi <- if (length(hi_candidates)) max(hi_candidates) else NA_real_
  lo <- if (length(prop_exact)) min(prop_exact) else NA_real_
  if (is.finite(hi) && hi < 0.8) {
    warn("up_not_approached", c(
      "The upper survival asymptote is never approached (max observed survival {round(hi, 2)} < 0.80).",
      i = "The upper asymptote `up` is weakly identified.",
      i = "Add milder conditions (lower temperature or shorter duration) that produce survival near 1, or avoid interpreting `up`."
    ))
  }
  if (!has_censoring && is.finite(lo) && lo > 0.2) {
    warn("low_not_approached", c(
      "The lower survival asymptote is never approached (min observed survival {round(lo, 2)} > 0.20).",
      i = "The lower asymptote `low` is weakly identified.",
      i = "Add harsher conditions (higher temperature or longer duration) that produce survival near 0, or avoid interpreting `low`."
    ))
  }

  # 7. CTmax extrapolated beyond the assayed temperatures (post-fit only).
  if (!is.null(ctmax)) {
    if (!is.null(group) && length(ctmax) == length(temp)) {
      outside_groups <- names(group_rows)[vapply(group_rows, function(rows) {
        tr <- range(temp[rows])
        any(ctmax[rows] < tr[1] | ctmax[rows] > tr[2])
      }, logical(1L))]
      if (length(outside_groups) > 0L) {
        warn("ctmax_extrapolated", c(
          "Fitted CTmax lies outside the assayed temperature range in {cli::qty(length(outside_groups))} fitted group{?s} {.val {outside_groups}}.",
          i = "Expand the assay range to bracket CTmax within each listed group, or report its CTmax as a temperature extrapolation."
        ))
      }
    } else {
      tr <- range(temp)
      out <- ctmax[ctmax < tr[1] | ctmax > tr[2]]
      if (length(out) > 0L) {
        out_range <- range(out)
        warn("ctmax_extrapolated", c(
          "{length(out)} fitted CTmax prediction{?s} lie outside the assayed temperature range [{round(tr[1], 2)}, {round(tr[2], 2)}] (outside range: {round(out_range[1], 2)} to {round(out_range[2], 2)}).",
          i = "Expand the assay range to bracket CTmax and refit; if that is impossible, report CTmax explicitly as an extrapolation and do not treat its interval as design-supported."
        ))
      }
    }
  }

  # 8. phi at the binomial limit (post-fit only): very large phi means the
  #    beta-binomial has collapsed to a binomial.
  if (identical(as.integer(family_code), 1L) &&
      !is.null(phi) && is.finite(phi) && phi > 1e4) {
    warn("phi_binomial_limit", c(
      "The overdispersion parameter phi ({signif(phi, 3)}) is at the binomial limit.",
      i = "The beta-binomial has effectively collapsed to a binomial; consider `family = \"binomial\"`."
    ))
  }

  invisible(fired)
}

#' Report identifiability diagnostics for a fitted model
#'
#' `check_tls()` re-runs the data-adequacy diagnostics
#' on a fitted `profile_tls` object, including the two post-fit checks that
#' [fit_tls()] cannot run before the model exists: whether any fitted `CTmax` is
#' extrapolated beyond the assayed temperatures (item 7), and whether `phi` has
#' reached the binomial limit (item 8). Each concern is emitted as a
#' [cli::cli_warn()]. Use it to audit a fit, or after [suppressWarnings()] around
#' [fit_tls()].
#'
#' The profile-geometry diagnostics (items 9-12) are emitted by [confint()] and
#' [profile()] when those are called, not here, because they require the profile
#' likelihood.
#'
#' @section Recovery guide:
#' The warning code returned by `check_tls()` identifies the next action:
#'
#' * `temps`: assay at least three distinct temperatures spanning the survival
#'   transition.
#' * `durations` / `durations_per_temp`: assay at least three distinct durations
#'   per temperature, with times on both sides of the transition.
#' * `no_mortality`: extend to hotter or longer exposures until mortality occurs.
#' * `all_mortality`: add cooler or shorter exposures that retain survivors.
#' * `all_left_censored`: lower the detection limit or improve assay sensitivity
#'   until at least some responses are observed exactly.
#' * `all_left_censored_group`: lower the detection limit or improve assay
#'   sensitivity in each listed fitted group until some responses are observed
#'   exactly.
#' * `threshold`: if absolute LT50 is the target, extend the design so that
#'   observed survival straddles 0.5 within each listed group. The relative
#'   4PL midpoint lies halfway between the fitted lower and upper asymptotes.
#' * `up_not_approached` / `low_not_approached`: add milder / harsher conditions
#'   approaching survival 1 / 0, or do not interpret that asymptote.
#' * `ctmax_extrapolated`: expand the assayed temperature range to bracket CTmax
#'   within each listed group, or report the affected estimates as temperature
#'   extrapolations.
#' * `phi_binomial_limit`: consider the simpler binomial family.
#'
#' After changing the design or family, refit and rerun `check_tls()`. For an
#' existing data set that cannot be augmented, use the warning to limit the
#' scientific claim; `vignette("profile-likelihood")` explains the strict
#' `fallback = FALSE` diagnostic. Fixed-effects fits can use a bootstrap fallback;
#' random-effects fits use Wald intervals when a profile does not close.
#'
#' @param fit A `profile_tls` fit from [fit_tls()], or a `freq_tls` workflow from
#'   [fit_4pl()].
#' @return Invisibly, a character vector of the diagnostic codes that fired.
#' @examples
#' d <- simulate_tls(family = "binomial", times = c(30, 60, 120, 240, 480),
#'                   CTmax = 36, z = 4, tref = 60, seed = 1)
#' fit <- fit_tls(d, y = survived, n = total, time = duration, temp = temp,
#'                family = "binomial", tref = 60)
#' codes <- check_tls(fit)
#' codes # character(0) means no data-adequacy diagnostic fired
#' @export
check_tls <- function(fit) {
  if (inherits(fit, "freq_tls")) fit <- fit$fit
  if (!inherits(fit, "profile_tls")) {
    cli::cli_abort("{.arg fit} must be a {.cls profile_tls} object from {.fn fit_tls} (or a {.cls freq_tls} workflow from {.fn fit_4pl}).")
  }
  d <- fit$diag_data
  if (is.null(d)) {
    cli::cli_abort(c(
      "This fit does not carry the data needed for diagnostics.",
      i = "Refit with the current version of {.fn fit_tls}."
    ))
  }
  # A coefficient in a contrast-coded CTmax formula is not itself a cell
  # estimate. Apply the fitted coefficients in design-matrix order so this
  # check uses the direct CTmax values implied by each fitted row. The names
  # in the display table can collapse a one-column design to `CTmax`, so the
  # optimizer parameter names and a dimension check are the reliable mapping.
  X_CT <- fit$tmb_inputs$data$X_CT
  beta_CT <- fit$par[names(fit$par) == "beta_CT"]
  ctmax <- if (!is.null(X_CT) && length(beta_CT) == ncol(X_CT)) {
    as.numeric(X_CT %*% beta_CT)
  } else {
    fit$estimates$estimate[startsWith(fit$estimates$parameter, "CTmax")]
  }
  phi <- if (fit$family$family_code >= 1L) {
    fit$estimates$estimate[fit$estimates$parameter == "phi"]
  } else {
    NULL
  }
  codes <- check_tls_data(
    y = d$y, n = d$n, time = d$time, temp = d$temp, group = d$group,
    ctmax = ctmax, phi = phi, left_censored = d$left_censored,
    censor_limit = d$censor_limit, family_code = fit$family$family_code
  )
  if (!isTRUE(fit$convergence$pdHess)) {
    cli::cli_warn(c(
      "The fitted Hessian is not positive definite even though optimizer convergence may be reported.",
      i = "Standard errors and Wald confidence intervals are unreliable; inspect the profile or refit with a better-supported design."
    ), .frequency = "regularly", .frequency_id = "freqtls-non-pd-hessian")
    codes <- c(codes, "non_pd_hessian")
  }
  invisible(codes)
}

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.