R/extract.R

Defines functions tls_wald_natural tls_report_scale tls_param_scale get_shape get_z get_ctmax tidy_parameters

Documented in get_ctmax get_shape get_z tidy_parameters

#' Tidy the parameters of a fitted freqTLS model
#'
#' `tidy_parameters()` returns a broom-style tibble with one row for each
#' fitted parameter or fixed-effect coefficient and optional Wald confidence
#' intervals. Constant and one-hot shape designs report natural-scale shape
#' estimates. General shape designs report their regression coefficients on the
#' fitted link scale because a slope has no natural-scale back-transform. Wald
#' intervals for natural-scale parameters are computed on the internal (link) scale as
#' `estimate +/- z * std.error` and then back-transformed to the natural scale,
#' so they respect each parameter's bounds (for example `z > 0`, `0 < low < up`)
#' and are equivariant under the link. For `CTmax` (identity link) this is the
#' usual symmetric Wald interval.
#'
#' With `method = "profile"` the intervals are profile-likelihood confidence
#' intervals (see [confint.profile_tls()]); with `method = "wald"` (default) they
#' are back-transformed for natural-scale parameters and remain on the fitted
#' link scale for general shape coefficients. The returned shape is identical;
#' only `interval_type` and the interval values differ. A profile that
#' does not close returns `NA` on the open side (never a fabricated bound).
#'
#' @param fit A `profile_tls` fit from [fit_tls()].
#' @param conf.int Logical; include `conf.low` / `conf.high` columns (default
#'   `TRUE`).
#' @param conf.level Confidence level for the interval (default `0.95`).
#' @param method Either `"wald"` (default) or `"profile"`.
#'
#' @return A [tibble][tibble::tibble] with one row per fitted parameter and
#'   columns `parameter`, `group`, `estimate`, `std.error`,
#'   `conf.low`, `conf.high`, `interval_type`, and `scale`. `scale` describes
#'   displayed values: `"natural"` for response-scale parameters, or the fitted
#'   link (`"identity"`, `"log"`, or `"logit"`) for general coefficients.
#'   General `low`, `up`, and `k` coefficients and their Wald limits are both on
#'   the fitted link scale. Covariate-model `z` rows are exponentiated
#'   multipliers and are reported on the natural scale.
#'   `interval_type` is `"wald"`, `"profile"`, `"fixed"` for exact fitted
#'   asymptotes, or `"none"` when confidence intervals were not requested.
#'
#' @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)
#' tidy_parameters(fit)
#' tidy_parameters(fit, method = "profile")
#'
#' @export
tidy_parameters <- function(fit, conf.int = TRUE, conf.level = 0.95,
                            method = c("wald", "profile")) {
  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}.")
  }
  if (!is.numeric(conf.level) || length(conf.level) != 1L ||
      conf.level <= 0 || conf.level >= 1) {
    cli::cli_abort("{.arg conf.level} must be a single number in (0, 1).")
  }
  method <- match.arg(method)

  est <- fit$estimates
  scale <- tls_report_scale(est$parameter, fit)

  conf.low <- rep(NA_real_, nrow(est))
  conf.high <- rep(NA_real_, nrow(est))
  interval_type <- rep(if (isTRUE(conf.int)) method else "none", nrow(est))

  if (isTRUE(conf.int) && identical(method, "wald")) {
    wald <- tls_wald_natural(fit, conf.level)
    idx <- match(est$parameter, wald$parameter)
    conf.low <- wald$conf.low[idx]
    conf.high <- wald$conf.high[idx]
  } else if (isTRUE(conf.int) && identical(method, "profile")) {
    ci <- confint(fit, parm = est$parameter, level = conf.level, method = "profile")
    idx <- match(est$parameter, ci$parameter)
    conf.low <- ci$conf.low[idx]
    conf.high <- ci$conf.high[idx]
    # Per-row honesty: `up` falls back to Wald/delta even under method="profile".
    interval_type <- ci$method[idx]
  }

  if (isTRUE(conf.int)) {
    fixed_value <- vapply(est$parameter, function(p) {
      value <- tls_fixed_asymptote_value(fit, p)
      if (is.null(value)) NA_real_ else value
    }, numeric(1L))
    fixed_rows <- is.finite(fixed_value)
    conf.low[fixed_rows] <- fixed_value[fixed_rows]
    conf.high[fixed_rows] <- fixed_value[fixed_rows]
    interval_type[fixed_rows] <- "fixed"
  }

  tibble::tibble(
    parameter = est$parameter,
    group = est$group,
    estimate = est$estimate,
    std.error = est$std.error,
    conf.low = conf.low,
    conf.high = conf.high,
    interval_type = interval_type,
    scale = scale
  )
}

