Package 'gadgetutils'

Title: Tools to create and maintain gadget3 models
Description: A collection of functions to aid (1) setting up and maintaining gadget3 models, (2) optimising, analysing, and running diagnostics on gadget3 models.
Authors: William Butler [aut, cre] (ORCID: <https://orcid.org/0000-0002-3286-0748>), Bjarki Thor Elvarsson [aut] (ORCID: <https://orcid.org/0000-0001-5855-1188>), Jamie Lentin [aut] (ORCID: <https://orcid.org/0000-0001-5727-2996>), Mikko Vihtakari [aut] (ORCID: <https://orcid.org/0000-0003-0371-4319>), Marine and Freshwater Research Institute (Iceland) [cph]
Maintainer: William Butler <[email protected]>
License: GPL-3
Version: 0.0.0.9000
Built: 2026-12-02 09:35:13 UTC
Source: https://github.com/gadget-framework/gadgetutils

Help Index


Add gadget3 attributes to an existing data frame

Description

Adds gadget3 attributes and missing columns to a a data frame ready to be passed to gadget3. Useful for importing data manually to gadget without the mfdb step.

Usage

add_g3_attributes(x, params, warn = 1)

Arguments

x

A data frame, tibble or data table.

params

A list of parameters defining the grouping. See details in mfdb_sample_count.

warn

Integer value to set the handling of warning messages. See the warn argument in options for details. In brief, set to negative to ignore warnings, to 1 to print warnings as they occur and to 2 to stop executing and return an error instead of a warning. The last is meant to prevent passing information that may cause unintended behavior in gadget3 models and is recommended.

Value

Returns x with g3 attributes and missing columns as instructed by the params argument.


Combine multiple gadget.fit objects

Description

This function combines multiple gadget.fit objects into a single fit object. An additional column 'id' is added to each element of the resulting list to identify the individual gadget.fits

Usage

bind_fit(fit_list)

Arguments

fit_list

A list of multiple gadget.fit objects.

Details

The 'id' column will be taken from names(fit_list). If fit_list has no names, the 'id' column will be taken from the positions, e.g., fit_list = list(a = fit1, b = fit2) will be produces id's of 'a' and 'b', whereas fit_list = list(fit1, fit2) will produce id's of '1' and '2'

Value

A gadget.fit object


Combine individual elements from multiple gadget.fit objects

Description

This function extracts a specific element from multiple gadget.fit objects and combines them into a single dataframe. The output has an additional column 'id' that distinguishes each input gadget.fit object

Usage

bind_fit_components(fit_list, component)

Arguments

fit_list

A list of multiple gadget.fit objects.

component

A string specifying which list element to extract (ie, see names(fit))

Details

The 'id' column will be taken from names(fit_list). If fit_list has no names, the 'id' column will be taken from the positions, e.g., fit_list = list(a = fit1, b = fit2) will be produces id's of 'a' and 'b', whereas fit_list = list(fit1, fit2) will produce id's of '1' and '2'

Value

A data frame that contains an 'id' column


Internal function to check whether g3_optim has succeeded or crashed. Used by the gadgetutils g3_* functions.

Description

check_params_out checks for NULLs and try-errors in a list of g3 parameters

Usage

check_params_out(param_list, id_name = NULL)

Arguments

param_list

List of g3 parameters

id_name

replacement name for 'id' in the output

Value

A data.frame that has collated the 'summary' attribute of each element of param_list.


Shortcut weights

Description

Estimates the weights for gadget3 likelihood components using shortcut (ie not iterative re-weighting) methods

Usage

estimate_weights(model, params)

Arguments

model

A gadget3 model of class 'g3_cpp' or 'g3_r'

params

A gadget3 parameter dataframe

Details

This function attempts to estimate the weights of gadget3 likelihood components. The shortcut weights are calculated by taking the inverse of the residual variance. How the residual variance is calculated depends on the type of likelihood distribution, i.e., whether it is a catch or abundance distribution. The residual variance calculations are performed by the helper functions 'estimate_weights_cdist' (catch distributions) and 'estimate_weights_adist' (abundance distributions)


Residual variance for abundance distributions

Description

This function fits a loess model to abundance data and returns the residual variance of the model fit

Usage

estimate_weights_adist(dat)

Arguments

dat

Abundance distribution model report (as a dataframe)


Residual variance for catch distributions

Description

Calculates the residual variance for catch distributions using a GLM

Usage

estimate_weights_cdist(dat)

Arguments

dat

Catch distribution model report (as a dataframe)


Convert normalised bounded form back to value

Description

Convert normalised bounded form back to value

Usage

eval_bounded(x, lower, upper)

Arguments

x

Input normalised value

lower

Lower bound for x

upper

Upper bound for x

Value

Raw value for x without bounding


Extracts year and step columns

Description

Gadget3 reports have a time column in the format 'YEAR-STEP'. This function creates a 'year' and 'step' column by splitting the existing 'time' column. This is useful for processing gagdet3 reports.

Usage

extract_year_step(data)

Arguments

data

dataframe of gadget3 reports. Must include a 'time' column.


Added parscale to a parameter template. No longer needed.

Description

Added parscale to a parameter template. No longer needed.

Usage

g3_add_parscale(parameters)

Arguments

parameters

A gadget3 parameter dataframe


Aggregate count data from a data frame to gadget3

Description

Aggregates data from a data frame ready to be passed to gadget3. Useful for generating datasets for g3l_catchdistribution, g3l_abundancedistribution and g3_timeareadata. Imitates the behavior of mfdb_sample_* functions and returns the required object attributes as instructed by the params argument.

Usage

g3_data(
  x,
  params = list(),
  method = "count",
  column_names = c(year = "year", step = "month", area = "areacell"),
  floating_point_correction = TRUE,
  verbose = FALSE
)

Arguments

x

A data frame, tibble or data table

params

A list of parameters defining the grouping. See details in mfdb_sample_count.

method

A character defining the aggregation method: use "count" to count occurrences or name of the column that should be summarized in x based on grouping defined by params. In other cases, use the column name that should be aggregated. The column will be summed based on year, step, area, and any additional columns defined in params.

column_names

A named vector explaining the column names which gadget3 expects in x if not the same than in mfdb. See the default suggestion for example.

floating_point_correction

Logical indicating whether 1e-6 should be added to all numeric columns except for year and step. Corrects for floating point issues associated with certain numbers such as 28 and 57.

verbose

Logical indicating whether the function should return messages about potential assumptions when expected data are not specified.

Details

This function attempts to do the same than mfdb_sample_* functions: summarise data ready for gadget3, with the difference that this function uses dataframes instead of a SQL database as input data. Consequently, this function should be independent of mfdb if the functions that generate groups and intervals as the mfdb package (mfdb_interval, mfdb_group) were imported to a project with this function.

Importantly, g3_data() does not attempt to filter other data than length unlike mfdb_sample_* functions that filter all columns passed to params.


Build output summary suitable for feeding into gadgetplots

Description

Build output summary suitable for feeding into gadgetplots

Usage

g3_fit(
  model,
  params,
  rec.steps = 1,
  steps = 1,
  printatstart = 1,
  f_by_age = NULL
)

Arguments

model

A G3 model built using g3_to_r or g3_to_tmb. The model must include g3a_report_detail.

params

The fitted parameters post-optimisation

rec.steps

Which steps should be considered recruitment steps? Vector of int, or NULL for all steps.

steps

Which steps to include in the annual output? Vector of int.

printatstart

Should the stock standard be printed at the start of the timestep (1) or the end (0)

f_by_age

The age range over which the annual fishing mortality (F) in res.by.year is averaged. Either:

  • NULL (default), the oldest age of each stock, i.e. assuming the top of a logistic selection curve;

  • a vector of the minimum and maximum age (or all ages, e.g. 4:7) applied to all stocks;

  • a named list with an age range for specific stocks, e.g. list(imm = c(1, 2), mat = c(5, 6)). The oldest age is used for stocks not included in the list.

F is NA for stocks with no ages within the range.

Details

The fishing mortality (F) in f.by.year and res.by.year is calculated separately for each stock and area. F in a step is −log(1−C/N)-log(1 - C/N), where CC is the number caught by fleets and NN is the number at the beginning of the step. F at age is summed over the steps of the year, and averaged (unweighted) over the ages in f_by_age. By default, F is that of the oldest age of each stock only.

Value

List of tibbles


Set initial guesses for parameter values in g3_cpp parameter template

Description

Set initial guesses for parameter values in g3_cpp parameter template

Usage

g3_init_guess(
  params,
  pattern,
  value = 0,
  lower = -999,
  upper = 999,
  optimise = 0
)

Arguments

params

Parameter template from g3_to_tmb()

pattern

Regular expression of parameter names in template to change

value

Initial value for all parameters matching pattern

lower

Lower bound to set for all parameters matching pattern

upper

Upper bound to set for all parameters matching pattern

optimise

1 if parameters matching pattern should be optimised, zero otherwise.

Value

Updated parameter template


Iterative re-weighting

Description

Perform multiple optimisation runs of a model, reweighting with each run

Usage

g3_iterative(
  gd,
  wgts = "WGTS",
  model,
  params.in,
  grouping = g3_iterative_default_grouping(params.in),
  use_parscale = TRUE,
  method = "BFGS",
  control = list(),
  shortcut = FALSE,
  cv_floor = 0,
  resume_final = FALSE,
  serial_compile = FALSE,
  mc.cores = parallel::detectCores()
)

g3_iterative_default_grouping(
  params.in,
  nll_dist_names = c("ldist", "aldist", "matp", "sexdist", "SI", "len\\d+SI")
)

Arguments

gd

Directory to store output

wgts

Directory name within gd to store run outputs

model

A G3 model, produced by g3_to_r() or g3_tmb_adfun()

params.in

Initial parameters to use with the model

grouping

List of component names to optmise together

use_parscale

Logical indicating whether optim(control$parscale) should be used

method

The optimisation method, see optim

control

List of control options for optim, see optim

shortcut

If TRUE, weights for each component will be approximated and a final optimisation performed

cv_floor

Minimum value of survey components (adist_surveyindices) as 1/cv_floor, applied prior to second stage of iterations.

resume_final

Logical value. If TRUE the re-weighting procedure starts at the second stage.

serial_compile

g3_tmb_adfun will be run in serial mode (i.e., not in parallel), potentially helping with memory issues

mc.cores

number of cores used, defaults to the number of available cores

nll_dist_names

Character vector of postfixes to consider when looking for groupings

Details

An implementation of the iterative reweigthing of likelihood components in gadget. It analyzes a given gadget model and, after a series of optimisations where each likelihood component is heavily weigthed, suggests a weigthing for the components based on the respective variance. If one (or more) components, other than understocking and penalty, are 0 then the gadget optimisation with the final weights will not be completed.

In Taylor et. al an objective reweighting scheme for likelihood components is described for cod in Icelandic waters. The authors nota that the issue of component weighting has been discussed for some time, as the data sources have different natural scales (e.g g vs. kg) that should not affect the outcome. A simple heuristic, where the weights are the inverse of the initial sums of squares for the respective component resulting in an initials score equal to the number of components, is therfor often used. This has the intutitive advantage of all components being normalised. There is however a drawback to this since the component scores, given the initial parametrisation, are most likely not equally far from their respective optima resulting in sub-optimal weighting. The iterative reweighting heuristic tackles this problem by optimising each component separately in order to determine the lowest possible value for each component. This is then used to determine the final weights. The resoning for this approach is as follows: Conceptually the likelihood components can be thought of as residual sums of squares, and as such their variance can be esimated by dividing the SS by the degrees of freedom. The optimal weighting strategy is the inverse of the variance. Here the iteration starts with assigning the inverse SS as the initial weight, that is the initial score of each component when multiplied with the weight is 1. Then an optimisation run for each component with the intial score for that component set to 10000. After the optimisation run the inverse of the resulting SS is multiplied by the effective number of datapoints and used as the final weight for that particular component. The effective number of datapoints is used as a proxy for the degrees of freedom is determined from the number of non-zero datapoints. This is viewed as satisfactory proxy when the dataset is large, but for smaller datasets this could be a gross overestimate. In particular, if the surveyindices are weigthed on their own while the yearly recruitment is esimated they could be overfitted. If there are two surveys within the year Taylor et. al suggest that the corresponding indices from each survey are weigthed simultaneously in order to make sure that there are at least two measurement for each yearly recruit, this is done through component grouping which is implemented.

Component grouping is oftern applied in cases where overfitting is likely, and this can also happen with catch composition data as well as survey indices.

In addition to grouping, a maximum weight can be assigned survey indices vie the cv_floor setting. The cv_floor parameter sets the minimum of the estimated component variance and thus the maximum of the inverser variance.

Weights are calculated using inverse-variance weighting (1/σ21/\sigma^2), and as 1/pmax(variance, cv_floor), hence the minimum value for survey components is 1/cv_floor. Use smaller cv_floor values to increase the weight of survey components.

g3_iterative_default_grouping

This assumes that your likelihood component names are of the form ⁠(nll_group)_(nll_dist)⁠, where (nll_dist) matches one of the regexes in nll_dist_names. For example, afleet_ldist, afleet_aldist, bfleet_ldist. afleet & bfleet will be the groups used.

Value

Final set of parameters

g3_iterative_default_grouping

A list of component groups to component names, as required by the grouping parameter


Initial parameters for iterative re-weighting

Description

Initial parameters for iterative re-weighting

Usage

g3_iterative_setup(lik_out, grouping = list())

Arguments

lik_out

A likelihood summary dataframe. The output of g3_lik_out(model, param)

grouping

A list describing how to group likelihood components for iterative re-weighting

Details

Initial weights are calculated by taking the inverse SS and then multiplying these by 10000 for the components that are to be optimised

Value

A list of G3 parameter dataframes, one for each group of likelihood components


Jitter analysis

Description

g3_jitter performs a jitter analysis.

Usage

g3_jitter(
  gd,
  outdir = "JITTER",
  model,
  params,
  njits = 10,
  jitter_fraction = 0.1,
  pattern_to_ignore = NULL,
  within_bounds = TRUE,
  use_parscale = TRUE,
  method = "BFGS",
  control = list(),
  serial_compile = FALSE,
  mc.cores = parallel::detectCores()
)

Arguments

gd

Directory to store output

outdir

Directory name within gd to store run outputs

model

A G3 model, produced by g3_to_tmb() or g3_to_r()

params

Initial parameters to use with the model, this should be a TMB parameter template i.e. attr(tmb_model, 'parameter_template')

njits

Number of jitters to run, defaults to 10

jitter_fraction

The fraction of jittering for a value

pattern_to_ignore

Regular expression of parameters to avoid jittering

within_bounds

Logical, if TRUE, jittered values that fall outside parameter bounds (params$lower, params$upper) will be adjusted to fall within the bounds

use_parscale

Logical indicating whether optim(control$parscale) should be used

method

The optimisation method, see optim

control

List of control options for optim, see optim

serial_compile

g3_tmb_adfun will be run in serial mode (i.e., not in parallel), potentially helping with memory issues

mc.cores

The number of cores to use, defaults to the number available

Value

A list of optimised parameter data frames (one for each jitter)


Leave-one-out analysis.

Description

Perform a leave-one-out analysis. This involves running a set of optimisations, each one of which has a component (or set of components) turned off (i.e. weight = 0).

Usage

g3_leaveout(
  gd,
  outdir = "LOCV",
  model,
  params,
  grouping = list(),
  use_parscale = TRUE,
  method = "BFGS",
  control = list(),
  serial_compile = FALSE,
  mc.cores = parallel::detectCores()
)

Arguments

gd

Directory to store output

outdir

Directory name within gd to store run outputs

model

A G3 model, produced by g3_to_tmb() or g3_to_r()

params

Initial parameters to use with the model, this should be a TMB parameter template i.e. attr(tmb_model, 'parameter_template')

grouping

List of component names to group together. This allows you to leave out multiple components at a time, e.g. length and age-length distributions from a particular fleet

use_parscale

Logical indicating whether optim(control$parscale) should be used

method

The optimisation method, see optim

control

List of control options for optim, see optim

serial_compile

g3_tmb_adfun will be run in serial mode (i.e., not in parallel), potentially helping with memory issues

mc.cores

The number of cores to use, defaults to the number available

Details

g3_leaveout performs a leave-one-out analysis.

Value

A list of optimised parameter data frames (one for each group)


Likelihood summary for a model run

Description

Likelihood summary for a model run

Usage

g3_lik_out(model, param)

Arguments

model

A G3 model, produced by g3_to_r() or g3_tmb_adfun()

param

A G3 parameter dataframe

Details

Runs the model, sums the number of data points (df) and nll's for each component (value), and extracts the corresponding weights from the input parameters (weight)

Value

A dataframe containing the likelihood component name, degrees of freedom, NLL value, and weight


Wrapper for stats::optim that returns the g3_cpp parameter template with optimised values

Description

Wrapper for stats::optim that returns the g3_cpp parameter template with optimised values

Usage

g3_optim(
  model,
  params,
  use_parscale = TRUE,
  method = "BFGS",
  control = list(),
  print_status = FALSE,
  print_id = "g3_optim",
  check_finite = TRUE,
  ...
)

Arguments

model

Either a g3 model of class 'g3_cpp', i.e., model <- gadget3::g3_to_tmb(actions), or an objective function from gadget3::g3_tmb_adfun(model, param)

params

The parameter template for a g3_cpp classed model.

use_parscale

Should parscale be used g3_tmb_parscale(params), see optim

method

The optimisation method, see optim

control

List of control options for optim, see optim

print_status

Print step comments, does not suppress comments from optim call (see control$trace)

print_id

A character string appended to the print statements (useful for when running g3_optim in parallel)

check_finite

boolean Should we check that initial/final conditions produce finite results before running?

...

Further arguments to be passed gadget3::g3_tmb_adfun, see g3_tmb_adfun

Value

A g3_cpp parameter template with optimised values


Analytical retrospective

Description

g3_retro runs an analytical retrospective model fitting run.

Usage

g3_retro(
  gd,
  outdir = "RETRO",
  model,
  params,
  nyears = 5,
  use_parscale = TRUE,
  method = "BFGS",
  control = list(),
  serial_compile = FALSE,
  mc.cores = parallel::detectCores()
)

Arguments

gd

Directory to store output

outdir

Directory name within gd to store run outputs

model

A G3 model, produced by g3_to_r() or g3_tmb_adfun()

params

Initial parameters to use with the model, this should be a TMB parameter template i.e. attr(tmb_model, 'parameter_template')

nyears

How many years back to go

use_parscale

Logical indicating whether optim(control$parscale) should be used

method

The optimisation method, see optim

control

List of control options for optim, see optim

serial_compile

g3_tmb_adfun will be run in serial mode (i.e., not in parallel), potentially helping with memory issues

mc.cores

The number of cores to use, defaults to the number available

Value

List optimised TMB parameter data.frames, the name of each list element corresponds to the number of years peeled in the retrospective analysis.


Sensitivity analysis

Description

g3_sensitivity performs a sensitivity analysis for a single parameter.

Usage

g3_sensitivity(
  gd,
  outdir = "SENS",
  model,
  params,
  par_name,
  par_values,
  use_parscale = TRUE,
  method = "BFGS",
  control = list(),
  serial_compile = FALSE,
  return_input_parameters = FALSE,
  run_optimisations = TRUE,
  mc.cores = parallel::detectCores()
)

Arguments

gd

Directory to store output

outdir

Directory name within gd to store run outputs

model

A G3 model, produced by g3_to_tmb() or g3_to_r()

params

Initial parameters to use with the model, this should be a TMB parameter template i.e. attr(tmb_model, 'parameter_template')

par_name

A glob-like string to match parameter names, see the 'name_spec' argument for g3_init_val

par_values

A vector of values to substitute into params

use_parscale

Logical indicating whether optim(control$parscale) should be used

method

The optimisation method, see optim

control

List of control options for optim, see optim

serial_compile

g3_tmb_adfun will be run in serial mode (i.e., not in parallel), potentially helping with memory issues

return_input_parameters

If TRUE, returns a list of modified parameter dataframes (i.e. without optimisations)

run_optimisations

If TRUE, will return a list of fits using the modified (non-optimised) parameters

mc.cores

The number of cores to use, defaults to the number available

Value

A list of parameter dataframes (either optimised or not) or a list of fits based on the unoptimised parameters


Extract parts from stock name

Description

Extract parts from stock name

Usage

g3_stock_name(stock, id = "full")

Arguments

stock

A g3 stock object

id

Part to extract, or 'full' to return the stock name

Value

Part from stock name


Updates weights for second stage of iterative re-weighting

Description

Updates weights for second stage of iterative re-weighting

Usage

g3_update_weights(lik_out_list, grouping, cv_floor)

Arguments

lik_out_list

A list of likelihood summarys. The output of g3_lik_out(model, param) for each group of likelihood components

grouping

A list describing how to group likelihood components for iterative re-weighting

cv_floor

Minimum value of survey components (adist_surveyindices) as 1/cv_floor, applied prior to second stage of iterations.

Details

This function updates weights for the second stage of iterative re-weighting. If the cv_floor is > 0, it will be applied here.

Value

A list of G3 parameter dataframes, one for each group of likelihood components


Generate parameterised g3a_initial_abund

Description

Generate parameterised g3a_initial_abund

Usage

init_abund(
  imm,
  mat,
  comp_id = "species",
  mature = TRUE,
  init_mode = 1,
  exp_init = FALSE,
  exp_init_scalar = FALSE,
  exp_init_f = FALSE,
  naturalmortality = gadget3::g3_parameterized("M", by_stock = TRUE)
)

Arguments

imm

A g3 stock object for immature specimens

mat

A g3 stock object for mature specimens

comp_id

Part of stock name to use for parameters, e.g. 'species' will share parameters with both mature/immature

mature

Generate actions for mature (TRUE) or immature (FALSE) stock

init_mode

One of: 0 (A single scalar that decays with mortality), 1 (Initial parameter per age group (across stocks)), 2 (Initial parameter per age group per stock), 3 (Initialised at equilibrium, special case for cetaceans, see https://gadget-framework.github.io/gadget-course/marine-mammals.html)

exp_init

Logical, whether the initial age-specific parameters should be exponentiated

exp_init_scalar

Logical, whether the initial scalar should be exponentiated

exp_init_f

Logical, whether the parameter 'init.f' should be exponentiated

naturalmortality

A g3 parameter for natural mortality

Value

g3a_initial_abund formula for use in g3a_initialconditions_normalparam()


Generate parameterised initial conditions

Description

Generate parameterised initial conditions

Usage

init_sd(stock, id, parametric = FALSE, mean_len)

Arguments

stock

A g3 stock object

id

Part of the stock name to use in parameter name

parametric

Is the initial conditions stddev parameterised, or a table by age?

mean_len

The mean length function (only relevant if parametric = TRUE)

Value

A formula suitable for g3a_initialconditions_normalparam()


Creates a jittered g3_tmb parameter template

Description

Creates a jittered g3_tmb parameter template

Usage

jitter_params(
  params,
  jitter_fraction = 0.1,
  pattern_to_ignore = NULL,
  within_bounds = TRUE
)

Arguments

params

A g3 tmb parameter template

jitter_fraction

The fraction of jittering for a value

pattern_to_ignore

Regular expression of parameters to avoid jittering

within_bounds

Logical, if TRUE, jittered values that fall outside parameter bounds (params$lower, params$upper) will be adjusted to fall within the bounds

Value

Jittered set of parameters


Generate a standard set of model actions

Description

Generate a standard set of model actions

Usage

model_actions(
  imm,
  mat,
  mlgg = 15,
  mature = TRUE,
  comp_id = "species",
  rec_id = list(imm, mat),
  rec_scalar_id = list(imm, mat),
  init_mode = 1,
  parametric_sd = FALSE,
  exp_params = c(),
  tv_params = c(),
  by_age_params = c(),
  recruiting = !mature
)

Arguments

imm

A g3 stock object for immature specimens

mat

A g3 stock object for mature specimens

mlgg

maxlengthgroupgrowth for growth of both mature/immature

mature

Generate actions for mature (TRUE) or immature (FALSE) stock

comp_id

Part of stock name to use for parameters, e.g. 'species' will share parameters with both mature/immature

rec_id

Part of stock name to use for recruitment parameters, e.g. 'species' will share parameters with both mature/immature

rec_scalar_id

Part of stock name to use for recruitment scalar parameter, e.g. 'species' will share parameters with both mature/immature

init_mode

One of 0 (initialised at equilibrium), 1 (Initial parameter per age group (across stocks)), 2 (Initial parameter per age group per stock)

parametric_sd

Is the initial conditions stddev parameterised, or a table by age?

exp_params

Which parameters should be exponentiated? exp_params is a vector of parameter names, possible parameters include: c('linf','k','bbin','recl','rec.sd','mat_alpha','mat_l50','init','init.scalar','rec','rec.scalar','init.f','m','walpha','wbeta'). Note that is a scalar is exponentiated the annual values will be too, and vice versa.

tv_params

Which parameters should be time-varying? tv_params is a vector of parameter names, possible time-varying parameters include: 'linf','k','walpha','beta','bbin','recl','rec.sd','mat_alpha','mat_l50','m'

by_age_params

Which parameters should be age-varying? by_age_params is a vector of parameter names, possible time-varying parameters include: 'linf','k','walpha','beta','bbin','mat_alpha','mat_l50','m'

recruiting

If TRUE, the renewal action will be added. Defaults to !mature, so it can be used to create a single non-maturing stock.

Value

A list of g3 actions


Generate parameterised g3a_initial_abund

Description

Generate parameterised g3a_initial_abund

Usage

mohnrho(
  fit,
  retrofit,
  vars = c("total.biomass", "F", "recruitment"),
  lag = c(0, 1, 1),
  aggregate_biomass = TRUE
)

Arguments

fit

A g3_fit object

retrofit

A list of g3_fit objects that differ in their terminal year, i.e. the result of a retrospective analysis

vars

Which variables would you like to calculate Mohn's Rho for? They must be columns in fit$res.by.year

lag

vector of lags for each variable in terms of years

aggregate_biomass

calculate rho for biomass over stocks? Only works if 'total.biomass' is in vars

Value

A data.frame with mohn's rho for each variable by stock


Reads a file from write.g3.file to an object. Now depreciated, using RData files for read/write.

Description

Reads a file from write.g3.file to an object. Now depreciated, using RData files for read/write.

Usage

read.g3.param(gd, file.name)

Arguments

gd

Directory to write the file

file.name

Name of the file


Wrapper for g3_optim. This is an internal function used by the g3_* functions

Description

Wrapper for g3_optim. This is an internal function used by the g3_* functions

Usage

run_g3_optim(
  model,
  params,
  use_parscale,
  method,
  control,
  serial_compile,
  mc.cores,
  ...
)

Arguments

model

Either a g3 model of class 'g3_cpp', i.e., model <- gadget3::g3_to_tmb(actions), or an objective function from gadget3::g3_tmb_adfun(model, param)

params

A list of parameter templates for a g3_cpp classed model.

use_parscale

Should parscale be used g3_tmb_parscale(params), see optim

method

The optimisation method, see optim

control

List of control options for optim, see optim

serial_compile

g3_tmb_adfun will be run in serial mode (i.e., not in parallel), potentially helping with memory issues

mc.cores

number of cores used, defaults to the number of available cores

...

Further arguments to be passed gadget3::g3_tmb_adfun, see g3_tmb_adfun

Value

A g3_cpp parameter template with optimised values


Generate paramerterised stock renewal factor

Description

Generate paramerterised stock renewal factor

Usage

stock_renewal(
  stock,
  id = "species",
  scalar_id = "species",
  exponentiate_rec = FALSE,
  exponentiate_rec_scalar = FALSE
)

Arguments

stock

A g3 stock object

id

Part of the stock name to use in parameter name

scalar_id

Part of the stock name to use in parameter name

exponentiate_rec

Should the annual renewal parameters be exponentiated?

exponentiate_rec_scalar

Should the renewal scalar be exponentiated?

Value

"scalar * renew" formula, parameterised for given stock


Extract species name from stock

Description

Extract species name from stock

Usage

stock_species(stock)

Arguments

stock

A g3 stock object

Value

Species name from stock


Calculates likelihood component scores, including scores normalised by the minimum value

Description

Calculates likelihood component scores, including scores normalised by the minimum value

Usage

tabulate_SS(lik_out, grouping)

Arguments

lik_out

Likelihood summary for a model run. Output of g3_lik_out(model, param)

grouping

A list describing how to group likelihood components for iterative re-weighting

Value

A list with a table of likelihood component scores (SS) and a table of normalised likelihood component scores (SS_norm)


Convert value into it's normalised form for use with bounded()

Description

Convert value into it's normalised form for use with bounded()

Usage

value_from_bounds(x, lower, upper)

Arguments

x

Raw value

lower

Lower bound for x

upper

Upper bound for x

Value

Normalised value for use with bounded()


Writes an object to a file, typically just used within gadgetutils g3_ functions

Description

Writes an object to a file, typically just used within gadgetutils g3_ functions

Usage

write.g3.file(obj, gd, file_name)

Arguments

obj

An object to write to file

gd

Directory to write the file

file_name

Name of the file


A wrapper for write.g3.file that formats a gadget3 TMB parameter data.frame for writing to file

Description

A wrapper for write.g3.file that formats a gadget3 TMB parameter data.frame for writing to file

Usage

write.g3.param(params, gd, file_name)

Arguments

params

A TMB parameter data.frame

gd

Directory to write the file

file_name

Name of the file