library(magrittr)
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') )
# 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) )
# by_stock, so the parameters will have the stock name prepended
param_template_for( g3_parameterized('K', by_stock = TRUE) )
# Similarly, we can prepend year & age
param_template_for( g3_parameterized('K', by_stock = TRUE, by_year = TRUE, by_age = TRUE) )
# 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) )
# 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)) )
param_template_for( g3_parameterized('K', by_stock = list(stock_imm, stock_mat), by_age = TRUE) )
# If there are no shared name parts, then all names will be added
param_template_for( g3_parameterized(
'btrigger',
by_stock = list(g3_fleet("surv"), g3_fleet("comm"))) )
# You can set fixed scale/offset for the parameter
g3_parameterized('K', scale = 5, offset = 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") )
Run the code above in your browser using DataLab