
Prepare length composition data for model input
prep_lf_data.RdTransforms wide or long length-frequency observations (one composition per
fishery x timestep and optional source) into model-ready arrays and vectors
that opal_model and get_length_like expect. The function
validates bin alignment against the length structure in data, optionally
filters to a subset of fisheries, and appends every derived object directly to
data.
Usage
prep_lf_data(
data,
lf_wide,
lf_keep_fisheries = NULL,
lf_switch = 1L,
lf_minbin = NULL,
lf_maxbin = NULL,
lf_var_adjust = NULL,
lf_cap = NULL,
lf_addtocomp = 1e-08,
lf_upper_edge = NULL,
lf_tails = c("accumulate", "truncate"),
lf_edge_tol = 1e-08 * data$len_bin_width,
lf_allow_misaligned = FALSE
)Arguments
- data
a
listcontaining at minimum the length structure scalarslen_bin_start,len_bin_width,n_len, andn_fishery.- lf_wide
A wide data frame, list of wide data frames, or long data frame. Wide frames contain
fishery,year,month,ts, and numeric count columns named by bin lower edges. Long input contains the same metadata plusbin_lower,bin_upper, andcount. Long observations are grouped byfishery,ts, and optionallysource; calendar metadata is taken from the first row.- lf_keep_fisheries
integer vector of fishery indices to retain. Pass
NULL(the default) to keep all fisheries present inlf_wide.- lf_switch
integer likelihood type selector passed straight through to
data$lf_switch(1 = multinomial, 2 = Dirichlet, 3 = Dirichlet-multinomial). Default1L.- lf_minbin
integer vector length
n_fisherygiving the minimum bin index (1-based) used for each fishery. Defaults torep(1L, n_fishery).- lf_maxbin
integer vector length
n_fisherygiving the maximum bin index (1-based) used for each fishery. Defaults torep(n_len, n_fishery).- lf_var_adjust
numeric vector of length
n_fisheryused to scale the effective sample size for each fishery. For observation row \(i\) belonging to fishery \(f\), the sample size is adjusted as \(n_i / \text{lf\_var\_adjust}_f\). Values greater than 1 downweight (reduce effective \(N\)) and values less than 1 upweight the fishery. Defaults torep(1, n_fishery)(no adjustment).- lf_cap
positive integer (or
NULL). When supplied, each effective sample size is capped: \(n_i = \min(n_i, \text{lf\_cap})\). Applied after proportions are computed so that observed compositions are unaffected. DefaultNULL(no cap). This is a Multifan-CL legacy feature.- lf_addtocomp
small non-negative numeric constant added to observed composition proportions after tail compression and before renormalisation. This robustifies zero bins. Default
1e-08.- lf_upper_edge
Final upper edge, or one per element of a list of wide data frames. Defaults to the model length-grid upper edge,
data$len_bin_start + data$n_len * data$len_bin_width. Ignored for long input, which supplies both boundaries explicitly.- lf_tails
Prediction tail handling:
"accumulate"(default) extends the outer matrix edges, or"truncate"excludes outside mass.- lf_edge_tol
Absolute edge tolerance; defaults to
1e-8 * data$len_bin_width. Nearby model edges snap silently.- lf_allow_misaligned
Allow observed edges to split model bins, with a diagnostic warning. The default rejects all edges not matching a model boundary, including edges beyond the model range.
Value
The input data list with the following elements appended or
updated:
len_lower,len_upper,len_midLength bin boundary and midpoint vectors derived from the scalar inputs.
lf_switchPassed through from the argument.
lf_obs_inObserved proportions in retained input order: a matrix with one row per observation when bin counts agree, otherwise a list of row vectors. Native input has
n_lencolumns.lf_nNumeric vector of sample sizes per observation row.
lf_n_intInteger Dirichlet-multinomial sample sizes obtained by half-up rounding of
lf_n.lf_fisheryInteger vector of fishery index per observation row.
lf_fishery_fInteger vector of unique fishery indices with LF data.
lf_n_fInteger vector of observation counts per fishery.
lf_yearInteger vector of model timestep index (1-based) per observation row.
lf_year_fi,lf_n_fi,lf_n_int_fi,lf_row_fiLists split by fishery containing model timesteps, effective sample sizes, and row indices. Precomputed so
opal_model()does not rebuild them on every objective evaluation.lf_seasonInteger vector of season index (all 1).
lf_minbin,lf_maxbinPassed through from arguments.
lf_var_adjustPassed through from argument; numeric vector
[n_fishery]of variance-adjustment divisors applied tolf_n.removal_switch_fInteger vector
[n_fishery]of removal flags (all 0).n_lfTotal number of observation rows.
lf_obs_dataNamed list
list(obs = lf_obs_list)for plain-R access inget_length_like.lf_obs_flatFlattened numeric vector of unrounded counts (for multinomial,
lf_switch = 1).lf_obs_intsFlattened integer counts that sum to
lf_n_intper observation (for Dirichlet-multinomial,lf_switch = 3).lf_obs_propFlattened numeric vector of normalised proportions (for Dirichlet,
lf_switch = 2).lf_addtocompStored value of the add-to-composition constant used to robustify observed composition bins.
lf_nbinsLegacy scalar bin count for the first observation in fishery-grouped flat order, or zero for empty input. Use
lf_nbins_ifor heterogeneous observations.
Details
Inputs may also be lists of wide data frames or long data frames.
Observed counts remain on their original grid. lf_scheme_edges is
a deduplicated list of observed edges, including native min/max pooling.
lf_rebin_s contains prediction matrices (observed bins by model
length bins). lf_scheme and lf_nbins_i follow fishery-grouped
flat observation order, while lf_row_fi indexes retained input order.
The logical lf_native_i, in flat observation order, identifies
observations originally on the full native grid before min/max pooling.
Heterogeneous observations use lists instead of rectangular matrices.
Splitting a model length bin at an observed boundary assumes uniform
prediction mass within that model bin. This approximation is not needed
when observed boundaries match model boundaries, so nonmatching boundaries
are rejected by default. Setting lf_allow_misaligned = TRUE opts into
the approximation with a diagnostic warning. Check bin labeling and units
before changing the grid. Diagnostics suggest a finer width dividing the
observed offsets; edges beyond the model range also require extending that
range. Changing the grid changes the mean and SD of len_mid, which
scale length-based selectivity parameters: existing par_sel values
are not directly transferable.
Observations with a total sample size of zero are silently removed after validating counts and edges. Bin alignment against the model's length structure is checked with the edge tolerance. Dirichlet-multinomial counts use half-up rounding of effective sample sizes and largest-remainder allocation. Observations that round to zero are skipped by the Dirichlet-multinomial likelihood.