R/methods.R

Defines functions print.summary.profile_tls summary.profile_tls print.profile_tls tls_fit_print_details

Documented in print.profile_tls print.summary.profile_tls summary.profile_tls

#' S3 methods for fitted freqTLS models
#'
#' Standard extractor and display methods for the `profile_tls` object returned
#' by [fit_tls()]: `print`, `summary`, `coef`, `vcov`, `logLik`, `AIC`, and
#' `nobs`. They mirror the drmTMB method idioms
#' (`drmTMB::R/methods.R:2-40,1826-1864,2025-2037`).
#'
#' @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, quiet = TRUE
#' )
#' coef(fit)
#' logLik(fit)
#' AIC(fit)
#' nobs(fit)
#' @name profile_tls-methods
#' @keywords internal
NULL

tls_fit_print_details <- function(x, digits = 4) {
  data_inputs <- x$tmb_inputs$data
  details <- character()
  fixed_parts <- c(
    if (identical(as.integer(data_inputs$fixed_low), 1L))
      paste0("low = ", tls_fmt(data_inputs$fixed_low_value, digits)),
    if (identical(as.integer(data_inputs$fixed_up), 1L))
      paste0("up = ", tls_fmt(data_inputs$fixed_up_value, digits))
  )
  if (length(fixed_parts)) {
    details <- c(details, paste0("Fixed asymptotes: ",
                                 paste(fixed_parts, collapse = "; ")))
  }
  ds <- x$data_summary
  if (identical(x$family$family, "beta") &&
      isTRUE(ds$n_left_censored > 0L)) {
    limits <- x$diag_data$censor_limit[x$diag_data$left_censored]
    details <- c(details, paste0(
      "Left-censored Beta rows: ", ds$n_left_censored,
      "; detection limits: ", tls_fmt(min(limits), digits), " to ",
      tls_fmt(max(limits), digits)
    ))
  }
  details
}

#' @describeIn profile_tls-methods Print a compact, readable summary: the call,
#'   the family, `tref`, the data summary, the natural-scale estimates table,
#'   and the convergence state.
#' @param x A `profile_tls` fit from [fit_tls()].
#' @param digits Number of significant digits for the estimates table.
#' @param ... Ignored.
#' @return `print` and `summary` return their input invisibly / the summary
#'   object; the extractors return the quantities named in their titles.
#' @export
print.profile_tls <- function(x, digits = 4, ...) {
  cli::cli_text("<freqTLS {x$family$family} {x$curve} fit>")
  cli::cli_text("Call: {paste(deparse(x$call), collapse = ' ')}")

  ds <- x$data_summary
  group_txt <- if (isTRUE(ds$grouped)) {
    paste0(ds$n_groups, " group", if (ds$n_groups != 1L) "s" else "",
           " (", paste(x$group_levels, collapse = ", "), ")")
  } else {
    "ungrouped"
  }
  cli::cli_text("Reference time (tref): {x$tref}  |  CTmax defined at this time")
  cli::cli_text(
    "Data: {ds$n_obs} observation{?s}, {group_txt}; ",
    "{ds$n_temps} temperature{?s} in [{tls_fmt(ds$temp_range[1], digits)}, ",
    "{tls_fmt(ds$temp_range[2], digits)}], ",
    "{ds$n_times} duration{?s} in [{tls_fmt(ds$time_range[1], digits)}, ",
    "{tls_fmt(ds$time_range[2], digits)}]"
  )
  if (identical(x$family$family, "beta")) {
    if (isTRUE(ds$n_left_censored > 0L)) {
      if (ds$n_exact > 0L) {
        cli::cli_text(
          "      Exact-row mean proportion {tls_fmt(ds$mean_exact_proportion, digits)} from {ds$n_exact} rows; {ds$n_left_censored} left-censored rows"
        )
      } else {
        cli::cli_text(
          "      {ds$n_left_censored} left-censored rows; no exact-row mean proportion"
        )
      }
    } else {
      cli::cli_text(
        "      mean observed proportion {tls_fmt(ds$total_successes / ds$n_obs, digits)}"
      )
    }
  } else {
    cli::cli_text(
      "      {ds$total_successes} survivor{?s} of {ds$total_trials} trial{?s}"
    )
  }
  for (detail in tls_fit_print_details(x, digits)) {
    cli::cli_text("{detail}")
  }
  for (blk in tls_re_blocks(x)) {
    cli::cli_text(
      "Random intercept on {blk$param}: (1 | {blk$spec$group_var}), {blk$spec$n} group{?s}"
    )
  }

  cat("\n")
  est <- x$estimates
  out <- data.frame(
    parameter = est$parameter,
    group = ifelse(is.na(est$group), "", est$group),
    estimate = signif(est$estimate, digits),
    std.error = signif(est$std.error, digits),
    stringsAsFactors = FALSE
  )
  print(out, row.names = FALSE)

  cat("\n")
  cv <- x$convergence
  ok <- identical(cv$code, 0L) && isTRUE(cv$pdHess)
  status <- if (ok) "converged (pdHess)" else "CHECK CONVERGENCE"
  cli::cli_text(
    "Optimiser: {cv$optimizer} | code {cv$code} | pdHess {cv$pdHess} | {status}"
  )
  if (!is.na(cv$message)) cli::cli_text("Message: {cv$message}")
  cli::cli_text(
    "logLik {tls_fmt(x$logLik, digits)} | df {x$df} | AIC {tls_fmt(x$AIC, digits)}"
  )
  invisible(x)
}

