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 a single project-standard schema and attaches metadata used by every downstream fitting and prediction helper. This is the single entry point for raw data — everything else in the library assumes the output of this function.

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 = "hours",
  temp_mean = NULL,
  detection_limit = NULL
)

Arguments

data

Raw data frame or tibble.

temp

Column name of the assay temperature (°C).

duration

Column name of the exposure duration. The unit is whatever is in the source data; record it via duration_unit.

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

Half-open clamp applied to proportion so values sit strictly inside ⁠(0, 1)⁠ (the Beta density is undefined at exactly 0 or 1): one number in ⁠(0, 0.5)⁠. Default 0.001. With a detection_limit, the clamp applies only on a side that has no limit; censored readings take their limit, and uncensored readings on a side with a limit are kept as recorded.

random_effects

Optional character vector of grouping variables for random effects, e.g. c("Date", "Tank"). These columns are converted to factors and stored in metadata for the fitter to read.

duration_unit

Label for the unit of duration, stored in metadata. Default "hours".

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.

detection_limit

For a continuous proportion response only: the limit(s) beyond which the measurement cannot resolve values, so that a reading there says only that the true value lies beyond the limit (a censored reading). NULL (default): no censoring, and readings of 0 or 1 are clamped by proportion_eps. Otherwise one of:

  • a number in ⁠(0, 1)⁠: a lower limit. Readings below it (typically recorded as 0) are left-censored: the true value is below the limit.

  • a length-2 numeric c(lower, upper), named or in that order, with NA for no limit on one side (or one named number, c(upper = 0.98)). Readings at or above upper (e.g. values capped at a ceiling) are right-censored: the true value is at or above the limit.

  • the name of a numeric column holding a per-row lower limit, or a length-2 character c(lower = "col_lo", upper = "col_hi") (a list mixes numbers and column names). NA in a limit column means no limit for that row.

Censored rows store their limit in survival, are flagged "left" or "right" in a new censored column ("none" otherwise), and enter the Beta likelihood as the probability of lying beyond the limit (brms cens(), added by fit_4pl() and the other fitters); the reading as recorded is kept in survival_recorded for plots and observed summaries. The fitted curve keeps its meaning (the mean of the uncensored response), so tls() and the prediction helpers are unchanged. There is no default limit: take it from the instrument or the data (e.g. the smallest positive reading). A reading equal to the lower limit counts as observed; if your instrument records below-limit readings AT the limit rather than as 0, recode those to 0 first. Positive readings below a lower limit, and readings strictly between an upper limit and 1, are coarsened to censored with a warning, since they suggest the limit is set too high (or too low).

The pointwise log-likelihood of a censored fit (log_lik(), loo(), waic()) scores each censored reading as a probability, not a density, so compare such fits only with fits to the same data and the same detection_limit, never with a fit whose readings were clamped.

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 (clamped into the open interval ⁠(proportion_eps, 1 - proportion_eps)⁠ so the Beta likelihood is finite, except on a side with a detection_limit); no n_total/n_surv columns are created. response_type is recorded as "proportion". If the instrument cannot resolve values beyond a limit (e.g. it records 0 when function is below what it can read), give that limit as detection_limit so those readings are treated as censored rather than clamped.

If the dataset spans multiple categories (life stages, species, populations, etc.), filter to one category before calling this function and fit a separate model per subset — the fitter does not estimate category-level effects.

Value

A tibble with the standardised columns (plus censored and survival_recorded when detection_limit is given) and a "tdt_meta" attribute storing temp_mean, 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 half-width used for a proportion fit, else NULL), detection_limit (as supplied, else NULL), censored (TRUE when a detection_limit was supplied) and n_censored (the numbers of left- and right-censored rows, else NULL).

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")

# 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")

# The instrument records 0 below 0.05: treat those zeros as censored
cens <- standardize_data(raw_p,
                         temp            = "temperature_C",
                         duration        = "exposure_h",
                         proportion      = "fvfm_ratio",
                         detection_limit = 0.05)
table(cens$censored)

bayesTLS documentation built on Nov. 29, 2026, 1:06 a.m.