R/formula.R

Defines functions tls_design_from_rhs tls_flatten_plus tls_parse_axes tls_parse_response tls_parse_formula tls_same_partition tls_re_terms tls_extract_re tls_re_bar_inner tls_is_re_bar tls_contains_random_bar tls_formula_rhs tls_formula_lhs tls_is_formula_call print.tls_formula tls_subparam_names tls_bf

Documented in tls_bf

#' Build a freqTLS formula object (brms/drmTMB-style)
#'
#' `tls_bf()` captures the per-sub-parameter formulas that define a `freqTLS`
#' model and returns them, unevaluated, as a `tls_formula` object. It is the
#' formula complement to the column interface of [fit_tls()]: instead of passing
#' bare column names, you write one response formula plus a formula per model
#' sub-parameter.
#'
#' The **first** argument must be the unnamed response-and-axes formula. Its
#' left-hand side names the survival counts, in either the brms idiom
#' `successes | trials(total)` or the `glm` idiom `cbind(successes, failures)`.
#' Its right-hand side names the two thermal-load-sensitivity axes with the
#' tagged markers `time(<duration>)` and `temp(<temperature>)` (order does not
#' matter).
#'
#' The remaining arguments are sub-parameter formulas keyed by their left-hand
#' side, one of `low`, `up`, `log_k`, `CTmax`, or `log_z`. Any sub-parameter you
#' omit defaults to `~ 1`. Each of `low`, `up`, and `log_k` may carry its
#' **own** design independently — a grouping factor (`low ~ group`), a
#' continuous covariate (`log_k ~ body_size`), or an intercept — and need not
#' share one factor or match the headline-parameter grouping. `CTmax` and `log_z`
#' accept fixed-effect formulas but must produce the same model-matrix columns
#' (for example, use `CTmax ~ group, log_z ~ group`); their supported random-
#' intercept groupings may differ. `CTmax` accepts multiple additive independent
#' intercepts, such as `CTmax ~ 1 + (1 | plant) + (1 | meas_day)`, when they
#' encode distinct grouping partitions. Aliased grouping columns are rejected
#' because only the sum of their variances is identifiable. `log_z`,
#' `low`, and `log_k` accept at most one grouping factor each. All random
#' intercepts are independent; putting the same grouping factor on two or more
#' coordinates fits separate variances (no correlation term) and warns. Random
#' intercepts are not supported on the upper asymptote `up`. Phylogenetic
#' covariance structures (for
#' example, `gr(species, cov = tree)`) are not implemented; use `bayesTLS` for
#' that model.
#'
#' @section Parser provenance:
#' The shape of the parser (variadic capture via [substitute()], a per-entry
#' formula walk, and random-bar detection) is adapted from drmTMB's
#' `drm_formula()` / `parse_drm_formula_entry()` (GPL-3); see `inst/COPYRIGHTS`.
#' freqTLS writes its own grammar (the `time()` / `temp()` axis markers, the
#' five fixed sub-parameter handles, and the package's supported random-effect
#' grammar).
#'
#' @param ... The response-and-axes formula first (unnamed, with a left-hand
#'   side), then sub-parameter formulas keyed by their left-hand side. See the
#'   grammar above.
#'
#' @return A `tls_formula` object: a list with the captured response formula,
#'   the named sub-parameter formulas, and the calling environment.
#'
#' @seealso [fit_tls()], which accepts either a `tls_formula` or the column
#'   interface.
#'
#' @examples
#' tls_bf(
#'   survived | trials(total) ~ time(duration) + temp(temp),
#'   CTmax ~ population,
#'   log_z ~ population
#' )
#' # cbind() response idiom, ungrouped:
#' tls_bf(cbind(survived, died) ~ time(duration) + temp(temp))
#'
#' @export
tls_bf <- function(...) {
  env <- parent.frame()
  calls <- as.list(substitute(list(...)))[-1L]
  if (length(calls) == 0L) {
    cli::cli_abort("{.fn tls_bf} requires at least the response-and-axes formula.")
  }

  nms <- names(calls)
  if (is.null(nms)) nms <- rep("", length(calls))
  nms[is.na(nms)] <- ""

  # Every argument must be a formula call.
  is_formula <- vapply(calls, tls_is_formula_call, logical(1))
  if (!all(is_formula)) {
    bad <- which(!is_formula)[1L]
    cli::cli_abort(c(
      "{.fn tls_bf} inputs must be formulas.",
      x = "Input {bad} ({.code {deparse1(calls[[bad]])}}) is not a formula."
    ))
  }

  # The first formula is the response-and-axes formula (must have an LHS).
  response_formula <- calls[[1L]]
  if (is.null(tls_formula_lhs(response_formula))) {
    cli::cli_abort(c(
      "The first {.fn tls_bf} formula must be the response-and-axes formula.",
      i = "Write it as {.code successes | trials(total) ~ time(<dur>) + temp(<temp>)} or {.code cbind(successes, failures) ~ time(<dur>) + temp(<temp>)}."
    ))
  }

  # Remaining formulas are sub-parameter formulas keyed by LHS name.
  sub_formulas <- list()
  if (length(calls) > 1L) {
    for (i in seq.int(2L, length(calls))) {
      f <- calls[[i]]
      lhs <- tls_formula_lhs(f)
      if (is.null(lhs)) {
        cli::cli_abort(c(
          "Sub-parameter formulas must name a sub-parameter on the left-hand side.",
          x = "Input {i} ({.code {deparse1(f)}}) has no left-hand side.",
          i = "Use one of {.val {tls_subparam_names()}}, e.g. {.code CTmax ~ group}."
        ))
      }
      handle <- deparse1(lhs)
      if (!handle %in% tls_subparam_names()) {
        cli::cli_abort(c(
          "Unknown sub-parameter handle {.val {handle}}.",
          i = "Valid sub-parameters are {.val {tls_subparam_names()}}."
        ))
      }
      if (!is.null(sub_formulas[[handle]])) {
        cli::cli_abort("Sub-parameter {.val {handle}} is given more than once.")
      }
      sub_formulas[[handle]] <- f
    }
  }

  out <- list(
    response = response_formula,
    sub_formulas = sub_formulas,
    env = env
  )
  class(out) <- "tls_formula"
  out
}