#' @describeIn profile_tls-methods Build a `summary.profile_tls` object carrying
#'   the estimates table (with Wald z-statistics and p-values), the family,
#'   `tref`, the data summary, and the convergence state.
#' @param object A `profile_tls` fit from [fit_tls()].
#' @export
summary.profile_tls <- function(object, ...) {
  est <- object$estimates
  scales <- rep("natural", nrow(est))
  shape_specs <- list(
    low = list(matrix = object$tmb_inputs$data$X_low, link = "logit"),
    up = list(matrix = object$tmb_inputs$data$X_up, link = "logit"),
    k = list(matrix = object$tmb_inputs$data$X_logk, link = "log")
  )
  for (natural in names(shape_specs)) {
    spec <- shape_specs[[natural]]
    if (!tls_is_constant_design(spec$matrix) && !tls_is_onehot(spec$matrix)) {
      rows <- startsWith(est$parameter, paste0(natural, ":"))
      scales[rows] <- paste0(spec$link, " link")
    }
  }
  z <- est$estimate / est$std.error
  pval <- 2 * stats::pnorm(abs(z), lower.tail = FALSE)
  fixed_rows <- vapply(est$parameter, function(parameter) {
    !is.null(tls_fixed_asymptote_value(object, parameter))
  }, logical(1))
  scales[fixed_rows] <- "fixed (natural)"
  standard_error <- est$std.error
  standard_error[fixed_rows] <- NA_real_
  z[fixed_rows] <- NA_real_
  pval[fixed_rows] <- NA_real_
  coefficients <- data.frame(
    parameter = est$parameter,
    group = est$group,
    scale = scales,
    estimate = est$estimate,
    std.error = standard_error,
    statistic = z,
    p.value = pval,
    stringsAsFactors = FALSE
  )
  structure(
    list(
      call = object$call,
      family = object$family,
      curve = object$curve,
      tref = object$tref,
      group_levels = object$group_levels,
      data_summary = object$data_summary,
      print_details = tls_fit_print_details(object),
      coefficients = coefficients,
      logLik = object$logLik,
      df = object$df,
      AIC = object$AIC,
      convergence = object$convergence
    ),
    class = "summary.profile_tls"
  )
}

