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

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

## ----simulate-----------------------------------------------------------------
set.seed(1)
dat <- simulate_tls(
  temps  = seq(30, 42, by = 2),
  times  = c(0.5, 1, 2, 4, 8),
  reps   = 3,
  n      = 20,
  CTmax  = 36,
  z      = 4,
  phi    = 50,
  family = "beta_binomial",
  seed   = 1
)
head(dat)

## ----truth--------------------------------------------------------------------
attr(dat, "truth")[c("CTmax", "z", "phi")]

## ----standardize--------------------------------------------------------------
std <- standardize_data(
  dat,
  temp     = "temp",
  duration = "duration",
  n_total  = "total",
  n_surv   = "survived"
)

## ----fit----------------------------------------------------------------------
fit <- fit_4pl(std, family = "beta_binomial", t_ref = 1)
fit

## ----fit-formula--------------------------------------------------------------
fit_f <- fit_tls(
  tls_bf(survived | trials(total) ~ time(duration) + temp(temp)),
  data   = dat,
  family = "beta_binomial",
  tref   = 1
)
all.equal(coef(fit_f), coef(fit))

## ----summary------------------------------------------------------------------
summary(fit)

## ----tidy---------------------------------------------------------------------
tidy_parameters(fit)
get_ctmax(fit)
get_z(fit)

## ----confint------------------------------------------------------------------
confint(fit, parm = "CTmax", method = "profile")
confint(fit, parm = "z", method = "profile")

## ----wald---------------------------------------------------------------------
confint(fit, parm = c("CTmax", "z"), method = "wald")

## ----survival-curves, fig.alt = "Fitted 4PL survival curves: survival probability declining with exposure duration, one curve per assay temperature, with observed proportions as points."----
plot_survival_curves(fit)

## ----confidence-eye, fig.alt = "Confidence Eye plot: a pale lens spanning the profile-likelihood interval with a hollow point estimate, for CTmax and z."----
plot_confidence_eye(fit, parm = c("CTmax", "z"), method = "profile")

## ----grouped------------------------------------------------------------------
dat_grp <- simulate_tls(
  temps = seq(30, 42, by = 2), times = c(0.5, 1, 2, 4, 8), reps = 3, n = 20,
  group = c("cool", "warm"), CTmax = c(34, 38), z = c(3, 5),
  phi = 50, family = "beta_binomial", seed = 2
)
fit_grp <- fit_tls(
  tls_bf(
    survived | trials(total) ~ time(duration) + temp(temp),
    CTmax ~ group,
    log_z ~ group
  ),
  data = dat_grp, family = "beta_binomial", tref = 1
)
confint(fit_grp, c("CTmax:cool", "CTmax:warm", "z:cool", "z:warm"),
        method = "profile")

## ----random-effect------------------------------------------------------------
dat_re <- simulate_tls(
  temps = seq(30, 42, by = 2), times = c(0.5, 1, 2, 4, 8), reps = 2, n = 20,
  CTmax = 36, z = 4, phi = 50, family = "beta_binomial",
  re_sd = 1.2, n_re_groups = 10, seed = 3
)
fit_re <- fit_tls(
  tls_bf(
    survived | trials(total) ~ time(duration) + temp(temp),
    CTmax ~ (1 | colony)
  ),
  data = dat_re, family = "beta_binomial", tref = 1
)
fit_re
head(ranef(fit_re))

## ----function-map, echo = FALSE, results = "asis"-----------------------------
# Inline the SVG rather than include_graphics() so the figure does not depend on
# pkgdown copying a co-located static asset. The Pandoc raw-HTML fence is
# essential: without it, wildcard labels such as get_*_summary() are parsed as
# Markdown emphasis, which breaks the SVG namespace. Accessibility is provided
# by the SVG's <title>/<desc>.
svg_candidates <- c(
  "freqTLS_function_map.svg",
  system.file("doc", "freqTLS_function_map.svg", package = "freqTLS")
)
svg_path <- svg_candidates[nzchar(svg_candidates) & file.exists(svg_candidates)][1]
if (is.na(svg_path)) {
  stop("The installed freqTLS function-map SVG is missing.")
}
svg_lines <- readLines(svg_path, warn = FALSE)
cat(
  "```{=html}\n",
  paste(svg_lines, collapse = "\n"),
  "\n```\n",
  sep = ""
)