#' The set of valid sub-parameter handles
#' @keywords internal
#' @noRd
tls_subparam_names <- function() {
  c("low", "up", "log_k", "CTmax", "log_z")
}

#' @export
print.tls_formula <- function(x, ...) {
  cli::cli_text("<tls_formula>")
  cli::cli_text("  {deparse1(x$response)}")
  for (handle in names(x$sub_formulas)) {
    cli::cli_text("  {deparse1(x$sub_formulas[[handle]])}")
  }
  invisible(x)
}

# ---- formula primitives -----------------------------------------------------
# These mirror the small formula-walking helpers in drmTMB's parse-formula.R
# (GPL-3; see inst/COPYRIGHTS).

#' Is `expr` a formula call (`~`)?
#' @keywords internal
#' @noRd
tls_is_formula_call <- function(expr) {
  is.call(expr) && identical(expr[[1L]], as.name("~"))
}

#' Left-hand side of a formula call, or `NULL` for a one-sided formula.
#' @keywords internal
#' @noRd
tls_formula_lhs <- function(expr) {
  if (length(expr) < 3L) return(NULL)
  expr[[2L]]
}

#' Right-hand side of a formula call.
#' @keywords internal
#' @noRd
tls_formula_rhs <- function(expr) {
  expr[[length(expr)]]
}

#' Does `expr` contain a random-effect bar (`a | b`) as a call?
#'
#' Detects the `lme4`/`brms` random-effect grouping operator. A bare `successes
#' | trials(total)` response is handled separately (we only call this on
#' sub-parameter right-hand sides), so any `|` here is a random effect.
#' @keywords internal
#' @noRd
tls_contains_random_bar <- function(expr) {
  if (is.call(expr)) {
    if (identical(expr[[1L]], as.name("|"))) return(TRUE)
    return(any(vapply(as.list(expr)[-1L], tls_contains_random_bar, logical(1))))
  }
  FALSE
}

#' Is `term` a top-level random-effects term, i.e. `(a | b)` or `a | b`?
#' @keywords internal
#' @noRd
tls_is_re_bar <- function(term) {
  if (is.call(term) && identical(term[[1L]], as.name("("))) {
    inner <- term[[2L]]
    return(is.call(inner) && identical(inner[[1L]], as.name("|")))
  }
  is.call(term) && identical(term[[1L]], as.name("|"))
}

