run_r: Gadget3 actions into R code

run_rR Documentation

Gadget3 actions into R code

Description

Convert g3 actions into an R function that can then be executed

Usage

g3_to_r(
        actions,
        work_dir = getOption('gadget3.r.work_dir', default = tempdir()),
        trace = FALSE,
        strict = FALSE,
        cmp_options = list(optimize = 3) )

## S3 method for class 'g3_r'
print(x, ..., with_environment = FALSE, with_template = FALSE)

Arguments

actions

A list of actions (i.e. list of formula objects), as produced by g3a_* functions.

work_dir

Where to write the temporary R script containing your function

cmp_options

options to pass through to compiler::cmpfun(). If NULL, then don't run the model through the byte-code compiler

trace

If TRUE, turn all comments into print statements.

strict

If TRUE, enable extra sanity checking in actions. Any invalid conditions (e.g. more/less fish after growth) will result in a warning.

x

The g3_to_r-generated function to print

with_environment

If TRUE, list data stored in function environment when printing

with_template

If TRUE, show parameter template when printing

...

Other arguments

Value

A function that takes a params variable, which can be:

  1. A list of parameters as defined by attr(fn, 'parameter_template')

  2. A data.frame of parameters defined by g3_to_tmb's parameter template

  3. Not provided, in which case the parameter defaults are used

The function will have the following attributes:

actions

The original actions list given to the function

parameter_template

A list of all parameters expected by the model, to fill in

Use e.g. attr(fn, 'parameter_template') to retrieve them.

Invariant model data will be stored as a closure, i.e. in environment(fn). This can be fetched with environment(fn)$cdist_sumofsquares_ldist_gil_obs__num.

The function will return nll produced by the model. You can also use attributes(nll) to get any report variables from the model.

Examples


ling_imm <- g3_stock(c(species = 'ling', 'imm'), seq(20, 156, 4)) %>% g3s_age(3, 10)

initialconditions_action <- g3a_initialconditions_normalparam(
    ling_imm,
    factor_f = g3a_renewal_initabund(by_stock_f = 'species'),
    by_stock = 'species',
    by_age = TRUE)

# Timekeeping action
time_action <- g3a_time(
    start_year = 2000,
    end_year = 2004,
    c(3, 3, 3, 3))

# Generate a model from the above 2 actions
# NB: Obviously in reality we'd need more actions
fn <- g3_to_r(list(initialconditions_action, time_action))

if (interactive()) {
  # Edit the resulting function
  fn <- edit(fn)
}

param <- attr(fn, 'parameter_template')
param$project_years <- 0
param$ling.init.F <- 0.4
param$ling.Linf <- 160
param$ling.K <- 90
param$ling.recl <- 12
param$recage <- g3_stock_def(ling_imm, 'minage')
param[grepl('^ling.init.sd.', names(param))] <- 50.527220
param[grepl('^ling_imm.init.\\d+', names(param))] <- 1
param$ling_imm.init.scalar <- 200
param$ling_imm.walpha <- 2.27567436711055e-06
param$ling_imm.wbeta <- 3.20200445996187
param$ling_imm.M <- 0.15

# Run the model with the provided parameters
nll <- fn(param)

# Get the report from the last model run
report <- attributes(nll)

# Fetch a value from the model data
environment(fn)$ling_imm__midlen


gadget-framework/gadget3 documentation built on June 13, 2025, 5:06 a.m.