Nothing
# Build and fit the joint Bayesian 4PL with built-in beta_binomial(link =
# "identity"). The asymptote reparam keeps the 4PL output inside (low_min,
# up_max) so identity link is safe; see
# notes/package_design/2026-05-12-identity-vs-logit-link.qmd for the empirical comparison.
#' Build the brms formula for the joint Bayesian 4PL
#'
#' Constructs the joint 4PL with the disjoint-bounds reparameterisation on the
#' asymptotes (`low`, `up`) and a positivity reparam on `k`. All four 4PL
#' sub-parameters (`lowraw`, `upraw`, `logk`, `mid`) get a `temp_c` slope — the
#' model assumes temperature can affect every aspect of the dose-response curve.
#' Random intercepts (if any) attach to `mid`.
#'
#' The response term depends on the family:
#'
#' - **Count families** (`binomial`, `beta_binomial`): the response is
#' `n_surv | trials(n_total) ~ <4PL>`.
#' - **`Beta` family** (continuous proportion): the response is
#' `<response_var> ~ <4PL>` with no `trials()` term. With `link = "identity"`
#' the 4PL value is the mean directly (safe because the reparam keeps it in
#' `(0, 1)`); with `link = "logit"` the 4PL is wrapped in `logit()` so the mean
#' is still the 4PL value. With `censored = TRUE` the response becomes
#' `<response_var> | cens(censored) ~ <4PL>`: rows flagged `"left"` or
#' `"right"` in a `censored` column contribute the probability of lying below
#' or above their recorded limit instead of a density (see the
#' `detection_limit` argument of [standardize_data()]).
#'
#' If a parameter has no real temperature effect, its `temp_c` slope shrinks
#' toward zero under the prior and the fit reduces cleanly to the simpler case.
#'
#' @param random_effects Optional character vector of grouping variables for
#' random intercepts on `mid`.
#' @param lower,upper Response-scale bounds for the asymptotes. Default
#' `0, 1` for proportion data; set narrower for sublethal
#' data whose response sits well above `0`.
#' @param family brms family. Default `beta_binomial(link = "identity")`.
#' Pass `binomial(link = "identity")` for the simpler
#' no-overdispersion case, or `brms::Beta(link = "identity")`
#' for a continuous proportion response. The count families
#' (`binomial`, `beta_binomial`) require `link = "identity"`
#' — the 4PL produces a probability directly, so any other
#' link would double-transform it (an error is thrown). A
#' known family name passed as a string (e.g. `"binomial"`)
#' is coerced to its identity-link constructor.
#' @param response_var Response column name for a `Beta` (continuous
#' proportion) fit. Ignored for count families. Default
#' `"survival"` (the column [standardize_data()] writes for
#' a `proportion` response).
#' @param censored Logical. `TRUE` adds brms's censoring term to a `Beta`
#' response, `<response_var> | cens(censored)`, so rows
#' flagged `"left"`/`"right"` in a `censored` column enter
#' the likelihood as the probability of lying beyond their
#' limit. Default `FALSE`. [fit_4pl()] and the other
#' fitters set it from the data when [standardize_data()]
#' was given a `detection_limit`. Errors for the count
#' families.
#' @param temp_effects Character vector naming which 4PL sub-parameters carry a
#' `temp_c` slope: any subset of `c("low", "up", "k",
#' "mid")`. Default is all four — temperature can affect
#' every aspect of the dose-response curve. `"mid"` must
#' always be present (its slope is \eqn{-1/z}, the core TDT
#' quantity). Restricting to `"mid"` gives the classical
#' *constant-shape* TDT model (asymptotes and slope shared
#' across temperature), which is the right choice for sparse
#' designs where the richer all-four model over-fits.
#' Ignored in the direct CTmax/z parameterisation.
#' @param bounds Length-2 numeric `c(lower, upper)` giving the
#' response-scale asymptote interval. Defaults to
#' `c(lower, upper)`. Preferred over the separate
#' `lower`/`upper` (which it supersedes — see those).
#' @param ctmax,z One-sided formulas for the **direct** parameterisation.
#' Supplying either switches `make_4pl_formula()` to fit
#' `CTmax` (as `CTmaxdev = CTmax - mean(temp)`) and `z`
#' (as `logz`) directly as nonlinear parameters, with `mid`
#' reconstructed from them. Carry the fixed and random
#' effects for tolerance and sensitivity, e.g.
#' `~ life_stage + (1 | Date)`. Omit `z` for a single
#' pooled `z` (`logz ~ 1`).
#' @param up,low,k Optional one-sided formulas for the asymptote and
#' steepness sub-parameters. In **direct** mode, when
#' omitted they inherit the `ctmax`/`z` **fixed** effects
#' crossed with `temp_c` (random effects are not inherited);
#' intercept-only `ctmax`/`z` gives `~ temp_c`. In
#' **midpoint** mode they default to the `temp_effects`
#' structure (or `by`, see below); an explicit formula
#' always overrides.
#' @param mid **Midpoint** mode only. Optional one-sided formula for
#' the midpoint sub-parameter (whose `temp_c` slope is
#' \eqn{-1/z}). Must carry `temp_c`. Defaults to the
#' `temp_effects`/`by` structure. Errors in direct mode
#' (where `mid` is reconstructed from `ctmax`/`z`).
#' @param by **Midpoint** mode only. Optional character vector of
#' moderator column(s). A shortcut that fits `~ temp_c * by`
#' on **all four** sub-parameters (`low`/`up`/`k`/`mid`) at
#' once -- i.e. every aspect of the curve, plus the
#' midpoint's temperature slope (\eqn{-1/z}), varies by
#' group. `by` is the more specific instruction, so it
#' overrides `temp_effects`. Equivalent to writing the four
#' `~ temp_c * by` formulas by hand; use explicit formulas
#' for finer per-parameter control. Errors in direct mode
#' (group there via `ctmax = ~ 0 + <moderator>`).
#' @param threshold `"relative"` (default) fits the midpoint backbone so
#' `CTmax`/`z` are the relative-threshold quantities;
#' `"absolute"` bakes the asymmetry correction
#' \eqn{C(T) = (1/k)\log((up-p)/(p-low))} into `mid` so
#' they are the absolute (`p`-survival) quantities. The
#' disjoint-bounds reparam splits the asymptotes at the
#' bounds' midpoint, so `"absolute"` is only well defined
#' for `p` *near that midpoint* (with the default bounds
#' `[0, 1]` the achievable range is roughly `(0.499,
#' 0.501)`); other LTx values (LT60, LT90, ...) must be
#' obtained from a `"relative"` fit plus
#' [tls()] with `target_surv = `, which
#' inverts the fitted surface post hoc for any `p`.
#' @param p Survival fraction defining the absolute threshold.
#' Default `0.5`. Only meaningful when `threshold =
#' "absolute"`, and (see `threshold`) only achievable for
#' `p` near the bounds' midpoint -- use the relative
#' threshold + `target_surv` for any other LTx.
#' @param log10_tref `log10` of the reference exposure (in the model's time
#' unit) at which `CTmax` is defined. Default `0` (one time
#' unit). [fit_4pl()] derives this from `t_ref` and the
#' data's duration unit.
#' @return A `brmsformula` object.
#' @examples
#' make_4pl_formula()
#' make_4pl_formula(family = binomial(link = "identity"))
#' make_4pl_formula(temp_effects = "mid") # constant-shape TDT
#' make_4pl_formula(family = brms::Beta(link = "identity"),
#' response_var = "survival")
#' make_4pl_formula(family = brms::Beta(link = "identity"),
#' censored = TRUE) # censored readings
#' make_4pl_formula(ctmax = ~ life_stage, z = ~ life_stage) # direct CTmax/z
#' @export
make_4pl_formula <- function(random_effects = NULL,
bounds = NULL,
family = brms::beta_binomial(
link = "identity"),
response_var = "survival",
temp_effects = c("low", "up", "k", "mid"),
ctmax = NULL,
z = NULL,
up = NULL,
low = NULL,
k = NULL,
mid = NULL,
by = NULL,
threshold = c("relative", "absolute"),
p = 0.5,
log10_tref = 0,
lower = 0,
upper = 1,
censored = FALSE) {
make_tdt_formula(model = "log-logistic",
random_effects = random_effects,
bounds = bounds,
family = family,
response_var = response_var,
temp_effects = temp_effects,
ctmax = ctmax,
z = z,
up = up,
low = low,
k = k,
mid = mid,
by = by,
threshold = threshold,
p = p,
log10_tref = log10_tref,
lower = lower,
upper = upper,
censored = censored)
}
#' Build the brms formula for a joint Bayesian four-parameter dose-response model
#'
#' Model-generic twin of [make_4pl_formula()]: the same construction with the
#' curve chosen by `model`. [make_4pl_formula()] is `make_tdt_formula(model =
#' "log-logistic")` and keeps meaning the log-logistic.
#'
#' @param model Dose-response curve: `"log-logistic"` (the 4PL),
#' `"log-normal"` (probit), `"weibull1"` (Weibull type 1, the
#' Gompertz-shaped curve on log time) or `"weibull2"` (Weibull type 2).
#' Every model is written in its midpoint form, so `mid`, the TDT line,
#' `z` and `CTmax` mean the same thing for all of them; only the shape of
#' the curve and the absolute-threshold correction differ.
#' @param threshold `"relative"` (default) or `"absolute"`, as in
#' [make_4pl_formula()], except that `"absolute"` bakes the chosen model's
#' correction into `mid`: \eqn{(1/k)\log((up-p)/(p-low))} for the
#' log-logistic, and the log-normal and Weibull models' own closed forms
#' otherwise.
#' @inheritParams make_4pl_formula
#' @inherit make_4pl_formula return
#' @examples
#' make_tdt_formula(model = "log-normal")
#' make_tdt_formula(model = "weibull1", ctmax = ~ 1, z = ~ 1)
#' @export
make_tdt_formula <- function(model = "log-logistic",
random_effects = NULL,
bounds = NULL,
family = brms::beta_binomial(
link = "identity"),
response_var = "survival",
temp_effects = c("low", "up", "k", "mid"),
ctmax = NULL,
z = NULL,
up = NULL,
low = NULL,
k = NULL,
mid = NULL,
by = NULL,
threshold = c("relative", "absolute"),
p = 0.5,
log10_tref = 0,
lower = 0,
upper = 1,
censored = FALSE) {
if (!is.logical(censored) || length(censored) != 1L || is.na(censored))
stop("`censored` must be TRUE or FALSE.", call. = FALSE)
if (is.null(bounds)) bounds <- c(lower, upper)
threshold <- match.arg(threshold)
b <- compute_4pl_bounds(bounds[1], bounds[2])
family <- validate_4pl_family(family)
mod <- tdt_model_spec(model)
low_expr <- sprintf("(%.6f + inv_logit(lowraw) * %.6f)", b$low_min, b$low_w)
up_expr <- sprintf("(%.6f + inv_logit(upraw) * %.6f)", b$up_min, b$up_w)
# Response LHS + optional logit() wrap, shared across parameterisations.
is_beta <- identical(family$family, "beta")
if (censored && !is_beta)
stop("`censored = TRUE` needs the Beta family (a continuous-proportion ",
"response); count responses are not censored.", call. = FALSE)
resp_lhs <- if (!is_beta) "n_surv | trials(n_total)"
else if (censored) paste(response_var, "| cens(censored)")
else response_var
wrap <- function(rhs) if (is_beta && identical(family$link, "logit"))
sprintf("logit(%s)", rhs) else rhs
if (is.null(ctmax) && is.null(z)) {
## ---- midpoint parameterisation ----
temp_effects <- match.arg(temp_effects, c("low", "up", "k", "mid"),
several.ok = TRUE)
main_rhs <- mod$rhs(low_expr, up_expr)
# Each sub-parameter's RHS. An explicit formula always wins. Otherwise:
# * with a `by` moderator -> ALL four get `temp_c * by` (treatment-coded, so
# the reference level keeps the centred Intercept prior and the per-group
# offsets are weakly shrunk toward it). `by` is the more specific
# instruction, so it overrides `temp_effects`.
# * without `by` -> the classic `temp_effects` default: `temp_c` for a
# sub-parameter in `temp_effects`, else `1` (constant in temperature).
# Every sub-parameter carries at least `temp_c` in the all-four default; `mid`
# MUST carry temp_c (its slope is -1/z, the core TDT quantity) -- enforced
# below so e.g. temp_effects = c("low", "up") (mid dropped) errors clearly.
by_term <- if (!is.null(by)) paste(by, collapse = " * ") else NULL
par_rhs <- function(par) {
if (!is.null(by_term)) return(paste0("temp_c * ", by_term))
if (par %in% temp_effects) "temp_c" else "1"
}
res <- function(explicit, par) {
if (!is.null(explicit)) formula_rhs(explicit, par_rhs(par)) else par_rhs(par)
}
low_r <- res(low, "low")
up_r <- res(up, "up")
k_r <- res(k, "k")
mid_r <- res(mid, "mid")
if (!grepl("temp_c", mid_r))
stop("`mid` must always carry temp_c (its slope is -1/z, the core TDT ",
"quantity); supply a `mid`/`by` formula that includes temp_c, or keep ",
"\"mid\" in `temp_effects`.", call. = FALSE)
# Guard the silent-pooling footgun: if low/up/k carry a moderator that mid
# does NOT, z is pooled across that moderator even though the asymptotes vary
# by it. That is a valid model, but rarely what a user writing
# `low = ~ temp_c * species` intends -- point them at `by=` / `mid=`.
shape_mods <- setdiff(rhs_fixed_vars(paste(low_r, up_r, k_r, sep = " + ")),
rhs_fixed_vars(mid_r))
if (length(shape_mods)) {
# When `by=` is set, the moderator landed on low/up/k via the `by`
# shortcut but an explicit `mid=` override dropped it -- so re-suggesting
# `by = "<mod>"` (which the user already passed) is circular. Point at the
# explicit-mid override as the cause and give only the working remedy.
remedy <- if (!is.null(by_term))
sprintf(paste0("your explicit `mid` override dropped the `by` moderator; ",
"include it, e.g. mid = ~ temp_c * %s, to let z vary by ",
"group."), shape_mods[1])
else
sprintf(paste0("Use by = \"%s\" (applies the moderator to all four ",
"sub-parameters) or mid = ~ temp_c * %s to let z vary by ",
"group."), shape_mods[1], shape_mods[1])
warning(sprintf(
paste0("low/up/k vary by %s but mid does not, so z (= -1/mid's temp_c ",
"slope) is POOLED across %s. %s"),
paste(shape_mods, collapse = ", "), paste(shape_mods, collapse = ", "),
remedy), call. = FALSE)
}
mid_rhs <- paste(c(mid_r, tdt_format_random_effects(random_effects)),
collapse = " + ")
return(brms::bf(
stats::as.formula(sprintf("%s ~ %s", resp_lhs, wrap(main_rhs))),
stats::as.formula(paste("lowraw ~", low_r)),
stats::as.formula(paste("upraw ~", up_r)),
stats::as.formula(paste("logk ~", k_r)),
stats::as.formula(paste("mid ~", mid_rhs)),
nl = TRUE, family = family))
}
## ---- direct CTmax/z parameterisation ----
# CTmaxdev and logz are estimated; low/up/k and mid are nlf-derived. mid is the
# relative backbone, minus C(T) under threshold = "absolute".
ctmax_rhs <- formula_rhs(ctmax, "1")
z_rhs <- formula_rhs(z, "1")
inherit <- if (!is.null(ctmax)) ctmax_rhs else z_rhs
# Absolute threshold builds log((up - p)/(p - low)); the disjoint asymptote
# reparameterisation keeps low <= low_max and up >= up_min, so this is finite
# for every draw iff low_max < p < up_min. With sub-unit response bounds (e.g.
# PSII in [0.85, 1]) the achievable range never reaches p = 0.5, which would
# give log() of a negative number (an opaque Stan failure). Catch it early with
# an actionable message; relative thresholds are well defined for any bounds.
if (identical(threshold, "absolute") && !(b$low_max < p && p < b$up_min))
stop(sprintf(
paste0("threshold = \"absolute\" with target p = %g is not achievable for ",
"response bounds [%g, %g]: the disjoint-bounds reparameterisation ",
"splits the asymptotes at the midpoint %.3f, so baking an absolute ",
"%g%% correction into the fit is only safe for p near %.3f (here the ",
"achievable range is roughly (%.3f, %.3f)). Either fit with ",
"threshold = \"relative\" (the per-draw midpoint, defined for any ",
"bounds), or -- to get an LT%g (or any LTx) -- fit with the relative ",
"threshold and derive it afterwards via ",
"tls(target_surv = %g), which inverts the fitted ",
"surface post hoc and works for any p."),
p, bounds[1], bounds[2], b$midpoint, 100 * p, b$midpoint,
b$low_max, b$up_min, 100 * p, p),
call. = FALSE)
mid_rel <- sprintf("(%g - (temp_c - CTmaxdev) / exp(logz))", log10_tref)
mid_expr <- if (identical(threshold, "relative")) mid_rel else
sprintf("(%s - %s)", mid_rel, mod$abs_corr_stan(p))
main_rhs <- mod$rhs("low", "up")
brms::bf(
stats::as.formula(sprintf("%s ~ %s", resp_lhs, wrap(main_rhs))),
brms::nlf(stats::as.formula(paste("low ~", low_expr))),
brms::nlf(stats::as.formula(paste("up ~", up_expr))),
brms::nlf(stats::as.formula(paste("mid ~", mid_expr))),
stats::as.formula(paste("lowraw ~", resolve_shape(low, inherit))),
stats::as.formula(paste("upraw ~", resolve_shape(up, inherit))),
stats::as.formula(paste("logk ~", resolve_shape(k, inherit))),
stats::as.formula(paste("CTmaxdev ~", ctmax_rhs)),
stats::as.formula(paste("logz ~", z_rhs)),
nl = TRUE, family = family)
}
#' Fit the joint Bayesian 4PL to standardised TDT data
#'
#' Wraps [make_4pl_formula()] + [make_4pl_priors()] + [brms::brm()]. Returns
#' a workflow object containing the fit, data, formula, prior, and metadata
#' that downstream helpers (e.g. [tls()]) read from.
#'
#' By default all four 4PL sub-parameters get a `temp_c` slope, and random
#' intercepts attach to `mid` only. Use `temp_effects = "mid"` for the classical
#' constant-shape TDT model (recommended for sparse designs — see `temp_effects`).
#' To fit separate categories (life stages, species, populations), filter `data`
#' per category and call this function once per subset.
#'
#' Censored readings: if [standardize_data()] was given a `detection_limit`
#' (a continuous-proportion response whose instrument cannot resolve values
#' beyond a limit), the fit adds brms's `cens()` term to the response so those
#' readings contribute the probability of lying beyond the limit. Only the
#' response side of the formula changes (`survival | cens(censored)`); the
#' 4PL, the priors and every downstream reader of the curve are the same.
#' Because censored readings then score as probabilities in `log_lik()`,
#' `loo()` and `waic()`, compare such a fit only with fits to the same data
#' and the same `detection_limit`.
#'
#' @param data Output of [standardize_data()].
#' @param random_effects Optional character vector of grouping variables for
#' random intercepts on `mid`.
#' @param temp_effects Which 4PL sub-parameters carry a `temp_c` slope; a subset
#' of `c("low", "up", "k", "mid")` (default all four).
#' `"mid"` is always required. The all-four default needs
#' roughly >= 15 cells per fixed effect to be stable; for
#' sparse designs (few temperatures x durations, small n)
#' prefer `temp_effects = "mid"` (constant-shape), which
#' estimates only the midpoint's temperature slope (= -1/z)
#' and shares the asymptotes and steepness across
#' temperature. Passed to the formula builder
#' ([make_4pl_formula()] for `fit_4pl()`,
#' [make_tdt_formula()] for the other models).
#' @param lower,upper Response-scale bounds for the asymptotes. Default
#' `0, 1`; pass `lower = 0.85, upper = 1` for sublethal
#' data bounded above zero.
#' @param family brms family. `NULL` (default) picks the family from the
#' data's `response_type` metadata:
#' `beta_binomial(link = "identity")` for count data and
#' `brms::Beta(link = "identity")` for a continuous
#' `proportion` response. Pass an explicit family to
#' override (e.g. `binomial(link = "identity")` for the
#' no-overdispersion count case).
#' @param prior Optional `brmsprior` object. If `NULL` (default), the
#' model's default priors are built from `data`,
#' `lower`, `upper`, and `random_effects`
#' ([make_4pl_priors()] for `fit_4pl()`,
#' [make_tdt_priors()] for the other models).
#' @param chains,iter,warmup,cores,seed Sampling arguments passed to [brms::brm()].
#' @param backend Either `"cmdstanr"` (default) or `"rstan"`.
#' @param control List of HMC control parameters.
#' @param init Initial values for the sampler. Default `0` initialises
#' every unconstrained parameter at zero.
#' @param file Optional path (without extension) to cache the fit.
#' @param file_refit Passed to [brms::brm()]. Default `"on_change"`.
#' @param fit Logical. If `FALSE`, returns the workflow spec without
#' fitting — useful for inspecting the formula and priors.
#' @param bounds Length-2 `c(lower, upper)` asymptote interval;
#' supersedes `lower`/`upper` when supplied.
#' @param ctmax,z,up,low,k One-sided formulas selecting the **direct** CTmax/z
#' parameterisation (see [make_tdt_formula()]). Supplying
#' `ctmax` and/or `z` fits CTmax and z directly; `up`/`low`/`k`
#' are inherited from them when omitted. `up`/`low`/`k` also
#' take explicit formulas in the **midpoint** parameterisation.
#' @param mid,by **Midpoint** parameterisation only (see
#' [make_tdt_formula()]). `mid` is an explicit one-sided
#' formula for the midpoint (must carry `temp_c`); `by` is a
#' moderator column-name shortcut applying the moderator to
#' all four sub-parameters (`low`/`up`/`k`/`mid`) at once, so
#' a per-group CTmax/z fit needs only `by = "<moderator>"`.
#' Either records the moderator in `meta$group_vars`, so
#' [tls()] auto-derives per-group quantities
#' with `by = `. Both error in direct mode.
#' @param threshold `"relative"` (default) or `"absolute"`; the threshold the
#' fitted CTmax/z refer to (direct mode only).
#' @param p Survival fraction for the `"absolute"` threshold (direct
#' mode). Default `0.5` (LT50). The disjoint-bounds reparam
#' splits the asymptotes at the bounds' midpoint, so an
#' absolute `p` is only achievable *near that midpoint*
#' (with the default bounds `[0, 1]` the achievable range is
#' roughly `(0.499, 0.501)`); `fit_4pl()` errors otherwise.
#' For any other LTx (LT60, LT90, ...) or a sub-unit-bounded
#' response, fit with `threshold = "relative"` and derive it
#' afterwards via [tls()] with
#' `target_surv = `.
#' @param t_ref Reference exposure for CTmax, in **minutes** (default 60,
#' i.e. one hour). Converted to the model's log10-time scale
#' via the data's `duration_unit`.
#' @param data2 Named list of objects referenced by the model
#' formulas but not part of `data` -- currently the
#' covariance/correlation matrices used by
#' `gr(<group>, cov = <name>)` random-effect terms, e.g.
#' a phylogenetic correlation matrix from
#' `ape::vcv(phy, corr = TRUE)`. Matrices are validated
#' (symmetry, dimnames covering the grouping levels,
#' unit diagonal) and passed to [brms::brm()].
#' @param fix_asymptotes Pin one or both asymptotes instead of estimating them.
#' Default `NULL`: `low` and `up` are estimated. `TRUE`
#' fixes both at the response bounds (`0` and `1` for
#' proportions); `c("up")` or `c("low")` fixes only that
#' one at its bound and leaves the other estimated; a named
#' numeric vector such as `c(low = 0.02)` or
#' `c(low = 0, up = 1)` fixes the named ones at those
#' values. A fixed asymptote cannot carry a formula (its
#' formula becomes `~ 1` and it is dropped from
#' `temp_effects`); it is pinned through a brms
#' `constant()` prior, so every downstream function reads
#' it like an estimated one. Values at the response bounds
#' are pinned at the edge of the padded window (`0.001` /
#' `0.999` with the defaults; see [compute_4pl_bounds()]).
#' The effective values are stored in `meta$fix_asymptotes`.
#' @param ... Further arguments passed to [brms::brm()].
#' @return A list of class `"bayes_tls"` with elements `fit`, `data`,
#' `formula`, `prior`, `meta`. See [print.bayes_tls()],
#' [summary.bayes_tls()], and [plot.bayes_tls()] for the available
#' methods.
#' @examples
#' \dontrun{
#' d <- standardize_data(raw, ...)
#' wf <- fit_4pl(d, fit = FALSE) # inspect spec only
#' wf <- fit_4pl(d, file = "output/models/my_fit")
#' wf <- fit_4pl(d, lower = 0.85) # sublethal data
#'
#' # Phylogenetic model: species-level CTmax/z with a phylogenetic
#' # correlation matrix (issue #12).
#' A <- ape::vcv(phy, corr = TRUE) # unit-diagonal correlation matrix
#' wf <- fit_4pl(d,
#' ctmax = ~ 1 + (1 | gr(species, cov = A)),
#' z = ~ 1 + (1 | gr(species, cov = A)),
#' t_ref = 60, data2 = list(A = A))
#' tls(wf, by = "species") # per-species z / CTmax (shrunk via A)
#' }
#' @export
fit_4pl <- function(data,
random_effects = NULL,
temp_effects = c("low", "up", "k", "mid"),
lower = 0,
upper = 1,
family = NULL,
prior = NULL,
chains = 4,
iter = 4000,
warmup = floor(iter / 2),
cores = chains,
seed = 123,
backend = "cmdstanr",
control = list(adapt_delta = 0.95,
max_treedepth = 12),
init = 0,
file = NULL,
file_refit = "on_change",
fit = TRUE,
bounds = NULL,
ctmax = NULL,
z = NULL,
up = NULL,
low = NULL,
k = NULL,
mid = NULL,
by = NULL,
threshold = c("relative", "absolute"),
p = 0.5,
t_ref = 60,
data2 = NULL,
fix_asymptotes = NULL,
...) {
tdt_fit(model = "log-logistic",
data = data,
random_effects = random_effects,
temp_effects = temp_effects,
lower = lower,
upper = upper,
family = family,
prior = prior,
chains = chains,
iter = iter,
warmup = warmup,
cores = cores,
seed = seed,
backend = backend,
control = control,
init = init,
file = file,
file_refit = file_refit,
fit = fit,
bounds = bounds,
ctmax = ctmax,
z = z,
up = up,
low = low,
k = k,
mid = mid,
by = by,
threshold = threshold,
p = p,
t_ref = t_ref,
data2 = data2,
fix_asymptotes = fix_asymptotes,
...)
}
# Internal engine behind fit_4pl(), fit_lognormal() and fit_weibull(): the
# whole fitting workflow with the curve chosen by `model`. Kept unexported so
# that each exported function's name says which curve it fits.
#' @noRd
tdt_fit <- function(model = "log-logistic",
data,
random_effects = NULL,
temp_effects = c("low", "up", "k", "mid"),
lower = 0,
upper = 1,
family = NULL,
prior = NULL,
chains = 4,
iter = 4000,
warmup = floor(iter / 2),
cores = chains,
seed = 123,
backend = "cmdstanr",
control = list(adapt_delta = 0.95,
max_treedepth = 12),
init = 0,
file = NULL,
file_refit = "on_change",
fit = TRUE,
bounds = NULL,
ctmax = NULL,
z = NULL,
up = NULL,
low = NULL,
k = NULL,
mid = NULL,
by = NULL,
threshold = c("relative", "absolute"),
p = 0.5,
t_ref = 60,
data2 = NULL,
fix_asymptotes = NULL,
...) {
threshold <- match.arg(threshold)
model <- tdt_match_model(model)
if (!is.null(bounds)) { lower <- bounds[1]; upper <- bounds[2] }
# Fixed asymptotes (prior-based, see tdt_resolve_fix_asymptotes()): force the
# pinned asymptote's formula to `~ 1`, refuse a conflicting user formula, and
# drop it from `temp_effects` so the metadata stays honest.
fx <- tdt_resolve_fix_asymptotes(fix_asymptotes, lower, upper)
if (!is.null(fx)) {
for (nm in names(fx)) {
f_user <- if (nm == "low") low else up
if (!is.null(f_user) && !identical(formula_rhs(f_user, "1"), "1"))
stop(sprintf(paste0("`%s` is fixed by `fix_asymptotes` but also has the ",
"formula `%s = %s`; a fixed asymptote cannot vary ",
"with temperature or moderators. Drop one of the ",
"two."), nm, nm, formula_rhs(f_user, "1")),
call. = FALSE)
}
if (!is.null(fx$low)) low <- ~ 1
if (!is.null(fx$up)) up <- ~ 1
temp_effects <- setdiff(temp_effects, names(fx))
}
fx_values <- tdt_fixed_asymptote_values(fx, compute_4pl_bounds(lower, upper),
lower, upper)
direct <- !is.null(ctmax) || !is.null(z)
if (direct && !is.null(random_effects))
stop("In the direct CTmax/z parameterisation, random effects go INSIDE the ",
"`ctmax`/`z` formulas, e.g. ctmax = ~ 0 + grp + (1 | batch). The ",
"`random_effects` argument is for the midpoint parameterisation and ",
"would otherwise be silently ignored here.", call. = FALSE)
if (direct && !is.null(mid))
stop("`mid` is a midpoint-parameterisation argument; in the direct CTmax/z ",
"parameterisation the midpoint is derived from ctmax/z and cannot be ",
"set directly.", call. = FALSE)
if (direct && !is.null(by))
stop("`by` builds the midpoint per-group structure; in the direct CTmax/z ",
"parameterisation, group per moderator via ctmax = ~ 0 + <moderator> ",
"(z / up / low / k then inherit it).", call. = FALSE)
# Make group coding irrelevant: a single-factor treatment term (`~ G` or
# `~ 1 + G`) spans the same model as cell-means (`~ 0 + G`), but only
# cell-means receives per-level CTmax/z priors -- under treatment coding the
# mean-zero contrast prior inflates non-reference groups' marginal prior
# variance (~sqrt(2)x), so the two codings would NOT give the same posterior.
# Normalising single-factor treatment terms to cell-means makes the
# parameterisations genuinely equivalent with correct priors. Multi-term,
# interaction, and continuous moderators are left untouched.
if (direct) {
ctmax <- normalize_cellmeans(ctmax, data)
z <- normalize_cellmeans(z, data)
up <- normalize_cellmeans(up, data)
low <- normalize_cellmeans(low, data)
k <- normalize_cellmeans(k, data)
}
# Validate covariance-matrix random effects (issue #12): gr(cov = ) terms
# need their matrix in `data2` (symmetric, level-covering dimnames, unit
# diagonal) and their grouping columns in `data`. Runs for both
# parameterisations; a no-op when no bar terms are present.
tdt_validate_data2(
list(ctmax = ctmax, z = z, up = up, low = low, k = k, mid = mid,
random_effects = if (!is.null(random_effects))
paste(tdt_format_random_effects(random_effects), collapse = " + ")
else NULL),
data = data, data2 = data2)
meta <- attr(data, "tdt_meta") %||%
list(temp_mean = mean(data$temp, na.rm = TRUE),
duration_unit = "model_units",
random_effects = random_effects,
response_type = "count")
response_type <- meta$response_type %||% "count"
response_var <- meta$response_var %||% "survival"
censored <- isTRUE(meta$censored)
if (censored && !"censored" %in% names(data))
stop("The data's metadata says its readings are censored (a ",
"`detection_limit` was given to standardize_data()), but the ",
"`censored` column is missing. Re-run standardize_data().",
call. = FALSE)
# Censoring flags made by standardize_data(detection_limit = ) (the
# `censored` + `survival_recorded` pair) whose metadata was lost (e.g. by
# merge()) would otherwise be fitted as exact readings at the limit. A
# user's own `censored` column with intact metadata is left alone.
if (is.null(attr(data, "tdt_meta")) &&
all(c("censored", "survival_recorded") %in% names(data)) &&
any(data$censored %in% c("left", "right")))
stop("The data flag ", sum(data$censored %in% c("left", "right")),
" row(s) as censored (standardize_data(detection_limit = )) but ",
"their 'tdt_meta' attribute was lost (e.g. by merge()). Re-run ",
"standardize_data() with its `detection_limit` on the combined data.",
call. = FALSE)
if (censored && !all(data$censored %in% c("none", "left", "right")))
stop("The `censored` column holds values other than \"none\", \"left\" ",
"or \"right\" (e.g. NA after binding data standardised with and ",
"without a `detection_limit`). Standardise the combined raw data in one ",
"standardize_data() call, with a per-row limit column (NA = no limit) ",
"where limits differ.", call. = FALSE)
# Default family from the response type when not supplied; otherwise validate
# (and lightly coerce string names) so a misspecified family fails clearly
# here rather than as a cryptic `$` error or a silently double-linked fit.
if (is.null(family)) {
family <- if (identical(response_type, "proportion"))
brms::Beta(link = "identity")
else
brms::beta_binomial(link = "identity")
} else {
family <- validate_4pl_family(family)
}
if (censored && !identical(family$family, "beta"))
stop("Censored readings (from standardize_data(detection_limit = )) need ",
"the Beta family; got `", family$family, "`.", call. = FALSE)
# Reference exposure for CTmax (direct mode): t_ref is in minutes; place it on
# the model's log10-time scale. log10_tref = log10(t_ref / minutes-per-unit).
# Use the SAME alias-aware unit mapping as tls()/derive_*(); a bespoke
# switch() here silently fell back to tm = 1 for any non-canonical label
# ("h", "hr", "Hours", ...), desynchronising log10_tref from the extractors.
# The NULL-unit sentinel "model_units" keeps tm = 1 (no real time unit given).
unit <- meta$duration_unit %||% "model_units"
tm <- if (identical(unit, "model_units")) 1 else tdt_unit_to_minutes(unit)
log10_tref <- log10(t_ref / tm)
formula <- make_tdt_formula(model = model,
random_effects = random_effects,
lower = lower,
upper = upper,
family = family,
response_var = response_var,
censored = censored,
temp_effects = temp_effects,
ctmax = ctmax,
z = z,
up = up,
low = low,
k = k,
mid = mid,
by = by,
threshold = threshold,
p = p,
log10_tref = log10_tref)
if (is.null(prior)) {
# Set the phi prior only for families that carry a precision parameter
# (beta and beta_binomial); skip it for binomial (no overdispersion).
family_name <- family$family
prior_phi <- if (family_name %in% c("beta_binomial", "beta"))
"gamma(2, 0.1)" else NULL
prior <- make_tdt_priors(model = model,
data = data,
lower = lower,
upper = upper,
random_effects = random_effects,
prior_phi = prior_phi,
ctmax = ctmax,
z = z,
up = up,
low = low,
k = k,
fix_asymptotes = fix_asymptotes)
}
# Fixed-effect moderators (e.g. species, life_stage) the fit varies over:
# direct mode reads them off ctmax/z/up/low/k; midpoint mode off up/low/k/mid
# plus the `by` shortcut. `grouped` is coding-independent, so the group-aware
# readers (tls(by=), ...) auto-derive per-group quantities
# under BOTH `~ 0 + G` and `~ 1 + G` instead of silently returning the
# reference level.
group_vars <- if (direct) direct_group_vars(ctmax, z, up, low, k)
else direct_group_vars(NULL, NULL, up, low, k, mid, by)
# `temp_effects` only governs the midpoint parameterisation; the direct
# CTmax/z fit ignores it, so don't record a misleading default-all-four value
# in a direct fit's meta.
meta_temp_effects <- if (direct) NULL else
match.arg(temp_effects, c("low", "up", "k", "mid"), several.ok = TRUE)
meta_full <- utils::modifyList(meta, list(
random_effects = random_effects,
temp_effects = meta_temp_effects,
lower = lower,
upper = upper,
bounds = compute_4pl_bounds(lower, upper),
response_type = response_type,
censored = censored,
n_censored = if (censored)
c(left = sum(data$censored == "left"),
right = sum(data$censored == "right")),
family = family$family,
link = family$link,
model = model,
parameterization = if (direct) "direct" else "midpoint",
threshold = threshold,
fix_asymptotes = fx_values,
t_ref = t_ref,
log10_tref = log10_tref,
group_vars = group_vars,
grouped = length(group_vars) > 0
))
workflow <- structure(
list(fit = NULL, data = data, data2 = data2, formula = formula,
prior = prior, meta = meta_full),
class = "bayes_tls"
)
if (!fit) return(workflow)
# The default priors set a general `class = "b"` catch-all on CTmaxdev/logz
# (needed only when CTmax/z carry a continuous moderator or interaction)
# alongside the per-level / Intercept priors. For the common cell-means or
# intercept-only fit every coefficient already has a specific prior, so brms
# emits a harmless "global prior ... will not be used in the model" note for
# the redundant catch-all. Muffle only that note (the fit is unaffected and
# the user cannot act on it); all other warnings pass through untouched.
workflow$fit <- withCallingHandlers(
brms::brm(
formula = formula,
data = data,
data2 = data2,
prior = prior,
chains = chains,
iter = iter,
warmup = warmup,
cores = cores,
seed = seed,
backend = backend,
control = control,
init = init,
file = file,
file_refit = file_refit,
...
),
warning = function(w) {
if (grepl("will not be used in the model", conditionMessage(w),
fixed = TRUE))
invokeRestart("muffleWarning")
}
)
# A cached `file` returned as-is (e.g. file_refit = "never") may hold a fit
# of a different curve than the one requested; refuse it rather than label a
# log-logistic fit "log-normal" (or vice versa) in meta.
got <- tdt_model_from_formula(workflow$fit$formula)
if (!is.na(got) && !identical(got, model))
stop(sprintf(paste0("The fit brms returned (file = \"%s\") is a %s model, ",
"but a %s model was requested. Delete the cached file ",
"or use a different `file`."),
file %||% "", got, model), call. = FALSE)
# Likewise a cached fit whose readings were (or were not) censored.
tdt_check_cached_censoring(workflow$fit$formula, censored, file)
workflow
}
# Refuse a fit (typically a cached `file` returned under file_refit = "never")
# whose response side disagrees with the requested censoring: a clamped fit
# labelled censored, or the reverse, would misreport print(), bayes_R2_tls()
# and the meaning of its log-likelihood.
tdt_check_cached_censoring <- function(fit_formula, censored, file = NULL) {
got <- tdt_fit_censored(fit_formula)
if (!identical(got, isTRUE(censored)))
stop(sprintf(paste0("The fit brms returned (file = \"%s\") has %s readings, ",
"but %s readings were requested. Delete the cached ",
"file or use a different `file`."),
file %||% "", if (got) "censored" else "uncensored",
if (isTRUE(censored)) "censored" else "uncensored"),
call. = FALSE)
invisible(TRUE)
}
#' Whether a workflow has been fitted
#'
#' @param workflow Object returned by [fit_4pl()].
#' @return Logical scalar.
#' @examples
#' wf <- list(fit = NULL); class(wf) <- "bayes_tls"
#' has_fit(wf)
#' @export
has_fit <- function(workflow) {
inherits(workflow, "bayes_tls") && !is.null(workflow$fit)
}
#' Extract the fitted brms model from a workflow
#'
#' Returns the underlying [brms::brmsfit] object held in a `bayes_tls`
#' workflow, ready for any brms / posterior / bayesplot helper (e.g.
#' [brms::bayes_R2()], [brms::mcmc_plot()], [posterior::as_draws_df()]).
#' Prefer this over reaching into `workflow$fit` directly: it errors clearly
#' on an unfitted workflow instead of silently returning `NULL`. Pairs with
#' the predicate [has_fit()].
#'
#' @param workflow A fitted `bayes_tls` workflow returned by [fit_4pl()].
#' @return The `brmsfit` object stored in the workflow.
#' @examples
#' \dontrun{
#' wf <- fit_4pl(std)
#' fit <- get_brmsfit(wf)
#' brms::bayes_R2(fit)
#' }
#' @export
get_brmsfit <- function(workflow) {
if (!has_fit(workflow))
stop("workflow has no fit; call fit_4pl() first.", call. = FALSE)
workflow$fit
}
# --- internal: family validation -------------------------------------------
# Validate (and lightly coerce) the brms family passed to make_4pl_formula() /
# fit_4pl(). Three jobs:
# 1. Reject non-family inputs with a clear message. A bare string (e.g.
# "binomial") is a plausible mistake -- ts_stage1() and brms::brm() accept
# string family names -- but make_4pl_formula() reads family$family /
# family$link, which on an atomic vector throws the cryptic "$ operator is
# invalid for atomic vectors". Coerce known string names via the matching
# brms constructor (identity link, matching the design's default).
# 2. Enforce identity link for the COUNT families (binomial, beta_binomial):
# the disjoint-bounds reparam keeps the 4PL output inside (low_min, up_max)
# so the 4PL value IS the mean, and only identity is wired up. A non-identity
# count link (e.g. binomial(link = "logit")) makes brms apply inv_logit() to
# a value that is already a probability -- a silent double transform. Catch
# it here with an actionable message rather than fitting a misspecified model.
# Returns the (possibly coerced) family.
validate_4pl_family <- function(family) {
if (is.character(family) && length(family) == 1L) {
ctor <- switch(family,
"binomial" = stats::binomial,
"beta_binomial" = brms::beta_binomial,
"beta" = brms::Beta,
"Beta" = brms::Beta,
NULL)
if (is.null(ctor))
stop(sprintf(paste0("`family` = \"%s\" is not a supported string family ",
"name. Pass a brms family object instead, e.g. ",
"beta_binomial(link = \"identity\"), ",
"binomial(link = \"identity\"), or ",
"brms::Beta(link = \"identity\")."), family),
call. = FALSE)
family <- ctor(link = "identity")
}
if (!inherits(family, c("brmsfamily", "family")))
stop("`family` must be a brms family object (e.g. ",
"beta_binomial(link = \"identity\")) or NULL, not a ",
class(family)[1], ".", call. = FALSE)
if (family$family %in% c("binomial", "beta_binomial") &&
!identical(family$link, "identity"))
stop(sprintf(paste0("Count family `%s` requires link = \"identity\" (got ",
"\"%s\"). The joint 4PL produces a probability directly ",
"and a non-identity link would double-transform it (brms ",
"would apply inv_%s() to an already-probability mean). ",
"Use %s(link = \"identity\")."),
family$family, family$link, family$link, family$family),
call. = FALSE)
family
}
# --- internal: formula-resolution helpers for the direct CTmax/z mode -------
# (Unexported. Kept after the documented functions so the roxygen @export blocks
# above attach to make_4pl_formula()/fit_4pl(), not to these helpers.)
# Fixed-effect moderator variable names in a formula RHS string, excluding
# `temp_c` and random-effect grouping factors. Empty character vector => no
# moderator. Coding-independent: both `0 + species` (cell-means) and `species`
# (treatment) yield "species".
rhs_fixed_vars <- function(rhs) {
if (is.null(rhs) || !nzchar(rhs)) return(character(0))
# AST-based bar removal (tdt_split_bars): the old regexes stopped at the
# first ")" and mangled nested grouping calls like (1 | gr(species, cov = A))
# into unparseable text (issue #12).
fixed <- tdt_split_bars(rhs)$fixed
if (fixed %in% c("", "1", "0")) return(character(0))
setdiff(all.vars(stats::as.formula(paste("~", fixed))), "temp_c")
}
# Fixed-effect moderator variable names across the parameterisation's formulas,
# plus any explicit `by` moderator. Direct mode reads ctmax/z/up/low/k; midpoint
# mode reads up/low/k/mid (+ by). Empty character vector => single-group fit.
direct_group_vars <- function(ctmax, z, up, low, k, mid = NULL, by = NULL) {
fixed_vars <- function(f) {
if (is.null(f) || !inherits(f, "formula")) return(character(0))
rhs_fixed_vars(formula_rhs(f, "1"))
}
unique(c(by, unlist(lapply(list(ctmax, z, up, low, k, mid), fixed_vars))))
}
# Rewrite a single-factor TREATMENT-coded one-sided formula (`~ G` or `~ 1 + G`,
# optionally carrying random-effect terms) to CELL-MEANS (`~ 0 + G [+ (re)]`).
# Treatment and cell-means coding span the same model, but only cell-means draws
# per-level CTmax/z priors; this rewrite makes the two codings fit the identical
# model with correct priors. Returns `f` unchanged unless the FIXED part is
# exactly one factor/character variable with an implicit intercept -- continuous
# moderators, interactions, `I()` terms, multiple fixed terms, and formulas that
# already drop the intercept (`0 +` / `- 1`) are left as-is.
normalize_cellmeans <- function(f, data) {
if (is.null(f) || !inherits(f, "formula")) return(f)
rhs <- formula_rhs(f, "1")
# AST-based split (tdt_split_bars): the old regexes truncated bar terms with
# nested parentheses -- (1 | gr(species, cov = A)) -- at the first ")".
sp <- tdt_split_bars(rhs)
re <- vapply(sp$bars, function(b)
paste0("(", paste(deparse(b), collapse = " "), ")"), character(1))
terms <- trimws(strsplit(sp$fixed, "\\+")[[1]])
terms <- terms[nzchar(terms)]
if (any(grepl("(^|[^[:alnum:]_.])0([^[:alnum:]_.]|$)|-\\s*1", terms)))
return(f) # already no-intercept
fixed <- setdiff(terms, "1") # drop an explicit intercept
if (length(fixed) != 1L) return(f) # one fixed term only
v <- fixed
if (!grepl("^[A-Za-z.][A-Za-z0-9._]*$", v)) return(f) # bare variable only
if (is.null(data) || !v %in% names(data)) return(f)
if (!(is.factor(data[[v]]) || is.character(data[[v]]))) return(f) # factors only
stats::as.formula(
paste("~", paste(c(paste0("0 + ", v), re), collapse = " + ")))
}
# RHS of a one-sided (or two-sided) formula as a string; `default` when NULL.
formula_rhs <- function(f, default) {
if (is.null(f)) return(default)
if (!inherits(f, "formula"))
stop("formula arguments (ctmax, z, up, low, k) must be one-sided formulas ",
"or NULL, e.g. `~ life_stage + (1 | Date)`.", call. = FALSE)
paste(deparse(f[[length(f)]]), collapse = " ")
}
# Resolve a shape sub-parameter (up/low/k) RHS. An explicit formula wins.
# Otherwise inherit the CTmax/z FIXED effects crossed with temp_c (so the
# absolute correction can differ per group); RANDOM effects are NOT inherited
# (they stay on CTmax/z); intercept-only CTmax/z gives `temp_c`.
resolve_shape <- function(shape_f, inherit_rhs) {
if (!is.null(shape_f)) return(formula_rhs(shape_f, "temp_c"))
# AST-based bar removal (tdt_split_bars); the old regex truncated nested
# grouping calls like (1 | gr(species, cov = A)) at the first ")".
fe <- trimws(tdt_split_bars(inherit_rhs)$fixed)
if (fe %in% c("", "1")) return("temp_c")
if (grepl("^0\\s*\\+", fe)) { # cell-means 0 + G
core <- trimws(sub("^0\\s*\\+\\s*", "", fe))
if (grepl("\\+", core)) paste0("0 + ", core, " + temp_c:(", core, ")")
else paste0("0 + ", core, " + temp_c:", core)
} else {
paste0("(", fe, ") * temp_c") # treatment G * temp_c
}
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.