#' The `a | b` call inside a (possibly parenthesised) random-effects term.
#' @keywords internal
#' @noRd
tls_re_bar_inner <- function(term) {
  if (identical(term[[1L]], as.name("("))) term[[2L]] else term
}

#' Split a sub-parameter right-hand side into its fixed part and a random intercept
#'
#' freqTLS supports independent random intercepts on `CTmax`, `log_z`, `low`, and
#' `log_k`, `<param> ~ <fixed> + (1 | group)`. `CTmax` may have multiple additive
#' grouping terms. This separates the random terms from the fixed terms and
#' validates the scope (intercept only, the
#' column exists), and returns the fixed-effect right-hand side plus an `re` spec
#' (or `NULL`). `param` is the sub-parameter name (`"CTmax"`, `"log_z"`, `"low"`,
#' or `"log_k"`), used in the messages and to name the affected variance component.
#' @keywords internal
#' @noRd
tls_extract_re <- function(rhs_expr, data, param = "CTmax", quiet = FALSE,
                           allow_multiple = FALSE) {
  sig_label <- switch(param, log_z = "sigma_logz", low = "sigma_low",
                      log_k = "sigma_logk", "sigma_CTmax")
  terms <- tls_flatten_plus(rhs_expr)
  is_bar <- vapply(terms, tls_is_re_bar, logical(1))
  bars <- terms[is_bar]
  if (length(bars) > 1L && !allow_multiple) {
    cli::cli_abort(c(
      "Only one random-effects term is supported on {.code {param}}.",
      i = "Multiple grouping factors are supported on {.code CTmax} only."
    ))
  }
  re_specs <- lapply(bars, function(bar) {
    inner <- tls_re_bar_inner(bar)
    re_lhs <- inner[[2L]]
    re_group <- inner[[3L]]
    if (!identical(deparse1(re_lhs), "1")) {
      cli::cli_abort(c(
        "Only a random intercept {.code (1 | group)} is supported on {.code {param}}.",
        x = "Got {.code ({deparse1(re_lhs)} | {deparse1(re_group)})}; random slopes are not supported."
      ))
    }
    if (is.call(re_group) && identical(as.character(re_group[[1L]]), "gr")) {
      cli::cli_abort(c(
        "Phylogenetic covariance random effects are not implemented in {.pkg freqTLS}.",
        x = "Got {.code (1 | {deparse1(re_group)})}.",
        i = "freqTLS supports only independent {.code (1 | group)} intercepts; use {.pkg bayesTLS} for {.code gr(..., cov = ...)} phylogenetic structures."
      ))
    }
    if (!is.name(re_group)) {
      cli::cli_abort(c(
        "The random-effects grouping must be a single column, e.g. {.code (1 | colony)}.",
        x = "Got {.code {deparse1(re_group)}}; nested / interaction groupings are not supported."
      ))
    }
    gname <- deparse1(re_group)
    if (!gname %in% names(data)) {
      cli::cli_abort("The random-effects grouping column {.code {gname}} is not in {.arg data}.")
    }
    g <- factor(data[[gname]])
    if (anyNA(g)) {
      cli::cli_abort("The random-effects grouping column {.code {gname}} must not contain missing values.")
    }
    if (!isTRUE(quiet) && nlevels(g) < 8L) {
      cli::cli_warn(c(
        "The random-effects grouping {.code {gname}} has only {nlevels(g)} level{?s}.",
        i = "With few groups, {.code {sig_label}} may be weakly identified; interpret its estimate and interval cautiously.",
        i = "Small-sample coverage of Wald, profile, and bootstrap intervals is under evaluation; no method is recommended here."
      ))
    }
    list(index = as.integer(g) - 1L, n = nlevels(g),
         group_levels = levels(g), group_var = gname)
  })
  if (length(re_specs) > 1L) {
    group_vars <- vapply(re_specs, `[[`, character(1), "group_var")
    if (anyDuplicated(group_vars)) {
      cli::cli_abort("Each random-intercept grouping factor may appear only once on {.code {param}}.")
    }
    for (i in seq_along(re_specs)) {
      if (i == 1L) next
      for (j in seq_len(i - 1L)) {
        if (tls_same_partition(re_specs[[i]]$index, re_specs[[j]]$index)) {
          cli::cli_abort(c(
            "Random-intercept grouping columns {.val {group_vars[[j]]}} and {.val {group_vars[[i]]}} encode the same grouping partition.",
            i = "Only one variance component is identifiable for this partition; keep a single random-intercept term."
          ))
        }
      }
    }
  }
  re <- if (length(re_specs) == 0L) NULL else if (length(re_specs) == 1L) {
    re_specs[[1L]]
  } else re_specs

  fixed_terms <- terms[!is_bar]
  fixed_rhs <- if (length(fixed_terms) == 0L) {
    ~1
  } else {
    stats::reformulate(vapply(fixed_terms, deparse1, character(1)))
  }
  list(fixed_rhs = fixed_rhs, re = re)
}

