sim_data()
View source ↗On this page
Simulate the population, the fishery and the survey with the full operating model, write each replicate to a file and return one replicate with its true values.
Usage
sim_data(
bio_vars,
params,
sim_year = NULL,
output_dir = ".",
iter_range = 4:100,
return_iter = NULL
)
Arguments
| Argument | Default | Description |
|---|---|---|
bio_vars | required | A list from |
params | required | The parameter list from |
sim_year | NULL | Total simulation years; NULL uses params$nyear. |
output_dir | "." | Directory of the replicate files, created when it does not exist. The default "." is the working directory, where the default |
iter_range | 4:100 | Distinct nonnegative integer replicate IDs (0 is allowed), also used as random seeds. Pass the same IDs to |
return_iter | NULL | Replicate ID to return in memory; NULL selects the largest ID in iter_range. All replicates are still written to output_dir. |
Value
The replicate selected by return_iter; NULL selects the largest ID in iter_range. The result includes survey numbers SN_at_len, population numbers at length N_at_len and at age N_at_age, SSB, Rec, fishery catch, and time, age and length metadata. All replicates are also written to output_dir as sim_rep<id> files containing sim.data. Files are written only after every replicate is valid, and file names use the integer form of each ID (sim_rep100000 for the ID 10⁵). Each replicate records its seed, random-number generator settings and package version in provenance, and provenance$random_numbers records whether the random numbers were drawn from one stream ("sequential") or by process ("by_process"; see initialize_params()). Each replicate sets its own seed and generator, so a replicate ID gives the same data under any session generator, and the caller's random-number stream is unchanged after the call. params$structure takes the same list as the structure argument of run_acl() and run_alscl(); sim_cal() reads it too.
Details
sm$nyear counts the retained time steps, first.year is the year of the first retained observation, and sm$years holds the years of all retained time steps. simulation_to_tables() and sim_acl() use these years; for a file without years, the years count from its first.year, or from 2000 when that is missing too. A single retained time step stays a matrix, although a fit needs at least two time steps.
Recruitment in the operating model follows a Beverton-Holt stock-recruitment relationship with AR(1) log deviations, which differs from the recruitment models of ACL and ALSCL.
sim_data() checks biology_inputs and the arrays against sim_cal(params) before drawing random numbers or writing files. Run sim_cal(params) again after changing growth, ages, the time step, the length bins, weight, maturity, catchability or fishery selectivity; change the parameters, not the arrays. Changing only mortality, recruitment, errors, the simulation duration or the calendar needs no new sim_cal().
Additional details from R help
The age-based operating model projects the numbers at age forward and
converts them to numbers at length with the probability of length given
age (pla); F depends on age. The length-based operating model projects
the numbers by length and age, nla[length, age], with the growth
transition matrix Gij; F depends on length, and the time steps can be
quarters.
Natural mortality and F_mean, the median of the F process, must
be finite and nonnegative. F_mean scales exponentiated AR(1) log
deviations before fishing selectivity is applied; it is not the
arithmetic mean of F.
Zero total mortality produces zero catch through the continuous Baranov
limit. Invalid biological arrays are rejected before simulation; a replicate
with nonfinite output is rejected before its file is written. Age-length
probabilities must sum to one for each age; each growth-transition column
must also sum to one, so probability arrays cannot lose population mass.
Initial total mortality and all process/observation standard deviations
must be finite and nonnegative. AR(1) coefficients may be negative but must
lie strictly between -1 and 1. Beverton-Holt alpha and initial recruitment
may be zero; beta must be positive. These process inputs are checked before
creating the output directory or drawing random numbers.
The length bins are checked against the convention of the fitted
models, and the coordinates of the biological arrays must match the
parameter list. The biological inputs and arrays must agree with params,
so rerun sim_cal(params) after changing growth, ages, time step, length
bins, catchability, weight, maturity or selectivity. A biological list
without a record of its inputs is accepted when its arrays match the
current parameters.
Process settings (including mortality, recruitment and observation error),
simulation duration and output controls can change without recalculating
biology. These checks run before creating files or drawing random numbers.
The initial numbers at age, in the first simulated year (before the
burn-in), are recruitment times exp(-init_Z (a - 1)) times a lognormal
deviation. With the default form the deviations accumulate over ages:
log N(a) = log N(a - 1) - init_Z + d(a - 1), with nage - 1 independent
normal increments d of SD std_logN0. With
params$structure = list(initial_deviations = "independent") every age,
the first included, has one independent deviation of SD std_logN0, as in
Equation 11 of Zhang and Cadigan (2022). The deviations of ages 2 to nage
are then the draws that the default form uses for its increments, so the
two forms share their random numbers; with random_numbers = "sequential" the extra draw of the first age changes the draws that
follow (survey errors), whereas with "by_process" it does not. Other
settings of params$structure (initial_plus_group, initial_length)
combine with either form as in run_alscl(). After a long burn-in, such
as the 80 years of the "flatfish" preset, the initial numbers have
little effect on the returned years; use burn_in = 0 in
initialize_params() to return the first simulated year.
Survey observations keep true zeros and small positive values; no
positive floor is applied. Survey biomass equals survey
abundance times weight at length. Fitting still treats zeros as missing
by default (or rejects them with zero_action = "error"); an all-zero survey
cannot be fitted by the lognormal observation model.
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)
# Use objects defined above
sm <- sim_data(bio, pa, iter_range = 4:5, return_iter = 4,
output_dir = "simulation_examples/flatfish")

sim_data(sim_cal(pa), pa, iter_range = 4, return_iter = 4)