Shortcuts to parameterise a model with g3_param

g3_parameterized(
        name,
        by_stock = FALSE,
        by_predator = FALSE,
        by_year = FALSE,
        by_step = FALSE,
        by_age = FALSE,
        by_area = FALSE,
        exponentiate = FALSE,
        avoid_zero = FALSE,
        scale = 1,
        offset = 0,
        ...)

Arguments

name

Suffix for parameter name.

by_stock

Should there be individual parameters per-stock?

FALSE

No

TRUE

Produce a "stock_name.name" parameter

character vector

Select the stock name_part(s) to use, e.g. to produce "stock_species.name" parameter with "species"

List of g3_stock objects

Produce a parameter that applies to all given stocks

by_predator

Should there be individual parameters per-predator (read: per-fleet) stock?

FALSE

No

TRUE

Produce a "fleet_stock_name.name" parameter

character vector

Select the stock name_part(s) to use, e.g. to produce "fleet_country.name" parameter with "country"

List of g3_stock objects

Produce a parameter that applies to all given stocks

by_year

Should there be individual parameters per model year?

FALSE

No

TRUE

Produce a "name.1998" parameter for each year the model runs

1998:2099

Override the year range, so when projecting there will be sufficient parameters available.

by_step

Should there be individual parameters per step within years?

FALSE

No

TRUE

Produce a "name.1" seasonal parameter for each step, or "name.1998.1" for every timestep in the model if combined with by_year.

by_age

Should there be individual parameters per stock age?

FALSE

No

TRUE

Produce a "name.4" parameter for each age of the stock(s) in by_stock

by_area

Should there be individual parameters per stock area?

FALSE

No

TRUE

Produce a "name.area" parameter for each area of the stock(s) in by_stock

exponentiate

Use exp(value) instead of the raw parameter value. Will add "_exp" to the parameter name.

avoid_zero

If TRUE, wrap parameter with avoid_zero

scale

Use scale * value instead of the raw parameter value. Either a numeric constant or character. If character, add another parameter for scale, using the same by_stock value.

offset

Use value + offset instead of the raw parameter value Either a numeric constant or character. If character, add another parameter for offset, using the same by_stock value.

...

Additional parameters passed through to g3_param, e.g. optimise, random, ...

Details

The function provides shortcuts to common formulas used when parameterising a model.

Value

A formula object defining the given parameters

Examples

stock_imm <- g3_stock(c(species = 'stock', 'imm'), seq(10, 35, 5)) %>% g3s_age(1, 4)
stock_mat <- g3_stock(c(species = 'stock', 'mat'), seq(10, 35, 5)) %>% g3s_age(3, 6)

# Helper function that shows the parameter template for the given parameter
param_template_for <- function (g3_param) {
  model_code <- g3_to_tmb(list(
      # g3a_naturalmortality() isn't important, it is a place to add our parameter
      g3a_naturalmortality(stock_imm, g3_param),
      # We also need stock_mat in the model at least once
      g3a_naturalmortality(stock_mat, 0),

      # Set a year range to use for parameters where relevant
      g3a_time(1990, 1994) ))

  # Extract template, throw away default parameters from g3a_time()
  params <- attr(model_code, "parameter_template")
  params <- params[!(rownames(params) %in% c('retro_years', 'project_years')),]
  return(params)
}

# Not 'by' anything, so we add a single parameter value
param_template_for( g3_parameterized('K') )
#>   switch type value optimise random lower upper parscale
#> K      K          0     TRUE  FALSE    NA    NA       NA

# Can set defaults for the parameter template when defining a parameter
param_template_for( g3_parameterized('K', value = 5, lower = 2,
    upper = 8, optimise = FALSE) )
#>   switch type value optimise random lower upper parscale
#> K      K          5    FALSE  FALSE     2     8       NA

# by_stock, so the parameters will have the stock name prepended
param_template_for( g3_parameterized('K', by_stock = TRUE) )
#>                  switch type value optimise random lower upper parscale
#> stock_imm.K stock_imm.K          0     TRUE  FALSE    NA    NA       NA

