retro_model()
View source ↗On this page
Refit after removing the last time steps one peel at a time, compare each peel with the full fit and report Mohn's rho for fits that pass the numerical checks.
Usage
retro_model(
nyear = 5,
data.CatL,
data.wgt,
data.mat,
rec.age,
nage,
M,
sel_L50 = NULL,
sel_L95 = NULL,
model_type = c("acl", "alscl"),
growth_step = NULL,
parameters = NULL,
parameters.L = NULL,
parameters.U = NULL,
map = NULL,
len_mid = NULL,
len_border = NULL,
len_lower = NULL,
len_upper = NULL,
train_times = 1,
ncores = 1,
silent = FALSE,
plot = FALSE,
line_size = NULL,
point_size = NULL,
point_shape = 21,
facet_scales = "free",
facet_ncol = NULL,
facet_nrow = NULL,
title = NULL,
xlab = NULL,
ylab = NULL,
font_family = NULL,
title_size = NULL,
axis_title_size = NULL,
axis_text_size = NULL,
strip_text_size = NULL,
legend_text_size = NULL,
x_breaks = NULL,
base_theme = NULL,
title_hjust = NULL,
rho_digits = 4,
rho_position = "top_right",
rho_size = 3.5,
F_structure = c("group_year", "year"),
polish = TRUE,
zero_action = c("missing", "error"),
control = list(),
nstarts = 1,
model = NULL,
peel_step = 1,
structure = list(),
start_spread = 0.15,
t0_lower = 0,
survey_q = NULL,
survey_q_age = NULL,
bounds = c("default", "wide"),
...
)
Arguments
| Argument | Default | Description |
|---|---|---|
nyear | 5 | Integer. Number of retrospective peels. Default is 5. Each successive peel removes peel_step further model time steps. With the default peel_step = 1, one peel removes one model time step (one quarter for quarterly data). |
data.CatL | required | Survey index table (length labels followed by one column per time step), or the input bundle used by |
data.wgt | required | Weight table, with the same length bins and time steps as the survey index table. |
data.mat | required | Maturity table, with values between 0 and 1 and the same length bins and time steps as the survey index table. |
rec.age | required | Recruitment age in years. |
nage | required | Number of age classes. The last class holds the fish of its starting age and older. |
M | required | Natural mortality rate per model time step, in one of the forms that |
sel_L50 | NULL | Length at 50 percent survey catchability. NULL when survey_q is supplied. |
sel_L95 | NULL | Length at 95 percent survey catchability. NULL when survey_q is supplied. |
model_type | c("acl", "alscl") | Character. Model to fit, "acl" (default) or "alscl". Letter case does not matter. |
growth_step | NULL | Length of a time step in years (0.25 for quarterly data). NULL uses rec.age when it is below 1, otherwise 1. |
parameters | NULL | Named list of starting values on the optimizer scale. |
parameters.L | NULL | Named list of lower bounds on the optimizer scale. |
parameters.U | NULL | Named list of upper bounds on the optimizer scale. |
map | NULL | Named list of TMB factor maps, as in |
len_mid | NULL | Midpoints of the length bins, one per row of the data. |
len_border | NULL | Interior boundaries of the length bins, one fewer than the number of bins. |
len_lower | NULL | Deprecated; it will be removed. Optional lower bin boundaries (ALSCL only). The first entry must be |
len_upper | NULL | Deprecated; it will be removed. Optional upper bin boundaries (ALSCL only). The final entry must be |
train_times | 1 | Positive integer. Number of successive optimization passes from each start; it does not add independent starts. |
ncores | 1 | Integer. Number of socket workers for the full fit and the peels. Default 1. With more than one worker, the full fit and the peels run in one batch, with the same estimates as with one worker. The ncores stored in an input bundle is not used, so give this argument in the call. Each fit runs its nstarts starts one after another. |
silent | FALSE | Logical. TRUE suppresses the progress messages; warnings about the numerical checks still appear. Default FALSE. |
plot | FALSE | Logical. TRUE adds the plot to the result. When no finite estimates are available, plot is NULL and a warning is given; fit_diagnostics and errors remain available. |
line_size | NULL | Numeric or NULL. Line width. NULL inherits the global line_size setting (1 under the default style, 1.5 under "classic"). While that setting keeps the value of the style, facets of more than four panels draw thinner lines. |
point_size | NULL | Numeric or NULL. Size of the points. NULL inherits the global point_size setting (default 2.5). |
point_shape | 21 | Numeric. Shape of the points that mark the last estimate of each peel. Default 21. |
facet_scales | "free" | Character. Axis scales of the panels, "fixed", "free", "free_x" or "free_y". Letter case does not matter. |
facet_ncol | NULL | Integer or NULL. Number of columns of panels. NULL chooses it from the number of panels, for example 5 × 3 for 15 panels and 6 × 4 for 23. |
facet_nrow | NULL | Integer or NULL. Number of rows of panels. |
title | NULL | NULL (default) draws the default title, FALSE, NA or "" draws no title, and a character string draws that string; see |
xlab | NULL | Character or NULL. Label of the x axis. NULL uses the global theme setting. |
ylab | NULL | Character or NULL. Label of the y axis. NULL uses the global theme setting. |
font_family | NULL | Character or NULL. Font family of the text, which must be installed on the computer. NULL uses the global theme setting (default "sans"). |
title_size | NULL | Numeric or NULL. Size of the plot title in pt. NULL uses the global theme setting (default 14). |
axis_title_size | NULL | Numeric or NULL. Size of the axis titles in pt. NULL uses the global theme setting (default 12). |
axis_text_size | NULL | Numeric or NULL. Size of the axis tick labels in pt. NULL uses the global theme setting (default 10). |
strip_text_size | NULL | Numeric or NULL. Size of the panel labels in pt. NULL uses the global theme setting (default 10). |
legend_text_size | NULL | Numeric or NULL. Size of the legend text in pt. NULL uses the global theme setting (default 10). |
x_breaks | NULL | Numeric vector or NULL. Positions of the x-axis breaks. NULL inherits the global x_breaks setting when at least one of its values lies on this axis; otherwise the breaks are chosen automatically. |
base_theme | NULL | Character or NULL. Name of the base ggplot2 theme, such as "bw", "minimal" or "theme_bw". NULL uses the global theme setting. Letter case does not matter. |
title_hjust | NULL | Numeric or NULL. Horizontal position of the title, 0 for left, 0.5 for centered and 1 for right. NULL uses the global theme setting. |
rho_digits | 4 | Integer. Number of decimals of the Mohn's rho label. Default 4. |
rho_position | "top_right" | Character. Position of the Mohn's rho label, "top_right" (default), "top_left", "bottom_right" or "bottom_left". Letter case does not matter. |
rho_size | 3.5 | Numeric. Text size of the Mohn's rho label. Default 3.5. |
F_structure | c("group_year", "year") | "group_year" (default) gives F a two-dimensional first-order autoregressive (AR(1)) field over age (ACL) or length (ALSCL) and time. "year" uses one F per time step with AR(1) correlation over time; the correlation between groups is then fixed and left out of the reports. Letter case does not matter. |
polish | TRUE | Logical. TRUE (default) refines the selected fit with safeguarded Newton steps; FALSE skips this. The raw optimizer codes are kept, and |
zero_action | c("missing", "error") | "missing" (default) treats zero observations as missing, and "error" stops at a zero observation; NA is always missing. Passed to every fit. Letter case does not matter. |
control | list() | Named list of nlminb control settings, passed to every fit; a supplied list replaces the stored list. |
nstarts | 1 | Positive integer. Number of independent starts of each fit, run one after another within the fit. Default 1, or the nstarts of the input bundle. |
model | NULL | Same as model_type, accepted for consistency with the other workflow functions. Supply one of the two. |
peel_step | 1 | Positive integer. Number of model time steps that each successive peel removes. Default 1. For quarterly data, peel_step = 4 gives annual peels, in which peel k ends k years before the last time step. |
structure | list() | Model structures passed to every full and peel fit; see |
start_spread | 0.15 | Spread of the second and later starts, passed to every full and peel fit; see |
t0_lower | 0 | Lower limit of the ALSCL t0, passed to every full and peel fit; see |
survey_q | NULL | Survey catchability at length, passed to every full and peel fit; see |
survey_q_age | NULL | Survey catchability multiplier by age, passed to every full and peel fit; see |
bounds | c("default", "wide") | Passed to every full and peel fit of ACL; see |
... | … | The former argument names facet_col and facet_row, accepted with a warning. |
Value
A list with results (the estimates of the full fit and of each peel, with NumericalOK and RawRho flags), last_points (the last estimate of each peel), rho_by_peel (Rho, RawRho and Valid for each quantity and peel), rho_text (Mohn's rho of each quantity, with the columns Variable, Rho, Year, Valid, ValidPeels and RequestedPeels; Year is the x position of the label in the plot, not a year) and raw_rho_text (the mean of RawRho, for diagnosis only). fit_diagnostics has one row for the full fit (Peel = 0) and one for each peel, with the numerical checks, the optimizer messages and passes, the refinement status and the warnings of each fit; errors lists the fit errors. model_type, workers (the number of socket workers used), nstarts (the starts of each fit) and peel_step (the time steps removed per peel) record the settings. With plot = TRUE, plot holds the plot, or NULL when no finite estimates can be drawn.
Details
retro_acl() and retro_alscl() call retro_model() with the model chosen. The function fits the complete data and the data of each peel. Each successive peel removes peel_step further model time steps (default 1, which is one quarter for quarterly data); for quarterly data, peel_step = 4 gives annual peels. Mohn's rho is the mean over peels of the relative difference between the last estimate of a peel and the estimate of the full fit for the same time step, so positive and negative differences can cancel.
The retrospective functions take the same input bundle as the fitting functions, for example retro_acl(x, nyear = 3), retro_alscl(x, nyear = 3) or retro_model(data.CatL = x, model_type = "alscl", nyear = 3). Each fit uses the shared settings of the bundle, then the settings of the selected model, then the arguments of the call; a map, parameters or control given in the call replaces the whole stored list. zero_action, control, nstarts, F_structure, polish, structure, start_spread, t0_lower, survey_q, survey_q_age and, for ACL, bounds reach every fit. The ncores stored in a bundle is not used; set ncores in the call to run the full fit and the peels in one batch on socket workers, with the same estimates as with one worker. Within each fit the nstarts starts run one after another.
Each peel shortens the time-dependent maps map$dev_log_R and map$dev_log_F to the time steps it keeps. Give these maps for the complete data. The recruitment map has one entry per time step, and the F map follows the column order of the group-by-time matrix (age classes for ACL, length bins for ALSCL, one group with F_structure = "year"). Factor levels and fixed NA entries are kept, and unused levels are dropped. Fixed entries or ties work only when the standard deviation and correlations of that process are fixed too; otherwise the fits fail and the call stops with an error. A map whose length does not match the complete data is an error. The maps of scalar parameters and of dev_log_N0 are the same in every peel.
fit_diagnostics has one row for the full fit (Peel = 0) and one for each requested peel, with the run time, the raw optimizer status, gradient, Hessian, boundaries and errors. rho_by_peel holds Rho, RawRho and Valid for each quantity and peel. rho_text$Rho gives a value for a quantity only when the full fit and every requested peel pass the numerical checks and every terminal comparison is finite with a nonzero reference; otherwise it is NA and a warning is given. Failed peels stay in the record. raw_rho_text is the mean of RawRho, for diagnosis only; it is NA when a terminal comparison is missing. The trajectories that are available remain in results.
When the full fit and every peel stop with an error, the call stops with an error that quotes their messages; when at least one fit returns, the result is returned with the failures recorded in fit_diagnostics and errors. When no finite trajectory can be drawn, plot = TRUE gives a warning and returns plot = NULL, and fit_diagnostics, errors and the missing rho values remain available for correcting the input or the fitting settings.
fit_diagnostics also holds optimizer_message and selected_pass for the selected pass, optimizer_codes and optimizer_messages for all passes, and refinement_status, refinement_steps and pre_refinement_max_gradient for the Newton refinement; max_abs_gradient is the final gradient. Records that a failed fit does not have are NA. A failed restart in the history does not invalidate an earlier selected pass, and a selected nonzero code is never changed to zero.
Warnings raised while fitting, including on socket workers, are recorded in fit_diagnostics$warnings for the fit that raised them, and one summary warning names these fits. A parameter that the map fixes has the same value in the full fit and in every peel. When the map fixes mean_log_R and parameters gives no value for it, the value is derived once from the complete observations (the log of the mean over time steps of the total survey index, minus the log of the first value of survey_q_age when that is supplied) and used in every fit.
M takes the forms that run_acl() or run_alscl() accepts. A matrix by age and time step has one column per time step of the complete data, and each peel uses the columns of the time steps that it keeps; the same holds for survey_q given as a matrix by length and time step. When survey_q_age is supplied, the default start of mean_log_R in every fit is shifted by -log(survey_q_age[1]).
Additional details from R help
Settings of an input bundle are taken in the order of run_acl():
fit_args, the fit_config of the model, then the arguments of the call. A
map, parameters or control given in the call replaces the whole
stored list, also when it is NULL. zero_action, control and nstarts are
passed to every full and peel fit.
Mohn's rho is the mean over peels of the relative difference between the
terminal estimate of a peel and the estimate of the full fit for the same
time step. With peel_step = 1 on quarterly data it is a mean over
quarterly peels; use peel_step = 4 for a rho based on annual peels. A
rho is reported for a quantity only when the full fit and every
requested peel pass the numerical checks and have finite terminal
estimates, with a reference estimate that is not zero. Fits that fail
are kept in the result.
A parameter that the map fixes has the same value in the full fit and in
every peel. When the map fixes mean_log_R and parameters gives no
value for it, the value is derived once from the complete data (the log
of the mean total survey index over time steps, minus the log of the
first value of survey_q_age when that is given) and used in every fit.
Warnings raised while fitting are recorded in fit_diagnostics$warnings
for the fit that raised them, also for fits on socket workers, and one
summary warning names these fits.
Examples
Run the setup before the example. Fitting examples require an R-compatible C++ toolchain; simulation and export examples may write files.
Example setup
library(ALSCL)
library(ggplot2)
library(patchwork)
data("YTF_example")
x <- YTF_example
inputs <- x[c("data.CatL", "data.wgt", "data.mat")]
dat <- x$data.CatL
a <- run_acl(x, silent = TRUE)
b <- run_alscl(x, silent = TRUE)
# Use objects defined above
rb <- retro_model(data.CatL = x, model_type = "alscl", nyear = 3)
rb$fit_diagnostics
rb$rho_by_peel
rb$rho_text

plot_retro(retro_model(x, nyear = 3, model = "alscl"))