On this page

Fit ACL or ALSCL to the replicates that sim_data() saved, one replicate per worker, and save each fit or failure to a file.

Usage

sim_model(
  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,
  model = c("acl", "alscl"),
  nstarts = 1,
  allow_M_mismatch = FALSE,
  structure = list(),
  t0_lower = NULL
)

Arguments

ArgumentDefaultDescription
iter_range4:100

Distinct nonnegative integer IDs (0 is allowed) of the sim_rep<id> files to fit.

sim_data_path"."

Folder containing the replicate files written by sim_data(); the default "." is the working directory.

output_dir"."

Directory of the result_rep_<id> files; use different directories for ACL and ALSCL. The default "." is the working directory, where the default iter_range = 4:100 writes 97 result files.

parametersNULL

Optional named list of starting values of the scalar parameters of the selected model.

parameters.LNULL

Optional named lower bounds, matching the selected model.

parameters.UNULL

Optional named upper bounds, matching the selected model.

mapNULL

Optional map for the selected model; a named NULL entry releases a parameter that the default map fixes.

MNULL

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.

ncores1

Maximum number of socket workers that fit replicates in parallel.

train_times1

Number of successive optimization passes from each start.

controllist()

Named nlminb optimizer controls.

F_structurec("group_year", "year")

"group_year" (default) or "year", which uses one F for all groups in each model time step. Letter case does not matter.

polishTRUE

Logical. TRUE (default) refines the selected fit with safeguarded Newton steps; FALSE skips this.

modelc("acl", "alscl")

"acl" (default) or "alscl". Letter case does not matter.

nstarts1

Independent starts per replicate, run one after another within the replicate; ncores sets how many replicates are fitted in parallel.

allow_M_mismatchFALSE

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.

structurelist()

Optional model structures of the fitted model, as in run_acl() and run_alscl(). The default, an empty list, is the original structure. The value need not equal the structure element of the parameter list that generated the replicates. A different value fits a model with another structure, for example list(initial_deviations = "independent") for data simulated with cumulative initial deviations.

t0_lowerNULL

ALSCL only. Lower limit of t0, as in run_alscl(); NULL (default) uses 0. Set a negative value, for example -1, to fit replicates generated with a negative t0.

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

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().

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.

Before fitting, the saved q_surv vector is checked against its logistic catchability metadata. When the metadata are absent, the logistic curve is recovered from the interior probabilities and checked against the full vector. An inconsistent file gives an error record; regenerate the simulation from matching parameters and biology.

Additional details from R help

M and F are rates per time step, also in quarterly simulations. Each replicate is fitted by one worker, also when nstarts is greater than 1. When replicates run on parallel workers (ncores greater than 1), each worker computes the standard errors on one core, so that workers do not start workers. With ncores = 1 the standard errors use the session setting of alscl_speed(); the results are identical. Each successful result keeps nstarts, workers and start_diagnostics of its fit. train_times sets the successive optimizer passes from each start, and nstarts the number of independent starts. The parameters and maps must be those of the selected model. sim_acl() and sim_alscl() are shorter calls for one model. The survey catchability recorded in a replicate must agree with its logistic parameters before fitting. A file that records only valid logistic parameters is accepted; when these are absent, a logistic curve is recovered from q_surv and checked against the complete recorded vector. Standard errors are computed for every replicate because summarize_simulations() uses them for interval coverage.

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
x <- YTF_example
pa <- x$parameters
bio <- sim_cal(pa)
folder <- tempfile("alscl-sim-")
sm <- sim_data(bio, pa, iter_range = 1:2, return_iter = 1, output_dir = folder)
cfg <- x$fit_config$alscl
fits <- sim_model(1:2, folder, file.path(folder, "fits_alscl"),
  parameters = cfg$parameters, map = cfg$map, M = pa$M, model = "alscl", ncores = 2, nstarts = 3)
lapply(fits, function(fit) fit$diagnostics)

See also

Search documentation