#' Extract the CTmax estimate(s)
#'
#' @param fit A `profile_tls` fit from [fit_tls()].
#' @param conf.int Logical; include confidence-interval columns (default
#'   `TRUE`).
#' @param conf.level Confidence level for the interval (default `0.95`).
#' @param method Either `"wald"` (default) or `"profile"`.
#' @return A [tibble][tibble::tibble] of the `CTmax` row(s) from
#'   [tidy_parameters()].
#' @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)
#' get_ctmax(fit)
#' @export
get_ctmax <- function(fit, conf.int = TRUE, conf.level = 0.95,
                      method = c("wald", "profile")) {
  method <- match.arg(method)
  tidy <- tidy_parameters(fit, conf.int = conf.int, conf.level = conf.level,
                          method = method)
  tidy[startsWith(tidy$parameter, "CTmax"), , drop = FALSE]
}

#' Extract the thermal-sensitivity (z) estimate(s)
#'
#' @inheritParams get_ctmax
#' @return A [tibble][tibble::tibble] of the `z` row(s) from
#'   [tidy_parameters()].
#' @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)
#' get_z(fit)
#' @export
get_z <- function(fit, conf.int = TRUE, conf.level = 0.95,
                  method = c("wald", "profile")) {
  method <- match.arg(method)
  tidy <- tidy_parameters(fit, conf.int = conf.int, conf.level = conf.level,
                          method = method)
  tidy[startsWith(tidy$parameter, "z"), , drop = FALSE]
}

#' Extract the shape parameters (low, up, k, and phi)
#'
#' @inheritParams get_ctmax
#' @details With `method = "profile"`, a shape parameter receives a profile
#'   interval only where [tidy_parameters()] supports that coordinate; `up` and
#'   unsupported shape coordinates retain their documented Wald route. For
#'   continuous covariate designs, `low`, `up`, and `k` coefficients and Wald
#'   limits are returned on their link scale. Constant and cell-mean shapes
#'   remain on the response scale.
#' @return A [tibble][tibble::tibble] of the shape rows (`low`, `up`, `k`, and
#'   `phi` for the beta-binomial family), including any covariate coefficients,
#'   from [tidy_parameters()].
#' @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)
#' get_shape(fit)
#' @export
get_shape <- function(fit, conf.int = TRUE, conf.level = 0.95,
                      method = c("wald", "profile")) {
  method <- match.arg(method)
  tidy <- tidy_parameters(fit, conf.int = conf.int, conf.level = conf.level,
                          method = method)
  keep <- tidy$parameter %in% c("low", "up", "k", "phi") |
    grepl("^(low|up|k):", tidy$parameter)
  tidy[keep, , drop = FALSE]
}

#' Natural-scale link for each natural-scale parameter
#'
#' Maps each human-facing parameter name to the link on which its Wald interval
#' is constructed, using the fit's `name_map`. `up` is reported on the identity
#' scale here because its Wald interval is delta-method based (freqTLS does not yet
#' profile the disjoint-bounds `up` coordinate `beta_up`; its interval comes from the
#' ADREPORTed `up` SE).
#'
#' @param parameter Character vector of natural-scale parameter names.
#' @param name_map The fit's `name_map` data frame.
#' @return Character vector of link names aligned with `parameter`.
#' @keywords internal
#' @noRd
tls_param_scale <- function(parameter, name_map) {
  vapply(parameter, function(p) {
    if (identical(p, "up")) return("identity")
    hit <- which(name_map$natural == p)
    if (length(hit)) return(name_map$link[hit[1L]])
    natural <- sub(":.*$", "", p)
    if (identical(natural, "up")) return("logit")
    hit <- which(name_map$natural == natural)
    if (length(hit) == 0L) return(NA_character_)
    name_map$link[hit[1L]]
  }, character(1L), USE.NAMES = FALSE)
}