#' @describeIn profile_tls-methods Print a `summary.profile_tls` object.
#' @export
print.summary.profile_tls <- function(x, digits = 4, ...) {
  cli::cli_text("<freqTLS {x$family$family} {x$curve} fit>  summary")
  cli::cli_text("Call: {paste(deparse(x$call), collapse = ' ')}")

  ds <- x$data_summary
  group_txt <- if (isTRUE(ds$grouped)) {
    paste0(ds$n_groups, " group", if (ds$n_groups != 1L) "s" else "",
           " (", paste(x$group_levels, collapse = ", "), ")")
  } else {
    "ungrouped"
  }
  cli::cli_text("Reference time (tref): {x$tref}  |  family: {x$family$family}")
  cli::cli_text("Data: {ds$n_obs} observation{?s}, {group_txt}")
  if (!is.null(x$model_formulas)) {
    cli::cli_text("Shape formulas: low {x$model_formulas[['low']]}; up {x$model_formulas[['up']]}; k {x$model_formulas[['k']]}")
  }
  for (detail in x$print_details %||% character()) {
    cli::cli_text("{detail}")
  }

  cat("\nCoefficients (scale shown per row; Wald z-test):\n")
  co <- x$coefficients
  out <- data.frame(
    parameter = co$parameter,
    group = ifelse(is.na(co$group), "", co$group),
    scale = co$scale,
    estimate = signif(co$estimate, digits),
    std.error = signif(co$std.error, digits),
    `z value` = signif(co$statistic, digits),
    `Pr(>|z|)` = signif(co$p.value, digits),
    check.names = FALSE,
    stringsAsFactors = FALSE
  )
  print(out, row.names = FALSE)

  cat("\n")
  cv <- x$convergence
  ok <- identical(cv$code, 0L) && isTRUE(cv$pdHess)
  status <- if (ok) "converged (pdHess)" else "CHECK CONVERGENCE"
  cli::cli_text(
    "Optimiser: {cv$optimizer} | code {cv$code} | pdHess {cv$pdHess} | {status}"
  )
  cli::cli_text(
    "logLik {tls_fmt(x$logLik, digits)} | df {x$df} | AIC {tls_fmt(x$AIC, digits)}"
  )
  invisible(x)
}

#' @describeIn profile_tls-methods Extract natural-scale point estimates. With
#'   `complete = FALSE` (default) a named numeric vector; with `complete = TRUE`
#'   the full `estimates` data frame (parameter, group, estimate, std.error).
#' @param complete Logical; return the full estimates data frame instead of a
#'   named vector.
#' @export
coef.profile_tls <- function(object, complete = FALSE, ...) {
  est <- object$estimates
  if (isTRUE(complete)) {
    return(est)
  }
  stats::setNames(est$estimate, est$parameter)
}

#' @describeIn profile_tls-methods The variance-covariance matrix of the
#'   internal (unconstrained) coordinates, from [TMB::sdreport()]. Returns
#'   `NULL` (with a warning) when the `sdreport` did not produce a covariance.
#' @export
vcov.profile_tls <- function(object, ...) {
  vc <- object$vcov
  if (is.null(vc)) {
    cli::cli_warn(c(
      "The standard-error report (sdreport) did not produce a covariance.",
      i = "The Hessian was not positive-definite at the optimum; refit and check convergence."
    ))
    return(NULL)
  }
  vc
}

#' @describeIn profile_tls-methods The maximised log-likelihood, as a `logLik`
#'   object with `df` and `nobs` attributes so [stats::AIC()] and
#'   [stats::BIC()] work.
#' @export
logLik.profile_tls <- function(object, ...) {
  val <- object$logLik
  attr(val, "df") <- object$df
  attr(val, "nobs") <- object$data_summary$n_obs
  class(val) <- "logLik"
  val
}

#' @describeIn profile_tls-methods Akaike's An Information Criterion. With the
#'   default `k = 2` this returns the stored `AIC`; other `k` are computed from
#'   the log-likelihood and `df`.
#' @param k Penalty per parameter (default `2`, giving the AIC).
#' @export
AIC.profile_tls <- function(object, ..., k = 2) {
  if (isTRUE(all.equal(k, 2))) {
    return(object$AIC)
  }
  -2 * object$logLik + k * object$df
}