# Normalize a single RE descriptor or a list of descriptors to a list.
tls_re_terms <- function(x) {
  if (is.null(x)) return(list())
  if (is.list(x) && !is.null(x$index)) return(list(x))
  x
}

# Whether two grouping vectors encode the same partition, allowing their
# labels and level order to differ. A one-to-one mapping between occupied
# groups in the contingency table is the defining property.
tls_same_partition <- function(x, y) {
  if (length(x) != length(y)) return(FALSE)
  occupied <- table(factor(x), factor(y)) > 0L
  all(rowSums(occupied) == 1L) && all(colSums(occupied) == 1L)
}

# ---- the parser -------------------------------------------------------------

#' Parse a `tls_formula` against a data frame into a fit specification
#'
#' Resolves the response counts, the time / temperature axes, and the `CTmax` /
#' `log_z` design matrices from a [tls_bf()] object. The returned spec carries
#' exactly what [fit_tls()] needs to run the existing engine: the count vectors,
#' the axis vectors, the (shared) design matrix and its column labels, and a
#' `grouped` flag.
#'
#' Restrictions enforced here:
#' * each sub-parameter takes its own fixed-effect design (intercept, grouping
#'   factor, or continuous covariate).
#' * random-effect bars (`(1 | group)`) are parsed on `CTmax`, `log_z`, `low`, and
#'   `log_k` (upstream, by `tls_extract_re()`), one grouping factor each; a bar on
#'   the upper asymptote `up` is rejected (no random-intercept term in the objective).
#' * the `CTmax` and `log_z` FIXED designs must produce the same columns (the
#'   engine accepts independent lengths, but the downstream profile / contrast /
#'   print machinery is keyed on one shared label set); their RE groupings may
#'   differ.
#'
#' @param formula A `tls_formula` from [tls_bf()].
#' @param data A data frame to resolve columns against.
#' @return A list with `y`, `n`, `time`, `temp` (numeric vectors), `X_CT`,
#'   `X_logz` (numeric matrices), `levels` (design column labels), and `grouped`.
#' @keywords internal
#' @noRd
tls_parse_formula <- function(formula, data, quiet = FALSE) {
  if (!inherits(formula, "tls_formula")) {
    cli::cli_abort("{.arg formula} must be a {.cls tls_formula} from {.fn tls_bf}.")
  }
  if (!is.data.frame(data)) {
    cli::cli_abort("{.arg data} must be a data frame when using {.fn tls_bf}.")
  }

  # ---- response counts -----------------------------------------------------
  counts <- tls_parse_response(formula$response, data)

  # ---- axes ----------------------------------------------------------------
  axes <- tls_parse_axes(formula$response, data)

  # ---- CTmax / log_z design matrices ---------------------------------------
  ct_f <- formula$sub_formulas[["CTmax"]]
  lz_f <- formula$sub_formulas[["log_z"]]

  # CTmax may carry multiple additive random intercepts; log_z may carry one.
  # Their grouping factors may differ. The bars are stripped here, so the
  # shared-design comparison below sees only the fixed parts.
  ct_rhs_expr <- if (is.null(ct_f)) quote(1) else tls_formula_rhs(ct_f)
  lz_rhs_expr <- if (is.null(lz_f)) quote(1) else tls_formula_rhs(lz_f)
  ct_re <- tls_extract_re(ct_rhs_expr, data, "CTmax", quiet = quiet,
                          allow_multiple = TRUE)
  lz_re <- tls_extract_re(lz_rhs_expr, data, "log_z", quiet = quiet)

  # (The same-grouping independent-variance warning is emitted once below, after
  # all four possible RE blocks -- CTmax, log_z, low, log_k -- have been parsed.)

  ct_design <- tls_design_from_rhs(ct_re$fixed_rhs, data, "CTmax")
  lz_design <- tls_design_from_rhs(lz_re$fixed_rhs, data, "log_z")

  # CTmax and log_z share one fixed-effect label set downstream. The engine
  # accepts different lengths, but the profile / contrast / print machinery does
  # not. Only the FIXED parts must match here; the random-effect bars were stripped
  # above, so the two RE groupings may differ.
  if (!identical(colnames(ct_design$X), colnames(lz_design$X))) {
    cli::cli_abort(c(
      "{.code CTmax} and {.code log_z} must use the same fixed-effect predictors.",
      x = "{.code CTmax} columns: {.val {colnames(ct_design$X)}}.",
      x = "{.code log_z} columns: {.val {colnames(lz_design$X)}}.",
      i = "Give them the same right-hand side; their random-effect groupings may still differ."
    ))
  }

  # ---- shape sub-parameter designs (low, up via disjoint bounds, log_k) -----
  # Intercept-only by default; each may be grouped or carry a continuous covariate
  # and `low` and `log_k` may also carry a single random intercept `(1 | group)`
  # (the RE bar is stripped here and the fixed design built from the
  # fixed part). Under disjoint bounds `up` has its own coordinate `beta_up`, but
  # the compiled objective has no random-intercept term for it, so a RE bar on `up`
  # is rejected by tls_design_from_rhs (as `up` is for profiling).
  shape_rhs_expr <- function(handle) {
    f <- formula$sub_formulas[[handle]]
    if (is.null(f)) quote(1) else tls_formula_rhs(f)
  }
  low_re <- tls_extract_re(shape_rhs_expr("low"), data, "low", quiet = quiet)
  logk_re <- tls_extract_re(shape_rhs_expr("log_k"), data, "log_k", quiet = quiet)
  low_rhs <- low_re$fixed_rhs
  logk_rhs <- logk_re$fixed_rhs
  up_expr <- shape_rhs_expr("up")
  up_rhs <- if (identical(up_expr, quote(1))) ~1 else stats::reformulate(deparse1(up_expr))
  low_design <- tls_design_from_rhs(low_rhs, data, "low")
  gap_design <- tls_design_from_rhs(up_rhs, data, "up")
  logk_design <- tls_design_from_rhs(logk_rhs, data, "log_k")
  # Each shape sub-parameter carries its own design INDEPENDENTLY (intercept,
  # grouping factor, or continuous covariate); `low` and `log_k` may additionally
  # carry a random intercept. predict() rebuilds each shape design from `newdata`
  # via `shape_terms` (the fixed part); no same-predictor / match-CTmax constraint.

  # Independent REs only: when two or more random intercepts share a grouping
  # factor, freqTLS fits them as INDEPENDENT variances (no correlation term).
  # The group-level deviations are usually correlated, and that correlation is then
  # absorbed into the marginal SDs and the fixed-effect intervals -- an honest
  # hazard, so warn (once per shared grouping).
  ct_re_terms <- tls_re_terms(ct_re$re)
  ct_named_terms <- if (length(ct_re_terms)) {
    stats::setNames(ct_re_terms, paste0("CTmax[", seq_along(ct_re_terms), "]"))
  } else list()
  re_present <- c(
    ct_named_terms,
    Filter(Negate(is.null),
      list(log_z = lz_re$re, low = low_re$re, log_k = logk_re$re))
  )
  if (!isTRUE(quiet) && length(re_present) >= 2L) {
    gvars <- vapply(re_present, function(s) s$group_var, character(1))
    for (gv in unique(gvars[duplicated(gvars)])) {
      shared <- names(re_present)[gvars == gv]
      cli::cli_warn(c(
        "Random intercepts on {.val {shared}} share the grouping {.code {gv}}.",
        i = "freqTLS fits these as {.strong independent} variances (no correlation term); any true correlation between the group-level deviations is absorbed into the marginal SDs and the fixed-effect intervals.",
        i = "For a correlated random effect, use {.pkg bayesTLS}."
      ))
    }
  }

  list(
    y = counts$y,
    n = counts$n,
    time = axes$time,
    temp = axes$temp,
    temp_variable = axes$temp_variable,
    X_CT = ct_design$X,
    X_logz = lz_design$X,
    X_low = low_design$X,
    X_up = gap_design$X,
    X_logk = logk_design$X,
    levels = colnames(ct_design$X),
    grouped = ct_design$grouped,
    # `[[` (exact) not `$`: an ungrouped design has `grouped` but no `group` key,
    # and `$` would partial-match `group` to `grouped` (FALSE).
    group = ct_design[["group"]],
    # Per-shape right-hand sides, so predict() can rebuild the shape designs from
    # newdata (NULL-safe: an intercept-only shape rebuilds to a column of ones).
    fixed_terms = list(CTmax = ct_re$fixed_rhs, log_z = lz_re$fixed_rhs),
    shape_terms = list(low = low_rhs, up = up_rhs, log_k = logk_rhs),
    shape_profileable = list(low = low_design$profileable,
                             up = gap_design$profileable,
                             log_k = logk_design$profileable),
    fixed_model_terms = list(CTmax = ct_design$terms, log_z = lz_design$terms),
    shape_model_terms = list(low = low_design$terms, up = gap_design$terms,
                             log_k = logk_design$terms),
    re = ct_re$re,
    re_logz = lz_re$re,
    re_low = low_re$re,
    re_logk = logk_re$re
  )
}

