R/bayes_tls_methods.R

Defines functions plot.bayes_tls summary.bayes_tls print.bayes_tls

Documented in plot.bayes_tls print.bayes_tls summary.bayes_tls

# S3 methods for objects of class `bayes_tls` (currently produced by
# fit_4pl()). Provides print, summary, and plot (trace) methods so users can
# inspect a fit at three levels of detail: header, parameter+diagnostic
# table, MCMC mixing plot. The underlying brmsfit lives in `$fit` and brms /
# bayesplot methods are still available on it (e.g. `plot(wf$fit)`).

#' Compact print method for a `bayes_tls` workflow
#'
#' One-screen header: data shape, asymptote/response bounds, parameterisation
#' (midpoint, or direct CTmax/z with its threshold and `t_ref`; for grouped
#' direct fits, the moderator(s)), random-effect grouping (if any), and fit
#' status (draws if fitted, "spec only" otherwise). Call
#' [summary()][summary.bayes_tls()] for parameter posteriors and HMC
#' diagnostics, [plot()][plot.bayes_tls()] for MCMC trace plots.
#'
#' @param x   A `bayes_tls` object returned by [fit_4pl()].
#' @param ... Ignored.
#' @return The object, invisibly.
#' @examples
#' \dontrun{
#' wf <- fit_4pl(std)
#' print(wf)
#' }
#' @export
print.bayes_tls <- function(x, ...) {
  bounds <- tdt_workflow_bounds(x)
  cat("<bayes_tls>\n")
  cat("  Data:    ", nrow(x$data), "rows;",
      length(unique(x$data$temp)),     "temperatures;",
      length(unique(x$data$duration)), "durations\n")
  cat("  T_bar:   ", round(x$meta$temp_mean, 2), "\n")
  cat(sprintf(
    "  Bounds:   response in (%.3f, %.3f); low in (%.3f, %.3f); up in (%.3f, %.3f)\n",
    x$meta$lower %||% 0, x$meta$upper %||% 1,
    bounds$low_min, bounds$low_max,
    bounds$up_min,  bounds$up_max
  ))
  lab <- tryCatch(tdt_model_spec(tdt_model(x))$label,
                  error = function(e) paste0(x$meta$model, " (unrecognised)"))
  cat("  Model:   ", lab, "\n")
  if (identical(x$meta$parameterization %||% "midpoint", "direct")) {
    cat(sprintf("  Param:    direct CTmax/z (%s threshold, t_ref = %g min)\n",
                x$meta$threshold %||% "relative", x$meta$t_ref %||% 60))
    if (isTRUE(x$meta$grouped))
      cat("  Groups:  ", paste(x$meta$group_vars, collapse = ", "),
          "(per-group z/CTmax via tls(by = ...))\n")
  } else {
    cat("  Param:    midpoint\n")
  }
  if (!is.null(x$meta$fix_asymptotes))
    cat("  Fixed:   ", paste(sprintf("%s = %.4g", names(x$meta$fix_asymptotes),
                                     x$meta$fix_asymptotes), collapse = ", "),
        "(asymptote(s) pinned via fix_asymptotes)\n")
  if (isTRUE(x$meta$censored)) {
    nc <- if ("censored" %in% names(x$data))
            c(left  = sum(x$data$censored == "left"),
              right = sum(x$data$censored == "right"))
          else x$meta$n_censored
    cat(sprintf("  Censored: %d below and %d above the detection limit (brms cens())\n",
                nc[["left"]], nc[["right"]]))
  }
  if (!is.null(x$meta$random_effects))
    cat("  RE:      ", paste(x$meta$random_effects, collapse = ", "), "\n")
  cat("  Status:  ",
      if (is.null(x$fit)) "spec only (not fitted)"
      else sprintf("fitted (%d draws)", brms::ndraws(x$fit)),
      "\n")
  invisible(x)
}

#' Summarise a fitted `bayes_tls` workflow
#'
#' Delegates to `summary()` on the underlying `brmsfit` (via
#' [get_brmsfit()]), so the returned object is a brms `brmssummary` with the
#' population-level coefficient table, group-level standard deviations,
#' family parameters, and HMC diagnostics already laid out by brms.
#'
#' The high-level workflow context (data shape, centred-temperature mean,
#' asymptote bounds, random-effect grouping, draw count) is available via
#' [print.bayes_tls()]. For natural-scale 4PL parameters (`low`, `up`,
#' `k`, `z`), use [tdt_parameter_table()]. For the TDT quantities
#' (`z`, `CTmax_1hr`, optionally `T_crit`), use [tls()].
#'
#' @param object A fitted `bayes_tls` workflow.
#' @param ...    Passed through to [brms::summary.brmsfit()] (e.g.
#'               `prob`, `mc_se`, `priors`, `robust`).
#' @return A brms `brmssummary` object (brms handles printing).
#' @examples
#' \dontrun{
#' summary(wf)
#' summary(wf, prob = 0.9, robust = TRUE)
#' }
#' @export
summary.bayes_tls <- function(object, ...) {
  summary(get_brmsfit(object), ...)
}

#' MCMC mixing plot for a fitted `bayes_tls` workflow
#'
#' Delegates to `plot()` on the underlying `brmsfit` (via [get_brmsfit()]),
#' which produces brms's default `mcmc_combo` layout (per-parameter density
#' on the left, post-warmup trace on the right, chains coloured). Use this
#' to eyeball chain mixing alongside the numeric Rhat / ESS in
#' [summary.bayes_tls()].
#'
#' @param x    A fitted `bayes_tls` workflow.
#' @param ...  Passed through to `brms`'s `plot.brmsfit` method (e.g.
#'             `variable`, `combo`, `N`, `ask`).
#' @return The brms plot output (invisibly): a list of `bayesplot_grid`
#'         grid objects, one per plotted page.
#' @examples
#' \dontrun{
#' plot(wf)
#' plot(wf, variable = "^b_mid", regex = TRUE)
#' plot(wf, combo = c("dens_overlay", "trace"))
#' }
#' @export
plot.bayes_tls <- function(x, ...) {
  plot(get_brmsfit(x), ...)
}

Try the bayesTLS package in your browser

Any scripts or data that you put into this service are public.

bayesTLS documentation built on Nov. 29, 2026, 1:06 a.m.