## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.height = 4,
  dpi = 96
)

## ----setup--------------------------------------------------------------------
library(freqTLS)

## ----profile-shrimp-----------------------------------------------------------
data(shrimp_lethal)
shrimp_std <- standardize_data(
  shrimp_lethal,
  temp = "Temperature_assay", duration = "Duration_exposure_hours",
  n_total = "N_individuals_after_trial", mortality = "Mortality_after_trial",
  duration_unit = "hours"
)
shrimp_fit <- fit_4pl(shrimp_std, t_ref = 1, family = "beta_binomial", quiet = TRUE)
tls(shrimp_fit, method = "profile")$summary

## ----eye, fig.alt = "Confidence Eye for the shrimp CTmax and z: pale confidence lenses with hollow point estimates, the freqTLS uncertainty display. Both profiles close, so each eye is a closed lens."----
plot_confidence_eye(shrimp_fit, parm = c("CTmax", "z"), method = "profile")

## ----survival, fig.alt = "Fitted shrimp survival curves: probability of survival declining with exposure duration, one curve per assay temperature, hotter temperatures dropping faster."----
plot_survival_curves(shrimp_fit)

## ----tdt, fig.alt = "Shrimp thermal-death-time line: log exposure time at the mortality threshold falling linearly with assay temperature, the slope encoding z."----
plot_tdt_curve(shrimp_fit)

## ----derive-------------------------------------------------------------------
c(
  CTmax_50pct_1h = round(derive_ctmax(shrimp_fit, surv = 0.5, duration = 1), 2),
  T_crit_rate1   = round(derive_tcrit(shrimp_fit, rate = 1), 2)
)

## ----three-way, echo = FALSE--------------------------------------------------
cache_path <- system.file("extdata", "bayesTLS_benchmark_cache.rds", package = "freqTLS")
has_cache  <- nzchar(cache_path) && file.exists(cache_path)

fmt <- function(est, lo, hi) {
  ifelse(is.na(est), "--", sprintf("%.2f [%.2f, %.2f]", est, lo, hi))
}

if (has_cache) {
  cache <- readRDS(cache_path)
  pro <- confint(shrimp_fit$fit, c("CTmax", "z"), method = "profile")  # engine fit from above

  pick <- function(df, parm, cols) {
    r <- df[df$dataset == "shrimp" & df$parameter == parm, ]
    stats::setNames(as.numeric(r[cols]), c("est", "lo", "hi"))
  }
  ts_ct <- pick(cache$two_stage, "CTmax", c("estimate", "lower", "upper"))
  ts_z  <- pick(cache$two_stage, "z",     c("estimate", "lower", "upper"))
  by_ct <- pick(cache$bayesian,  "CTmax", c("median",   "lower", "upper"))
  by_z  <- pick(cache$bayesian,  "z",     c("median",   "lower", "upper"))
  pr_ct <- pro[pro$parameter == "CTmax", ]
  pr_z  <- pro[pro$parameter == "z", ]

  knitr::kable(data.frame(
    Quantity = c("CTmax (°C)", "z (°C / decade)"),
    `Two-stage (delta CI)`    = c(fmt(ts_ct["est"], ts_ct["lo"], ts_ct["hi"]),
                                  fmt(ts_z["est"],  ts_z["lo"],  ts_z["hi"])),
    `bayesTLS (95% CrI)`      = c(fmt(by_ct["est"], by_ct["lo"], by_ct["hi"]),
                                  fmt(by_z["est"],  by_z["lo"],  by_z["hi"])),
    `freqTLS (profile CI)` = c(fmt(pr_ct$estimate, pr_ct$conf.low, pr_ct$conf.high),
                                  fmt(pr_z$estimate,  pr_z$conf.low,  pr_z$conf.high)),
    check.names = FALSE
  ), caption = "Shrimp CTmax and z shown side by side: the two model fits use the relative midpoint; the classical estimator uses absolute LT50.")
} else {
  stop("The shipped bayesTLS benchmark cache is missing; reinstall freqTLS from a complete source tarball.")
}

