Build the flat observation vector and metadata for OSA residuals
Source:R/0-osa_data.R
build_osa_data.RdInternal helper used by rearrange_data() to assemble the inputs the TMB
template needs to compute one-step-ahead (OSA) residuals via
TMB::oneStepPredict().
OSA residuals require every observation that enters the likelihood to live in
a single flat vector (obsvec) with a companion keep indicator. This
function walks the already-built control/observation matrices and produces:
obsvec– the flat vector of observations. Aggregate (catch and index) observations are stored as log(observation) because their likelihood is lognormal; composition (comp) and conditional-age-at-length (caal) observations are stored as bin counts,(proportion + 1e-5) * N, matching exactly what the TMB likelihood forms during fitting.obs_ctl– a data frame with one row per element ofobsvec, mapping each position back to its data type, fleet, species, year, age/length bin, etc., so residuals stay interpretable. R-side metadata only; removed before the data list is passed to TMB.*_obsvec_idx– per observation-row index vectors. For aggregate series this is the 0-basedobsvecposition of each row; for composition and caal it is the 0-basedobsvecposition of the row's FIRST bin (the template reads the row's bins asobsvec.segment(start, n_bins)).-1marks rows excluded from the likelihood.
The inclusion rules and per-row bin counts below must match the guards in the
TMB template exactly, so that every observation the template evaluates has a
valid obsvec position. All fitted observation types are handled: aggregate
catch and index, comp and caal composition, and predator diet composition.
Arguments
- data_list
A
data_listwhose*_ctl/*_obs/*_nmatrices have already been built (and coerced to matrices) byrearrange_data().- build_osa
Logical. When
TRUE, build the full OSA observation data for every type (aggregate, composition, caal, and diet) soosa_residuals()can be computed. WhenFALSE(the default), only the aggregate index/catch entries the TMB template always reads are built and the (much larger) composition/caal/diet metadata is skipped – this is the fast path for simulation testing (e.g.run_mse()), where the fitted objective is identical but OSA composition residuals are not produced.