Compute OSA residuals for composition data
get_osa.RdFormats observed and expected composition data and calculates one-step-ahead (OSA) residuals using multinomial, Dirichlet-multinomial, or logistic-normal likelihoods. This function is the main interface for residual diagnostics, internally calling [run_external_comp_osa()] to perform the residual calculations.
Usage
get_osa(
obs_mat = NULL,
exp_mat = NULL,
N = NULL,
DM_theta = NULL,
LN_Sigma = NULL,
years = NULL,
seas = NULL,
fleet = NULL,
bins = NULL,
comp_type = NULL,
bin_label = NULL,
comp_like = 0,
addtocomp = 0,
model = NULL,
data = NULL,
comp_source = NULL,
index_source = NULL,
family = "discrete",
pop = FALSE,
discard = FALSE,
tag = FALSE,
osa_method = NULL,
parallel = FALSE
)Arguments
- obs_mat
Array of observed compositions, dimensioned by
[region, year, bin, sex, fleet]. May containNAs, which are removed when filtering byyears.- exp_mat
Array of expected compositions, dimensioned the same as
obs_mat. May containNAs, which are removed when filtering byyears.- N
Input (or effective if Multinomial) sample size, at the model's full year dimension in every case:
years(oryears_by_region) selects the years actually used, the same way it selects them fromobs_mat/exp_mat, soNis never pre-filtered by the caller. Dimensions depend oncomp_type:comp_type = 0(aggregated): vector of lengthn_years(the model's, not the length ofyears).comp_type = 1(split by region and sex): array[n_regions, n_years, n_sexes].comp_type = 2(split by region, joint by sex): matrix[n_regions, n_years].
For years without data, users can simply input an NA or any abritary number (it gets filtered out within the function). This is the same array
ISS_*Compsalready is in the model's data list, so it can usually be passed straight through.- DM_theta
Dirichlet-multinomial overdispersion parameter(s). Dimensions must match
N:aggregated: scalar
split by sex: matrix
[n_regions, n_sexes]joint by sex: vector of length
n_regions
- LN_Sigma
Logistic-normal covariance matrix. Dimensions depend on
comp_type:aggregated: matrix
[n_bins, n_bins]split by region and sex: array
[n_regions, n_bins, n_bins, n_sexes]joint by sex: array
[n_regions, n_bins, n_bins]
Use [get_logistN_Sigma()] to help construct this input.
- years
Years with composition data. Either a plain vector, used for every region, or a list with one vector of years per region when the regions have different years. Both forms work for every composition type. A region with no years is skipped.
- seas
Season index
- fleet
Fleet identifier (character or numeric) to filter to.
- bins
Vector of age or length bin labels corresponding to the composition categories.
- comp_type
Integer specifying how compositions are structured:
0 = aggregated across regions and sexes
1 = split by region and sex
2 = split by region, joint by sex
- bin_label
Character label describing whether bins represent ages or lengths.
- comp_like
Integer specifying the likelihood type (defaults to 0):
0 = multinomial
1 = Dirichlet-multinomial
2-4 = logistic-normal variants
- addtocomp
Constant that is added to compositions
- model
A fitted RTMB model object from
fit_model(built withdo_internal_comp_osa = TRUEordo_internal_conv_tag_osa = TRUE). Supplyingmodelswitchesget_osa()from the default external (post-hoc, compResidual-based) path to the internal path, which callsRTMB::oneStepPredict()directly on the model's internally tracked OSA vector. Allobs_mat/exp_mat/N/DM_theta/LN_Sigma/years/comp_type/comp_likearguments above are ignored in this mode.- data
The model
datalist (e.g.input_list$data) used to buildmodel. Required whenmodelis supplied.- comp_source
One of
"FishAge","FishLen","SrvAge","SrvLen", identifying which composition data source to pull internal OSA residuals for. Conditional age-at-length uses"Fish_caal"or"Srv_caal", which return an extralencolumn giving the length bin each age composition was conditioned on and ignorefamily, since CAAL has only the discrete likelihoods. Required whenmodelis supplied andindex_sourceisNULLandtag = FALSE.- index_source
One of
"Catch","Discard","FishIdx","SrvIdx", identifying which continuous (log-normal) index-type data source to pull internal OSA residuals for. When supplied, takes precedence overcomp_source/tag. Only used whenmodelis supplied.- family
Character,
"discrete"or"continuous"; which of the two internally-tracked OSA vectors to use forcomp_source(a source can have both, e.g. some fleets multinomial and others logistic-normal). Only used whenmodelis supplied,tag = FALSE, andindex_sourceisNULL.- pop
Logical; population-specific composition or index source. Only used when
modelis supplied andtag = FALSE. DefaultFALSE.- discard
Logical; discard composition source (only valid for
comp_source %in% c("FishAge","FishLen")). Only used whenmodelis supplied,tag = FALSE, andindex_sourceisNULL. DefaultFALSE.- tag
Logical; if
TRUE(andmodelis supplied, andindex_sourceisNULL), compute internal OSA residuals for conventional tag recapture data instead of composition data. DefaultFALSE.- osa_method
Optional override for
RTMB::oneStepPredict'smethod, used only in internal mode. Must be one of"oneStepGeneric","oneStepGaussianOffMode", or"oneStepGaussian"; the"cdf"method is not permitted (it is numerically fragile for the discrete likelihoods used here). Defaults to"oneStepGeneric"for discrete families/tags and"oneStepGaussianOffMode"for continuous (logistic-normal) composition families.- parallel
Whether or not to parallelize OSA computation in internal mode. Defaults to
FALSE.
Value
A list with one element:
- res
Data frame of OSA residuals. Columns include:
fleet,index_label,year,index,resid,region,seas,sex, andcomp_type(composition sources, external or internal);fleet,region,cohort,release_year/release_region/release_season,recovery_year/recovery_season,years_at_liberty,resid, andcomp_type = "Tag"(tag = TRUE); orfleet,region,year,season,pop,resid, andidx_typeset toindex_source(index_sourcesupplied.
Details
When computing OSA residuals for population-specific composition data,
slice the leading population dimension from the obs_mat and
exp_mat arrays before passing them to this function. For example,
to compute residuals for population p:
get_osa(obs_mat = Obs_FishAge_pop_mat[p,,,,,,],
exp_mat = Pred_FishAge_pop_mat[p,,,,,,],
...)Population-specific composition arrays returned by
get_comp_prop are dimensioned
[n_pop × n_regions × n_years × n_seas × n_bins × n_sexes × n_fleets].
Slicing on p yields a 6D array matching the expected input dimensions.
For internal (model-based) OSA residuals, fit the model with
do_internal_comp_osa = TRUE and/or do_internal_conv_tag_osa =
TRUE (set in Setup_Mod_Dim), then call, e.g.: