Skip to contents

Return keyed catch and survey rows for plotting or further checks. This is a convenience extractor. Standardized residuals use fitted states and marginal observation SDs. Shrinkage affects these residuals, and standardization leaves any across-age correlation in place.

Usage

# S3 method for class 'tam_fit'
residuals(
  object,
  type = c("standardized", "log", "response", "osa", "quantile"),
  method = NULL,
  ...,
  residual_seed = NULL
)

Arguments

object

A fitted tam_fit.

type

"standardized" (default), "log" (log observation minus log median), "response" (observation minus prediction), "quantile", or "osa".

method

For type = "quantile", use "mle-mvn" for a Gaussian draw of the random effects, "mle-eb" for the fitted states, or "auto" to choose by model. With NULL, use the retained method or the automatic choice. For type = "osa", select an algorithm from add_osa_residuals(). Supplying a method explicitly requests a fresh calculation.

...

Computation controls passed to RTMB::oneStepPredict(), such as trace = FALSE or parallel = FALSE. subset and conditional index included observations in likelihood order; unrequested rows retain missing residuals. Explicit controls request a fresh calculation; other residual types reject them.

residual_seed

Optional nonnegative integer for type = "quantile". Controls the Gaussian state draw and randomization of recorded zeros. Defaults to the stored seed, or 123456. Supplying a seed requests a fresh calculation.

Value

A data frame with year, age, type (catch/index), survey, observations, predictions, residuals, and available unit/fleet columns. OSA output also identifies osa_method. The residual_type attribute names the requested scale. Quantile output identifies the method and any fallback in the table. The quantile attribute records the requested and used methods, status and seed. The osa attribute retains available method, settings and coverage.

Details

type = "osa" reuses cached OSA columns when no computation controls or method are supplied. Otherwise both catch and index residuals are calculated together, using one algorithm at fitted parameter values. It can be expensive, but does not rerun fit_tam() or re-estimate fixed parameters. Temporary objectives protect the fitted object's state. For Gamma and generalized gamma, standardized residuals use the mean and SD of the log of positive observations. Log residuals use the positive median. Delta lognormal standardized residuals also use the positive component. Tweedie standardized residuals are response-scale Pearson residuals. Delta and Tweedie response residuals use the mean including zeros.

For "mle-mvn", fixed effects stay at their estimates and all fitted random effects are sampled together. The Gaussian approximation uses their conditional mode and precision. Catch and survey distributions are evaluated at this draw, using the approach described in the sdmTMB residual documentation. The fitted-state method "mle-eb" evaluates the same distributions at the mode. For lognormal observations at the mode, quantile and standardized residuals agree. Both quantile methods randomize uniformly across the cumulative probability step at each sampled zero. Missing observations, projections and structural zeros have missing quantile residuals. Marginal quantiles retain any across-age observation correlation.

The automatic choice uses "mle-mvn" when there are fitted random effects and the conditional draw is usable. Otherwise it uses "mle-eb". If a draw fails, the returned metadata records the reason. An explicit request for "mle-mvn" in that case gives an error. OSA is available for all-lognormal fits. The supplied fit is not modified. To retain results for later extraction and dashboard use, assign fit_osa <- add_osa_residuals(fit, method = "oneStepGeneric", trace = FALSE).

Examples

if (FALSE) { # \dontrun{
residuals(fit)
residuals(fit, type = "log")
residuals(fit, type = "quantile")
residuals(fit, type = "quantile", method = "mle-eb")
residuals(fit, type = "osa", method = "oneStepGeneric", trace = FALSE)
} # }