#' Resolve the response left-hand side into successes / trials vectors
#'
#' Accepts `successes | trials(total)` (brms idiom: successes + trials columns)
#' or `cbind(successes, failures)` (glm idiom: total = successes + failures).
#' @keywords internal
#' @noRd
tls_parse_response <- function(response_formula, data) {
  lhs <- tls_formula_lhs(response_formula)

  eval_col <- function(expr, role) {
    v <- tryCatch(
      eval(expr, envir = data, enclos = parent.frame()),
      error = function(e) {
        cli::cli_abort(c(
          "Could not find the {role} column {.code {deparse1(expr)}} in {.arg data}.",
          x = conditionMessage(e)
        ))
      }
    )
    if (!is.numeric(v)) {
      cli::cli_abort("The {role} column {.code {deparse1(expr)}} must be numeric.")
    }
    v
  }

  # brms idiom: successes | trials(total)
  if (is.call(lhs) && identical(lhs[[1L]], as.name("|"))) {
    successes_expr <- lhs[[2L]]
    rhs_marker <- lhs[[3L]]
    if (!is.call(rhs_marker) || !identical(rhs_marker[[1L]], as.name("trials"))) {
      cli::cli_abort(c(
        "The response after {.code |} must be {.fn trials}.",
        i = "Write the response as {.code successes | trials(total)}."
      ))
    }
    if (length(rhs_marker) != 2L) {
      cli::cli_abort("{.fn trials} takes exactly one column, e.g. {.code trials(total)}.")
    }
    y <- eval_col(successes_expr, "successes")
    n <- eval_col(rhs_marker[[2L]], "trials")
    return(list(y = as.numeric(y), n = as.numeric(n)))
  }

  # glm idiom: cbind(successes, failures)
  if (is.call(lhs) && identical(lhs[[1L]], as.name("cbind"))) {
    if (length(lhs) != 3L) {
      cli::cli_abort(c(
        "{.fn cbind} response needs exactly two columns.",
        i = "Write it as {.code cbind(successes, failures)}."
      ))
    }
    y <- eval_col(lhs[[2L]], "successes")
    failures <- eval_col(lhs[[3L]], "failures")
    if (length(y) != length(failures)) {
      cli::cli_abort("The two {.fn cbind} columns must have equal length.")
    }
    return(list(y = as.numeric(y), n = as.numeric(y) + as.numeric(failures)))
  }

  # Bare-name response: a continuous proportion in (0, 1) for the beta family.
  # There is no trials column; fit_tls() supplies a dummy `n` and validates the
  # (0, 1) range. A bare name combined with a count family then triggers the
  # clear "`n` (trials) is required" error downstream.
  if (is.name(lhs)) {
    y <- eval_col(lhs, "response")
    return(list(y = as.numeric(y), n = NULL))
  }

  cli::cli_abort(c(
    "Unrecognised response specification {.code {deparse1(lhs)}}.",
    i = "Use {.code successes | trials(total)}, {.code cbind(successes, failures)}, or a bare proportion column for {.code family = \"beta\"}."
  ))
}