# Similarly, we can prepend year & age
param_template_for( g3_parameterized('K', by_stock = TRUE, by_year = TRUE, by_age = TRUE) )
#>                                switch type value optimise random lower upper
#> stock_imm.K.1990.1 stock_imm.K.1990.1          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1991.1 stock_imm.K.1991.1          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1992.1 stock_imm.K.1992.1          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1993.1 stock_imm.K.1993.1          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1994.1 stock_imm.K.1994.1          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1990.2 stock_imm.K.1990.2          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1991.2 stock_imm.K.1991.2          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1992.2 stock_imm.K.1992.2          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1993.2 stock_imm.K.1993.2          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1994.2 stock_imm.K.1994.2          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1990.3 stock_imm.K.1990.3          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1991.3 stock_imm.K.1991.3          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1992.3 stock_imm.K.1992.3          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1993.3 stock_imm.K.1993.3          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1994.3 stock_imm.K.1994.3          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1990.4 stock_imm.K.1990.4          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1991.4 stock_imm.K.1991.4          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1992.4 stock_imm.K.1992.4          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1993.4 stock_imm.K.1993.4          0     TRUE  FALSE    NA    NA
#> stock_imm.K.1994.4 stock_imm.K.1994.4          0     TRUE  FALSE    NA    NA
#>                    parscale
#> stock_imm.K.1990.1       NA
#> stock_imm.K.1991.1       NA
#> stock_imm.K.1992.1       NA
#> stock_imm.K.1993.1       NA
#> stock_imm.K.1994.1       NA
#> stock_imm.K.1990.2       NA
#> stock_imm.K.1991.2       NA
#> stock_imm.K.1992.2       NA
#> stock_imm.K.1993.2       NA
#> stock_imm.K.1994.2       NA
#> stock_imm.K.1990.3       NA
#> stock_imm.K.1991.3       NA
#> stock_imm.K.1992.3       NA
#> stock_imm.K.1993.3       NA
#> stock_imm.K.1994.3       NA
#> stock_imm.K.1990.4       NA
#> stock_imm.K.1991.4       NA
#> stock_imm.K.1992.4       NA
#> stock_imm.K.1993.4       NA
#> stock_imm.K.1994.4       NA

# You can specify the name part to use,
# e.g. if a parameter should be shared between mature and immature:
param_template_for( g3_parameterized('K', by_stock = 'species', by_year = TRUE) )
#>                    switch type value optimise random lower upper parscale
#> stock.K.1990 stock.K.1990          0     TRUE  FALSE    NA    NA       NA
#> stock.K.1991 stock.K.1991          0     TRUE  FALSE    NA    NA       NA
#> stock.K.1992 stock.K.1992          0     TRUE  FALSE    NA    NA       NA
#> stock.K.1993 stock.K.1993          0     TRUE  FALSE    NA    NA       NA
#> stock.K.1994 stock.K.1994          0     TRUE  FALSE    NA    NA       NA

# Can give a list of stocks, in which case it works out name parts for you
param_template_for( g3_parameterized('K', by_stock = list(stock_imm, stock_mat)) )
#>          switch type value optimise random lower upper parscale
#> stock.K stock.K          0     TRUE  FALSE    NA    NA       NA
param_template_for( g3_parameterized('K', by_stock = list(stock_imm, stock_mat), by_age = TRUE) )
#>              switch type value optimise random lower upper parscale
#> stock.K.1 stock.K.1          0     TRUE  FALSE    NA    NA       NA
#> stock.K.2 stock.K.2          0     TRUE  FALSE    NA    NA       NA
#> stock.K.3 stock.K.3          0     TRUE  FALSE    NA    NA       NA
#> stock.K.4 stock.K.4          0     TRUE  FALSE    NA    NA       NA
#> stock.K.5 stock.K.5          0     TRUE  FALSE    NA    NA       NA
#> stock.K.6 stock.K.6          0     TRUE  FALSE    NA    NA       NA

# You can set fixed scale/offset for the parameter
g3_parameterized('K', scale = 5, offset = 9)
#> g3_param("K") * 5 + 9

# ...or give names and they will also be parameters, sharing the by_stock setting
param_template_for( g3_parameterized('K', by_stock = TRUE, scale = "sc", offset = "offs") )
#>                        switch type value optimise random lower upper parscale
#> stock_imm.K       stock_imm.K          0     TRUE  FALSE    NA    NA       NA
#> stock_imm.sc     stock_imm.sc          0     TRUE  FALSE    NA    NA       NA
#> stock_imm.offs stock_imm.offs          0     TRUE  FALSE    NA    NA       NA