action_trace.Rd
Trace value of variables in a G3 model, warning if conditions are met
A list of model actions to add tracing to
Boolean, notify if variable is not finite (i.e. Inf, NA, NaN)
Boolean, notify if variable is < 0
Boolean, notify if variable is <= 0
What to do when a variable fails one of the checks? NB: "browser" will not work in a TMB-compiled model.
Boolean, if true print the value of the variable at the point the test fails. NB: This will not work in a TMB-compiled model.
Regular expression(s), variable whose name matches will be traced.
The main reason to use g3a_trace_var
is to find out why a model is producing NaN in reports / likelihood.
Adding this to your model will help pinpoint the action this originally occurs in, so you can inspect closer for incorrect settings and/or bugs.
The var_re parameter chooses which variables are traced, and should be tweaked to further pinpoint the problem.
Generally, once an error has been found, dig into the code (e.g. by doing edit(g3_to_r(actions))
),
and see what other variables are available for tracing.
Some pre-canned suggestions follow:
c("__num$", "__wgt$") (i.e. default)
This will trace abundance/weight for all stocks, and a good starting point.
^[stock_name]__(num|wgt|cons|suit|totalpredate|consratio|feedinglevel)$
This will, once [stock_name]
is replaced with the name of your stock,
dig deeper into the predation mechanisms.
stocks <- list(
st = g3_stock("st", 1:10 * 10) |> g3s_age(1, 5) )
actions <- list(
g3a_time(1990, 1995, c(3,3,3,3)),
g3a_initialconditions_normalcv(stocks$st),
g3a_growmature(stocks$st, impl_f = gadget3::g3a_grow_impl_bbinom(
maxlengthgroupgrowth = 2L) ),
NULL )
model_fn <- g3_to_r(c(actions,
list(g3a_trace_var(actions)),
g3a_report_detail(actions) ))
# Configure set of working parameters
attr(model_fn, "parameter_template") |>
g3_init_val("*.K", 0.3) |>
g3_init_val("*.t0", 0.2) |>
g3_init_val("*.Linf", 80) |>
g3_init_val("*.lencv", 0.1) |>
g3_init_val("*.walpha", 0.01) |>
g3_init_val("*.wbeta", 3) |>
g3_init_val("*.M.#", 0.01) |>
identity() -> params.in
nll <- model_fn(params.in) ; r <- attributes(nll) ; nll <- as.vector(nll)
# Try setting parameters to NaN and see what fails:
r <- model_fn(params.in |> g3_init_val("*.t0", NaN))
#> st__num failed test at 1990-1, after 'g3a_initialconditions for st'
#> st__wgt failed test at 1990-1, after 'g3a_grow for st'
r <- model_fn(params.in |> g3_init_val("*.bbin", NaN))
#> st__num failed test at 1990-1, after 'g3a_grow for st'
#> st__wgt failed test at 1990-1, after 'g3a_grow for st'