#' Resolve the time() and temp() axis markers into numeric vectors
#' @keywords internal
#' @noRd
tls_parse_axes <- function(response_formula, data) {
  rhs <- tls_formula_rhs(response_formula)
  terms <- tls_flatten_plus(rhs)

  time_expr <- NULL
  temp_expr <- NULL
  for (term in terms) {
    if (is.call(term) && identical(term[[1L]], as.name("time"))) {
      if (!is.null(time_expr)) cli::cli_abort("{.fn time} axis is given more than once.")
      if (length(term) != 2L) {
        cli::cli_abort("{.fn time} takes exactly one column, e.g. {.code time(duration)}.")
      }
      time_expr <- term[[2L]]
    } else if (is.call(term) && identical(term[[1L]], as.name("temp"))) {
      if (!is.null(temp_expr)) cli::cli_abort("{.fn temp} axis is given more than once.")
      if (length(term) != 2L) {
        cli::cli_abort("{.fn temp} takes exactly one column, e.g. {.code temp(temperature)}.")
      }
      temp_expr <- term[[2L]]
    } else {
      cli::cli_abort(c(
        "Untagged term {.code {deparse1(term)}} on the response right-hand side.",
        i = "Name the axes with the markers {.code time(<duration>)} and {.code temp(<temperature>)}."
      ))
    }
  }

  if (is.null(time_expr)) {
    cli::cli_abort(c(
      "The response formula is missing the {.fn time} axis.",
      i = "Add {.code time(<duration>)} to the right-hand side."
    ))
  }
  if (is.null(temp_expr)) {
    cli::cli_abort(c(
      "The response formula is missing the {.fn temp} axis.",
      i = "Add {.code temp(<temperature>)} to the right-hand side."
    ))
  }

  eval_axis <- function(expr, role) {
    v <- tryCatch(
      eval(expr, envir = data, enclos = parent.frame()),
      error = function(e) {
        cli::cli_abort(c(
          "Could not find the {role} column {.code {deparse1(expr)}} in {.arg data}.",
          x = conditionMessage(e)
        ))
      }
    )
    if (!is.numeric(v)) {
      cli::cli_abort("The {role} column {.code {deparse1(expr)}} must be numeric.")
    }
    as.numeric(v)
  }

  list(
    time = eval_axis(time_expr, "time"),
    temp = eval_axis(temp_expr, "temp"),
    # Prediction APIs use `temp` for the fitted temperature coordinate. Retain
    # its source column so temperature-dependent shape formulas can be rebuilt
    # from that coordinate when it was named differently in `temp(<column>)`.
    temp_variable = if (is.name(temp_expr)) as.character(temp_expr) else NULL
  )
}

