standardize_data: Standardise a raw survival / proportion dataset for the TDT...

View source: R/standardize_data.R

standardize_dataR Documentation

Standardise a raw survival / proportion dataset for the TDT function library

Description

Rewrites user column names into the common data schema used by fit_4pl() and the TDT compatibility helpers. Direct fit_tls() column and tls_bf() formula fits can use a data frame without this conversion; supply durations in minutes for those interfaces.

Usage

standardize_data(
  data,
  temp,
  duration,
  n_total = NULL,
  n_surv = NULL,
  n_dead = NULL,
  survival = NULL,
  mortality = NULL,
  proportion = NULL,
  proportion_eps = 0.001,
  random_effects = NULL,
  duration_unit = NULL,
  temp_mean = NULL,
  left_censored = NULL,
  censor_limit = NULL
)

Arguments

data

Raw data frame or tibble.

temp

Column name of the assay temperature (°C).

duration

Column name of the exposure duration. Supply its source unit with duration_unit; standardize_data() converts the values to minutes.

n_total

Column name for total individuals per replicate. Required for count responses; leave NULL (default) for a continuous proportion response.

n_surv

Column name for survivor counts.

n_dead

Column name for death counts. Converted to n_surv via n_surv = n_total - n_dead.

survival

Column name for survival proportions in ⁠[0, 1]⁠. Converted to integer counts via n_total.

mortality

Column name for mortality proportions in ⁠[0, 1]⁠. Converted to n_surv = round((1 - mortality) * n_total).

proportion

Column name for a continuous proportion response in ⁠[0, 1]⁠ with no denominator (modelled with a Beta likelihood). Mutually exclusive with the count arguments above.

proportion_eps

Boundary clamp applied to exact proportion values so they sit strictly inside ⁠(0, 1)⁠ (the Beta density is undefined at exactly 0 or 1). With censoring columns, exact interior values and flagged placeholders are preserved, and an upper-boundary clamp must remain at or above its detection limit. Must be one finite value in ⁠(0, 0.5)⁠. Default 0.001.

random_effects

Optional character vector of grouping variables for random effects, e.g. c("Date", "Tank"). These columns are converted to factors and recorded in the standardization metadata; this argument does not add model effects. Specify supported random intercepts in the ctmax, z, low, or k formula passed to fit_4pl(). Group names cannot collide with columns created by standardization.

duration_unit

Unit of the input duration column. A recognised value ("seconds", "minutes", "hours", or "days", with common abbreviations) is required. Durations are converted to minutes, so tref / t_ref is always in minutes and the one-hour default is 60. This argument is required.

temp_mean

Value to subtract from temp to form temp_c. NULL (default) uses mean(temp). Supply a fixed value to align multiple datasets to a common centre.

left_censored

Optional column name containing non-missing logical indicators for measurements known only to lie below a detection limit. Must be paired with censor_limit; zero values are not inferred to be censored. An exact response below its declared limit is an error before boundary clamping; an exact value equal to its limit is allowed.

censor_limit

Optional numeric column name with one lower detection limit per observation. Values must be finite and strictly inside ⁠(0, 1)⁠.

Details

Two response types are supported:

  • Count data (binomial / beta-binomial): supply n_total plus exactly one of n_surv, n_dead, survival, or mortality. The other counts are derived and the standardised columns include n_total, n_surv, n_dead, survival. response_type is recorded as "count".

  • Continuous proportion (Beta), e.g. a chlorophyll-fluorescence F_v/F_m ratio with no denominator: supply proportion and omit the count arguments. The value is stored in survival; without censoring, values are clamped into ⁠(proportion_eps, 1 - proportion_eps)⁠. For explicit left-censored measurements, provide both censoring columns below. Their placeholders and exact interior proportions are retained; exact boundary values are clamped only when this keeps them above their detection limit. A warning reports affected rows whenever values are changed; no n_total/n_surv columns are created. response_type is recorded as "proportion".

If the dataset spans multiple categories (life stages, species, populations, etc.), retain the grouping column. Use fit_4pl(by = "group") or grouped formulas in tls_bf() to estimate category-level effects, or filter first when separate models are scientifically preferable.

Value

A tibble with the standardised columns plus a "tdt_meta" attribute storing temp_mean, duration_unit (always "minutes"), input_duration_unit, random_effects, response_type ("count" or "proportion"), response_var (the response column name for a proportion fit, else NULL), proportion_eps (the clamp used for a proportion fit, else NULL), and has_left_censoring (whether censoring columns were explicitly requested for a proportion response).

Examples

# Count data
raw <- data.frame(
  temperature_C = rep(c(30, 32, 34), each = 4),
  exposure_h    = rep(c(1, 2, 4, 8), times = 3),
  n             = 30L,
  alive         = c(29, 28, 25, 5, 30, 27, 18, 2, 28, 22, 10, 1)
)
standardize_data(raw,
                 temp     = "temperature_C",
                 duration = "exposure_h",
                 n_total  = "n",
                 n_surv   = "alive",
                 duration_unit = "hours")

# Continuous proportion (Beta) data
raw_p <- data.frame(
  temperature_C = rep(c(30, 32, 34), each = 4),
  exposure_h    = rep(c(1, 2, 4, 8), times = 3),
  fvfm_ratio    = c(0.95, 0.9, 0.7, 0.2, 0.92, 0.6, 0.3, 0, 0.8, 0.4, 0.1, 0)
)
standardize_data(raw_p,
                 temp       = "temperature_C",
                 duration   = "exposure_h",
                 proportion = "fvfm_ratio",
                 duration_unit = "hours")

freqTLS documentation built on Dec. 4, 2026, 5:11 p.m.