run_r.Rd
Convert g3 actions into an R function that can then be executed
A list of actions (i.e. list of formula objects), as produced by g3a_* functions.
Where to write the temporary R script containing your function
options to pass through to compiler::cmpfun()
.
If NULL
, then don't run the model through the byte-code compiler
If TRUE, turn all comments into print statements.
If TRUE, enable extra sanity checking in actions. Any invalid conditions (e.g. more/less fish after growth) will result in a warning.
The g3_to_r
-generated function to print
If TRUE, list data stored in function environment when printing
If TRUE, show parameter template when printing
Other arguments
A function that takes a params variable, that defines all g3_params required by the model. The following attributes will be set:
The original actions list given to the function
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.
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
#> 20:24 24:28 28:32 32:36 36:40 40:44 44:48 48:52 52:56 56:60
#> 22 26 30 34 38 42 46 50 54 58
#> 60:64 64:68 68:72 72:76 76:80 80:84 84:88 88:92 92:96 96:100
#> 62 66 70 74 78 82 86 90 94 98
#> 100:104 104:108 108:112 112:116 116:120 120:124 124:128 128:132 132:136 136:140
#> 102 106 110 114 118 122 126 130 134 138
#> 140:144 144:148 148:152 152:156 156:Inf
#> 142 146 150 154 158