#' Flatten an additive (`+`) right-hand side into a list of terms
#' @keywords internal
#' @noRd
tls_flatten_plus <- function(expr) {
  if (is.call(expr) && identical(expr[[1L]], as.name("+"))) {
    return(c(tls_flatten_plus(expr[[2L]]), tls_flatten_plus(expr[[3L]])))
  }
  list(expr)
}

#' Build a CTmax / log_z design matrix from a sub-parameter right-hand side
#'
#' Mirrors the column-interface behaviour: the special case `~ group` for a
#' single factor emits `~ 0 + group` (so labels and the fit are byte-identical to
#' the column interface). Any other formula keeps the intercept and labels
#' columns by their model-matrix names.
#' @keywords internal
#' @noRd
tls_design_from_rhs <- function(rhs, data, role, fitted_terms = NULL,
                                fitted_columns = NULL) {
  if (tls_contains_random_bar(tls_formula_rhs(rhs))) {
    cli::cli_abort(c(
      "Random intercepts are supported on {.code CTmax}, {.code log_z}, {.code low}, and {.code log_k}, but not {.code up}.",
      i = "Multiple additive independent grouping terms are supported on {.code CTmax}; {.code log_z}, {.code low}, and {.code log_k} support one each. Use a fixed-effects formula for {.code up}."
    ))
  }

  variables <- all.vars(rhs)
  missing_predictors <- variables[vapply(variables, function(variable) {
    variable %in% names(data) && anyNA(data[[variable]])
  }, logical(1))]
  if (length(missing_predictors)) {
    cli::cli_abort(c(
      "The {.code {role}} design predictors must not contain missing values.",
      i = "Missing values found in {.val {paste(missing_predictors, collapse = ', ')}}. Remove or impute these rows before fitting."
    ))
  }

  # Evaluate the complete RHS while preserving input rows. `model.matrix()`
  # defaults to na.omit and can silently discard rows when a transformation
  # (for example sqrt(x) for x < 0) creates NA/NaN values.
  mf <- tryCatch(
    stats::model.frame(fitted_terms %||% rhs, data = data,
                       na.action = stats::na.pass),
    error = function(e) cli::cli_abort(c(
      "Could not evaluate the {.code {role}} predictor formula.",
      i = "Check predictor transformations and supply finite values for every row.",
      x = conditionMessage(e)
    ))
  )
  if (nrow(mf) != nrow(data) || anyNA(mf) ||
      any(vapply(mf, function(x) is.numeric(x) && any(!is.finite(x)), logical(1)))) {
    cli::cli_abort(c(
      "The evaluated {.code {role}} predictors must be finite for every input row.",
      i = "A missing or non-finite predictor value was found after evaluating the formula."
    ))
  }

  terms_obj <- attr(mf, "terms")
  rhs_terms <- attr(terms_obj, "term.labels")
  has_intercept <- attr(terms_obj, "intercept") == 1L

  # Intercept-only: a single "(Intercept)" column, ungrouped.
  if (length(rhs_terms) == 0L) {
    if (!has_intercept) {
      cli::cli_abort("A {.code {role}} formula must have at least an intercept or a predictor.")
    }
    X <- matrix(1, nrow = nrow(data), ncol = 1L,
                dimnames = list(NULL, "all"))
    return(list(X = X, grouped = FALSE, terms = terms_obj,
                profileable = TRUE))
  }

  # Special case a single factor term, with OR without an intercept -- `~ factor`
  # and the `by=` cell-means form `~ 0 + factor` both resolve to per-group
  # cell-means here, labelled by the bare factor levels so the design and labels
  # match the column interface exactly (clean `<level>`, not model.matrix's
  # `<factorname><level>`). Continuous `~ 0 + x` terms are excluded by the
  # factor/character test and fall through to the general path below.
  single_factor <- length(rhs_terms) == 1L &&
    rhs_terms[[1L]] %in% names(data) &&
    (is.factor(data[[rhs_terms[[1L]]]]) || is.character(data[[rhs_terms[[1L]]]]))

  if (single_factor) {
    value <- mf[[rhs_terms[[1L]]]]
    lev <- fitted_columns %||% if (is.factor(value)) levels(value) else NULL
    g <- if (is.null(lev)) factor(value) else factor(value, levels = lev)
    if (any(is.na(g))) {
      cli::cli_abort(c(
        "The {.code {role}} grouping column has missing or unknown levels.",
        i = "Supply values from the factor levels used to fit the model."
      ))
    }
    mf[[rhs_terms[[1L]]]] <- g
    X <- stats::model.matrix(
      stats::reformulate(rhs_terms[[1L]], intercept = FALSE),
      data = mf
    )
    colnames(X) <- levels(g)
    attr(X, "assign") <- NULL
    attr(X, "contrasts") <- NULL
    # Surface the per-row labels so the formula interface can carry a grouping
    # vector for diagnostics / diag_data, matching the column interface.
    return(list(X = X, grouped = TRUE, group = as.character(g),
                terms = terms_obj, profileable = TRUE))
  }

  # General fixed-effect formula: keep the intercept, label by model-matrix
  # column names. Coefficients are surfaced downstream as CTmax:<colname>.
  X <- stats::model.matrix(terms_obj, data = mf)
  if (nrow(X) != nrow(data) || anyNA(X) || any(!is.finite(X))) {
    cli::cli_abort(c(
      "The {.code {role}} design matrix must have one finite row per input row.",
      i = "Check predictor transformations and missing values before fitting."
    ))
  }
  attr(X, "assign") <- NULL
  attr(X, "contrasts") <- NULL
  list(X = X, grouped = ncol(X) > 1L, terms = terms_obj,
       profileable = tls_is_onehot(X))
}

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.