sim_acl()
View source ↗On this page
Fit ACL to the replicates that sim_data() saved, one replicate per worker, and save each fit or failure to a file.
Usage
sim_acl(
iter_range = 4:100,
sim_data_path = ".",
output_dir = ".",
parameters = NULL,
parameters.L = NULL,
parameters.U = NULL,
map = NULL,
M = NULL,
ncores = 1,
train_times = 1,
control = list(),
F_structure = c("group_year", "year"),
polish = TRUE,
nstarts = 1,
allow_M_mismatch = FALSE,
structure = list()
)
Arguments
| Argument | Default | Description |
|---|---|---|
iter_range | 4:100 | Distinct nonnegative integer IDs (0 is allowed) of the |
sim_data_path | "." | Folder containing the replicate files written by |
output_dir | "." | Directory of the |
parameters | NULL | Optional named list of starting values of the ACL scalar parameters, on the optimizer scale. |
parameters.L | NULL | Optional named list of lower bounds, on the optimizer scale. |
parameters.U | NULL | Optional named list of upper bounds, on the optimizer scale. |
map | NULL | TMB map. factor(NA) fixes a parameter and a named NULL entry releases it; map = NULL uses the default ACL map. |
M | NULL | Natural mortality rate per model time step used in every fit. The default, NULL, uses the natural mortality rate recorded in each replicate file by sim_data(). A supplied value must equal the recorded rate unless allow_M_mismatch is TRUE. Files without a recorded rate require M. |
ncores | 1 | Maximum number of socket workers that fit replicates in parallel; 1 fits them one after another. Each fit uses one worker. |
train_times | 1 | Positive integer. Number of successive optimization passes from each start; it does not add independent starts. |
control | list() | Named nlminb control settings, such as iter.max and eval.max. |
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 |
nstarts | 1 | Independent starts per replicate, run one after another within the replicate; ncores sets how many replicates are fitted in parallel. |
allow_M_mismatch | FALSE | Logical. FALSE (default) stops when a supplied M differs from the natural mortality rate recorded in a replicate file. TRUE fits the supplied M, for a study of misspecified natural mortality. |
structure | list() | Optional model structures of the fitted model, as in |
Value
A named list of replicate fits or error records, including iter and elapsed. Every result, failures included, also replaces its result_rep_<id> file. Failed fits have class alscl_fit_error and contain converge = "FAILED", error and iter; for a missing replicate file the error reads, for example, Simulation file sim_rep2 not found in <folder>. A successful result keeps nstarts, workers and start_diagnostics (the result, process and timing of every start) from the core fit. simulation_M records the natural mortality rate used (used), the rate recorded in the replicate file (recorded, NA when absent) and where the rate used came from (source).
Details
sim_acl() saves the result of every replicate, failures included, to result_rep_<id>. In a reused output directory, a failed fit replaces the file of an earlier successful fit of the same replicate, so check converge and error before summarizing. A file that cannot be written stops the batch with an error. Replicates are converted as in simulation_to_tables(), which uses the length coordinates recorded in the replicate and rejects parameter coordinates that conflict with them.
The fits use the time and length coordinates recorded in the replicate files. Each replicate uses one fitting worker, including when nstarts is greater than 1. When replicates run on parallel workers (ncores greater than 1), each worker computes standard errors on one core to avoid nested parallelism. With ncores = 1 the standard errors use the session setting of alscl_speed(); the results are identical. Results do not contain the TMB object obj, in memory or in the saved files; use refit_model() on a result when a TMB object is needed. M and F are rates per model time step. The function fits data generated by the operating model; for data simulated from a fit, see simulate_fit() and self_test().
sim_acl() evaluates the path and parameter arguments before it sends tasks to the socket workers, so variables defined in the console or in a script work with ncores greater than 1. ncores sets how many replicates are fitted in parallel and nstarts the starts within each replicate. Before calculating performance statistics, check the failed replicates and the numerical checks of each fit, and keep the failures in the summary.
M = NULL (default) uses the natural mortality rate recorded in each replicate file; a supplied M that differs from the recorded rate is an error unless allow_M_mismatch = TRUE. train_times sets the successive optimization passes from each start, and nstarts the number of independent starts.
Replicate IDs must be distinct nonnegative integers, 0 included, in sim_data(), sim_model(), sim_acl() and sim_alscl(). Simulation uses each ID as its seed, and fitting reads the matching sim_rep<id> file; a file named in the default number format of R, such as sim_rep1e+05 for the ID 10⁵, is read as well.
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
pa <- initialize_params(species = "flatfish")
bio <- sim_cal(pa)
sm <- sim_data(bio, pa, iter_range = 4:5, return_iter = 4,
output_dir = "simulation_examples/flatfish")
# Use objects defined above
do.call(sim_acl, c(list(iter_range = 4:5,
sim_data_path = "simulation_examples/flatfish",
output_dir = "simulation_examples/fit", M = pa$M), x$fit_config$acl))