#' @describeIn profile_tls-methods The number of observations (temperature-by-
#'   duration cells) used in the fit.
#' @export
nobs.profile_tls <- function(object, ...) {
  object$data_summary$n_obs
}

#' Random-effect BLUPs (conditional modes) for a freqTLS fit
#'
#' `ranef()` returns the predicted random intercepts (the conditional modes /
#' BLUPs) with their conditional standard errors, for a fit with a random
#' intercept on any of `CTmax`, `log_z`, `low`, or `log_k`
#' (`<param> ~ <fixed> + (1 | group)`). It errors for a fixed-effects-only fit.
#' Each BLUP is a deviation on its coordinate's internal scale: `CTmax` in degrees
#' C, `log_z` on `log(z)`, `low` on `logit(low)`, `log_k` on `log(k)`. When several
#' REs are present the rows are stacked in `CTmax`, `log_z`, `low`, `log_k` order.
#'
#' @param object A `profile_tls` fit from [fit_tls()] with a random intercept.
#' @param ... Reserved; must be empty.
#' @return A [tibble][tibble::tibble] with one row per group level (per RE term):
#'   `group`, `term` (`"CTmax"`, `"log_z"`, `"low"`, or `"log_k"`), `estimate` (the
#'   BLUP), and `std.error` (the conditional SE).
#' @examples
#' d <- simulate_tls(family = "binomial", CTmax = 36, z = 4,
#'                   times = c(30, 60, 120, 240, 480), tref = 60,
#'                   re_sd = 1.5, n_re_groups = 12, seed = 42)
#' fit <- fit_tls(
#'   tls_bf(survived | trials(total) ~ time(duration) + temp(temp),
#'          CTmax ~ 1 + (1 | colony)),
#'   data = d, family = "binomial", tref = 60)
#' ranef(fit)
#' @export
ranef <- function(object, ...) {
  UseMethod("ranef")
}

#' @rdname ranef
#' @export
ranef.profile_tls <- function(object, ...) {
  dots <- list(...)
  if (length(dots) > 0L) {
    cli::cli_abort("{.arg ...} is reserved; pass only documented arguments.")
  }
  blocks <- tls_re_blocks(object)
  if (length(blocks) == 0L) {
    cli::cli_abort(c(
      "This fit has no random effects.",
      i = "Add a random intercept on `CTmax`, `log_z`, `low`, or `log_k`, e.g. {.code CTmax ~ <fixed> + (1 | group)}."
    ))
  }
  sdr <- object$sdreport
  if (is.null(sdr)) {
    cli::cli_abort(c(
      "The standard-error report (sdreport) is unavailable for this fit.",
      i = "Refit and check convergence (pdHess)."
    ))
  }
  sm <- summary(sdr, select = "random")
  parts <- lapply(blocks, function(bk) {
    rows <- which(rownames(sm) == bk$b_name)
    rows <- rows[bk$b_idx]
    if (length(rows) != bk$spec$n) {
      cli::cli_abort("Internal: expected {bk$spec$n} {bk$term} random intercept{?s}, found {length(rows)}.")
    }
    tibble::tibble(
      group = bk$spec$group_levels,
      term = bk$term,
      estimate = unname(sm[rows, "Estimate"]),
      std.error = unname(sm[rows, "Std. Error"])
    )
  })
  tibble::as_tibble(do.call(rbind, parts))
}

#' Format a numeric scalar for printed method output
#'
#' @param x Numeric scalar (or `NA`).
#' @param digits Significant digits.
#' @return A length-1 character string.
#' @keywords internal
#' @noRd
tls_fmt <- function(x, digits = 4) {
  if (length(x) == 0L || is.na(x)) return("NA")
  format(signif(x, digits), trim = TRUE)
}

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.