#' Reporting scale for a fitted parameter row
#'
#' Constant and one-hot cell-mean designs report estimates on the natural scale.
#' General fixed-effect coefficients remain on their fitted link scale.
#' @param parameter Character parameter names from `fit$estimates`.
#' @param fit A `profile_tls` fit.
#' @return Character scale labels aligned with `parameter`.
#' @keywords internal
#' @noRd
tls_report_scale <- function(parameter, fit) {
  design_for <- function(natural) switch(natural,
    CTmax = fit$tmb_inputs$data$X_CT,
    z = fit$tmb_inputs$data$X_logz,
    low = fit$tmb_inputs$data$X_low,
    up = fit$tmb_inputs$data$X_up,
    k = fit$tmb_inputs$data$X_logk,
    NULL
  )
  vapply(parameter, function(p) {
    natural <- sub(":.*$", "", p)
    # CTmax rows are additive temperature estimates and z rows are exponentiated
    # z multipliers in fit_tls(); both are displayed on their response scale.
    if (natural %in% c("CTmax", "z")) return("natural")
    X <- design_for(natural)
    if (!grepl(":", p) || is.null(X) || tls_is_constant_design(X) ||
        tls_is_onehot(X)) return("natural")
    tls_param_scale(p, fit$name_map)
  }, character(1L), USE.NAMES = FALSE)
}

