language.Rd
Additional meta-functions available for use in G3 formula.
Whilst used as functions, these functions alter the code output of the model, rather than appearing directly.
Adds a - 1
to the supplied expression, but only in C++ (which has 0-based
indexes). Under R the expression is passed through unchanged.
Note: This is generally for internal use, as [[
will do this
automatically for you.
For example, g3_idx(a)
will be replaced with a
in R output
and a - 1
in C++ output.
Reference a scalar parameter by name. Arguments:
Variable name for parameter. Required
Initial value in model parameter_template. Default 0
Initial optimise setting in parameter_template. Default TRUE
Initial random setting in parameter_template. Default FALSE
Initial lower setting in parameter_template. Default NA
Initial upper setting in parameter_template. Default NA
For example, g3_param("ling.Linf")
will register a scalar parameter
called ling.Linf, available in the model parameter template, and be
replaced by a reference to that parameter.
g3_param("ling.Linf")
can be used multiple times, to refer to the same
value.
Reference a vector parameter by name. Arguments:
Variable name for parameter. Required
Initial value for use in model paramter_template. Default 0
Same as g3_param
, but the parameter will be expected to be a vector.
You can then dereference with [[
.
For example, g3_param_vector("lingimm.M")[[age - 3 + 1]]
.
Reference a lookup-table of parameters.
Variable name for parameter. Required
A data.frame, one column for each variable to check, one row for possible values. Required
Initial value for use in model parameter_template. Default 0
Initial optimise setting in parameter_template. Default TRUE
Initial random setting in parameter_template. Default FALSE
Initial lower setting in parameter_template. Default NA
Initial upper setting in parameter_template. Default NA
Value to return when outside of table bounds. Default NaN with warning if a value is missing
This is similar to providing a vector, but can use values in the model to provide bounds-checking.
The function takes 2 arguments, a prefix for the generated parameters, and a
data.frame of variables to possible values. expand.grid
can be
used to produce a cross product of all provided variables.
Note: The variables referenced will need to be integer variables, most
likely iteration variables such as cur_year
, age
,
area
...
For example, the following: g3_param_table('lingimm.M', expand.grid(age = seq(ling_imm__minage,
ling_imm__maxage)))
will generate parameters
lingimm.M.3..lingimm.M.10, assuming that ling_imm has ages 3..10.
The call to g3_param_table
will be replaced with
param[[paste("lingimm.M", age, sep = ".")]]
, or equivalent code
in C++.
g3_with(var1 := val1, var2 := val2, { x <- val1 * val2 })
is equivalent to
local({var1 <- val1, var2 <- val2, { x <<- val1 * val2 } })
However, we don't make a new environment for the code block in R, only in C++.