Skip to contents

Extracts observations, predictions, and observation SDs for catch and index from a fitted TAM object, and adds standardized residuals on the log scale.

Usage

tidy_obs_pred(
  fit,
  add_osa_res = FALSE,
  interval = 0.95,
  method = "oneStepGaussianOffMode",
  ...,
  residual_seed = NULL,
  quantile_method = NULL
)

Arguments

fit

A fitted TAM object as returned by fit_tam().

add_osa_res

Logical; calculate one-step-ahead residuals?

interval

Confidence level for combined catchability intervals.

method

OSA algorithm; defaults to "oneStepGaussianOffMode". See add_osa_residuals() for supported choices and computation details.

...

Arguments to pass to RTMB::oneStepPredict().

residual_seed

Optional nonnegative integer seed for the Gaussian draw and randomization of observed zeros. Defaults to the stored seed or 123456.

quantile_method

"auto", "mle-mvn" or "mle-eb", as in residuals.tam_fit(). With NULL, retain stored residuals when available and use the automatic choice otherwise.

Value

A named list with two data frames:

  • catch: original columns plus pred, sd, and std_res.

  • index: original columns plus pred, sd, q, and std_res. Combined catchability intervals are q_lwr/q_upr; q_se is on the selected link scale named in q_se_scale. They include uncertainty across the full formula, rather than combining separate coefficient SEs. With q_power(), q remains effective catchability. Additional q_base, q_power, and q_base_lwr/q_base_upr/q_base_se columns separate the baseline and exponent. Baseline-q SEs are on the log scale. Its unit is unspecified for active power groups because it depends on p and abundance units.

Details

For lognormal observations, pred is the median and sd is the marginal SD of log observations. For Gamma and generalized gamma, pred is the arithmetic mean. For delta and Tweedie families, pred is the arithmetic mean including zeros. For delta families sd describes the positive component. The sd_scale column identifies a log SD, CV, Prentice scale or square-root Tweedie dispersion. Fits using these families also report the family, the scale of pred in pred_scale, the arithmetic mean in pred_mean, the positive-component mean and median. Delta and Tweedie rows include zero and positive probabilities (p_zero, p_positive and their logs). Delta rows identify delta_type. Generalized gamma rows include the signed gengamma_Q. Tweedie rows report tweedie_power and tweedie_phi and use response-scale Pearson residuals. Other standardized residuals use the positive component's log mean and log SD. Standardized residuals (std_res) use fitted states. Quantile residuals (quantile_res) use one conditional Gaussian draw when fitted random effects and usable precision are available. All-lognormal fits without random effects use standardized residuals in the default plots. The quantile_method and quantile_status columns identify the calculation and any fallback. Both residual types use the marginal distribution of correlated observations. One-step-ahead residuals use a different predictive calculation and are available for all-lognormal fits. See tinyAM-model for the equations. OSA calculation rebuilds a temporary objective at stored fitted parameters; it does not refit fixed parameters or modify the fitted objective. With add_osa_res = TRUE, it computes the requested method afresh and records its settings and observation coverage. osa_method identifies the algorithm beside osa_res. To retain diagnostics while preserving existing observation tables and confidence intervals, use add_osa_residuals(). When stockassessment is loaded, one-step residuals run sequentially: the upstream TMB parallel helper cannot select between SAM and RTMB libraries.

Examples

fit <- fit_tam(
  cod_obs,
  years = 1983:2024,
  ages = 2:14,
  N_settings = list(process = "iid", init = "exp"),
  F_settings = list(process = "rw", mu_form = NULL),
  M_settings = list(process = "off", mu_supplied = ~ I(0.3)),
  catch_settings = list(sd_form = ~ 1),
  index_settings = list(sd_form = ~ 1, q_form = ~ q_block),
  silent = TRUE
)
obs_pred <- tidy_obs_pred(fit)
head(obs_pred$catch)
#>     year age obs          unit F_y_block F_a_block is_proj      pred        sd
#> 174 1983   2  18 thousand fish 1954-1991       2-3   FALSE 13.281992 0.3814101
#> 175 1984   2   3 thousand fish 1954-1991       2-3   FALSE  4.438363 0.3814101
#> 176 1985   2   0 thousand fish 1954-1991       2-3   FALSE  2.539587 0.3814101
#> 177 1986   2   1 thousand fish 1954-1991       2-3   FALSE  2.211582 0.3814101
#> 178 1987   2  42 thousand fish 1954-1991       2-3   FALSE 18.146481 0.3814101
#> 179 1988   2  25 thousand fish 1954-1991       2-3   FALSE 21.066685 0.3814101
#>        std_res quantile_res quantile_method quantile_status quantile_detail
#> 174  0.7969445    0.7719163         mle-mvn              ok            <NA>
#> 175 -1.0269088   -0.9285626         mle-mvn              ok            <NA>
#> 176         NA           NA         mle-mvn              ok            <NA>
#> 177 -2.0809837   -2.1905319         mle-mvn              ok            <NA>
#> 178  2.2002381    1.6692431         mle-mvn              ok            <NA>
#> 179  0.4488161   -0.9227721         mle-mvn              ok            <NA>
head(obs_pred$index)
#>     year age      obs     survey samp_time q_block          unit is_proj
#> 612 1983   2 698822.0 Fall 2J3KL       0.8       2 thousand fish   FALSE
#> 633 1984   2 685850.3 Fall 2J3KL       0.8       2 thousand fish   FALSE
#> 654 1985   2 128102.7 Fall 2J3KL       0.8       2 thousand fish   FALSE
#> 675 1986   2 178331.8 Fall 2J3KL       0.8       2 thousand fish   FALSE
#> 696 1987   2 144096.3 Fall 2J3KL       0.8       2 thousand fish   FALSE
#> 717 1988   2 227219.7 Fall 2J3KL       0.8       2 thousand fish   FALSE
#>         pred        sd        q   q_unit      q_se    q_upr   q_lwr q_se_scale
#> 612 669326.5 0.4282125 1.751148 unitless 0.1739374 2.462513 1.24528        log
#> 633 371895.6 0.4282125 1.751148 unitless 0.1739374 2.462513 1.24528        log
#> 654 183755.3 0.4282125 1.751148 unitless 0.1739374 2.462513 1.24528        log
#> 675 138185.2 0.4282125 1.751148 unitless 0.1739374 2.462513 1.24528        log
#> 696 259546.9 0.4282125 1.751148 unitless 0.1739374 2.462513 1.24528        log
#> 717 280768.8 0.4282125 1.751148 unitless 0.1739374 2.462513 1.24528        log
#>        std_res quantile_res quantile_method quantile_status quantile_detail
#> 612  0.1007073    0.1007079         mle-mvn              ok            <NA>
#> 633  1.4293045    1.7235346         mle-mvn              ok            <NA>
#> 654 -0.8425091   -0.9133271         mle-mvn              ok            <NA>
#> 675  0.5956191    0.4637165         mle-mvn              ok            <NA>
#> 696 -1.3742146   -1.1437069         mle-mvn              ok            <NA>
#> 717 -0.4941802   -0.8731195         mle-mvn              ok            <NA>