#' Wald confidence intervals on the natural scale
#'
#' Builds confidence intervals on the internal (link) scale from the fixed-effect
#' part of the [TMB::sdreport()] and back-transforms the endpoints. `up` uses a
#' delta-method interval on the natural scale taken from the ADREPORTed `up`
#' standard error (freqTLS does not yet profile the disjoint-bounds `up`
#' coordinate `beta_up`).
#'
#' @param fit A `profile_tls` fit.
#' @param conf.level Confidence level.
#' @return A data frame with `parameter`, `conf.low`, `conf.high`.
#' @keywords internal
#' @noRd
tls_wald_natural <- function(fit, conf.level) {
  est <- fit$estimates
  zq <- stats::qt(1 - (1 - conf.level) / 2, df = tls_ci_df(fit))

  conf.low <- rep(NA_real_, nrow(est))
  conf.high <- rep(NA_real_, nrow(est))

  fixed_value <- vapply(est$parameter, function(p) {
    value <- tls_fixed_asymptote_value(fit, p)
    if (is.null(value)) NA_real_ else value
  }, numeric(1L))
  fixed_rows <- is.finite(fixed_value)
  conf.low[fixed_rows] <- fixed_value[fixed_rows]
  conf.high[fixed_rows] <- fixed_value[fixed_rows]

  sdr <- fit$sdreport
  if (is.null(sdr)) {
    return(data.frame(
      parameter = est$parameter, conf.low = conf.low, conf.high = conf.high,
      stringsAsFactors = FALSE
    ))
  }

  # A finite covariance approximation is not usable when the optimizer's
  # Hessian failed the positive-definiteness check. Keep point estimates
  # available, but withhold every Wald limit across extractors and summaries.
  if (!isTRUE(fit$convergence$pdHess)) {
    return(data.frame(
      parameter = est$parameter, conf.low = conf.low, conf.high = conf.high,
      stringsAsFactors = FALSE
    ))
  }

  # Internal-scale estimates + SEs, indexed positionally to match name_map
  # (grouped fits repeat row names such as "beta_CT").
  fixed <- summary(sdr, select = "fixed")
  nm <- fit$name_map

  # Position of each internal coordinate within the fixed-effect summary, in
  # name_map order. log_phi is mapped out for the binomial family and absent
  # from name_map there, so this stays aligned.
  link_endpoints <- function(internal_name, link, occurrence) {
    rows <- which(rownames(fixed) == internal_name)
    if (length(rows) < occurrence) return(c(NA_real_, NA_real_))
    r <- rows[occurrence]
    e <- fixed[r, "Estimate"]
    se <- fixed[r, "Std. Error"]
    lo_i <- e - zq * se
    hi_i <- e + zq * se
    # `low` lives on a disjoint-bounds logit: low = low_min + low_w * plogis(beta_low),
    # so its Wald endpoints must be rescaled to the natural scale (not plain plogis).
    if (internal_name == "beta_low") {
      bb <- fit$tmb_inputs$data
      return(c(bb$low_min + bb$low_w * stats::plogis(lo_i),
               bb$low_min + bb$low_w * stats::plogis(hi_i)))
    }
    c(tls_backtransform(lo_i, link), tls_backtransform(hi_i, link))
  }

  # Track how many times we have seen each internal base name (for the repeated
  # beta_CT / beta_logz rows in grouped fits).
  seen <- list()
  for (i in seq_len(nrow(nm))) {
    base <- sub("\\[.*$", "", nm$internal[i])
    occ <- (seen[[base]] %||% 0L) + 1L
    seen[[base]] <- occ
    natural <- nm$natural[i]
    if (natural == "up" || startsWith(natural, "up:")) next
    target_rows <- which(est$parameter == natural)
    if (length(target_rows) == 0L) next
    ep <- link_endpoints(base, nm$link[i], occ)
    conf.low[target_rows] <- ep[1L]
    conf.high[target_rows] <- ep[2L]
  }

  # A multi-column continuous shape design reports regression coefficients on
  # its link scale. Keep both the estimates and Wald limits on that same scale.
  fixed_shape_ci <- function(X, natural, beta_name) {
    if (tls_is_constant_design(X) || tls_is_onehot(X)) return(invisible(NULL))
    hits <- which(rownames(fixed) == beta_name)
    if (length(hits) != ncol(X)) return(invisible(NULL))
    for (j in seq_len(ncol(X))) {
      r <- which(est$parameter == paste0(natural, ":", colnames(X)[j]))
      if (length(r)) {
        e <- fixed[hits[j], "Estimate"]
        se <- fixed[hits[j], "Std. Error"]
        conf.low[r] <<- e - zq * se
        conf.high[r] <<- e + zq * se
      }
    }
    invisible(NULL)
  }
  fixed_shape_ci(fit$tmb_inputs$data$X_low, "low", "beta_low")
  fixed_shape_ci(fit$tmb_inputs$data$X_up, "up", "beta_up")
  fixed_shape_ci(fit$tmb_inputs$data$X_logk, "k", "beta_logk")

  # `up` (or per-group `up:<level>`): delta-method Wald on the natural scale from
  # the ADREPORTed `up` SE (disjoint-bounds beta_up not yet profiled).
  # The ADREPORT `up` order matches the estimates-table `up` rows (both in the
  # shape-design / group-level order).
  rep_sum <- summary(sdr, select = "report")
  up_hits <- which(rownames(rep_sum) == "up")
  up_general <- !tls_is_constant_design(fit$tmb_inputs$data$X_up) &&
    !tls_is_onehot(fit$tmb_inputs$data$X_up)
  up_rows_idx <- if (up_general) integer(0) else
    which(est$parameter == "up" | startsWith(est$parameter, "up:"))
  if (length(up_hits) == length(up_rows_idx) && length(up_hits) >= 1L) {
    for (j in seq_along(up_hits)) {
      r <- up_rows_idx[j]
      up_est <- rep_sum[up_hits[j], "Estimate"]
      up_se <- rep_sum[up_hits[j], "Std. Error"]
      conf.low[r] <- up_est - zq * up_se
      conf.high[r] <- up_est + zq * up_se
    }
  }

  # Random-effects SDs (`sigma_CTmax` / `sigma_logz`): Wald interval on the log
  # scale, exp() of log_sd_* +/- z * se, so the interval stays positive. The
  # profile / bootstrap paths are the RE-aware alternatives for the fixed effects;
  # the variance components themselves keep this log-scale Wald interval.
  ct_blocks <- Filter(function(bk) identical(bk$param, "CTmax"), tls_re_blocks(fit))
  for (bk in ct_blocks) {
    sig_row <- which(est$parameter == bk$sigma_label)
    sd_rows <- grep("^log_sd_CT", rownames(fixed))
    if (length(sig_row) == 1L && length(sd_rows) >= bk$sd_idx) {
      ls <- fixed[sd_rows[bk$sd_idx], ]
      conf.low[sig_row] <- exp(ls["Estimate"] - zq * ls["Std. Error"])
      conf.high[sig_row] <- exp(ls["Estimate"] + zq * ls["Std. Error"])
    }
  }
  for (sg in list(c("sigma_logz", "log_sd_logz"), c("sigma_low", "log_sd_low"),
                  c("sigma_logk", "log_sd_logk"))) {
    sig_row <- which(est$parameter == sg[1L])
    if (length(sig_row) == 1L) {
      rows <- which(rownames(fixed) == sg[2L])
      if (length(rows) >= 1L) {
        ls_est <- fixed[rows[1L], "Estimate"]
        ls_se <- fixed[rows[1L], "Std. Error"]
        conf.low[sig_row] <- exp(ls_est - zq * ls_se)
        conf.high[sig_row] <- exp(ls_est + zq * ls_se)
      }
    }
  }

  data.frame(
    parameter = est$parameter, conf.low = conf.low, conf.high = conf.high,
    stringsAsFactors = FALSE
  )
}

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.