Constructs an expression mapping
Usage
bm_expr(
expr,
assume = character(0),
labels = NULL,
.envir = rlang::caller_env()
)Arguments
- expr
An expression object, typically created using
rlang::quo(), that can be evaluated in a data mask containing the root variables, derived variables, and data variables.- assume
character vector listing valid assumptions for the combination of the expression and data, derived variables, and root variables. This can be used to specify assumptions that allow for more efficient Jacobian calculations. For example, if the expression is row-wise in the data and derived variables, then
assume = "rowwise"can be used to indicate that the Jacobian with respect to derived variables can be calculated simultaneously for all rows. Supported assumptions are "rowwise", "linear", "additive", and "no_root".- labels
list of
root,derived, andsuffix. Therootandderivedelements specify the pronoun labels for the data mask for root and derived variables. Default "root" and "derived", respectively. For example, ifroot = "latent", then the root variables will be accessible in the expression as.latent$varname. Ifsuffixis non-NULL, a character string specifying a suffix to add to the root variable names when making them directly available to the expression. Default is NULL, equivalent to"". For example, ifsuffix = "_latent", then a root variable named"x"will be available as"x_latent"in the expression, in addition to the data mask pronoun version.- .envir
The environment for the expression evaluation. By default, this is set to the caller environment.
Details
The input is currently ignored.
data should be a list with data objects, with the main object called
data.
If is_rowwise == TRUE, the number of rows in the data data.frame
determines the number of rows in the output, and the columns can be used as
constants in the expression, accessed via .data$colname,
.data.$colname, or .data.[["colname"]].
See also
bru_mapper, bru_mapper_generics
Other mappers:
bm_aggregate(),
bm_collect(),
bm_const(),
bm_factor(),
bm_fm_mesh_1d,
bm_fmesher(),
bm_harmonics(),
bm_index(),
bm_linear(),
bm_logitaverage(),
bm_logsumexp(),
bm_marginal(),
bm_matrix(),
bm_multi(),
bm_pipe(),
bm_reparam(),
bm_repeat(),
bm_scale(),
bm_shift(),
bm_sum(),
bm_taylor(),
bru_get_mapper(),
bru_mapper()
Examples
# Basic expression with only root variables ("x").
(m <- bm_expr(rlang::quo(cos(x))))
#> expr(~cos(x))
ibm_eval(m, list(), list(x = 1:5))
#> [1] 0.5403023 -0.4161468 -0.9899925 -0.6536436 0.2836622
ibm_eval2(m, list(), list(x = 1:5))
#> $offset
#> [1] 0.5403023 -0.4161468 -0.9899925 -0.6536436 0.2836622
#>
#> $jacobian
#> 5 x 5 sparse Matrix of class "dgCMatrix"
#>
#> [1,] -0.8414713 . . . .
#> [2,] . -0.9092972 . . .
#> [3,] . . -0.1411195 . .
#> [4,] . . . 0.7568028 .
#> [5,] . . . . 0.9589241
#>
# Expression with data
(m <- bm_expr(rlang::quo(cos(x) * .data$z)))
#> expr(~cos(x) * .data$z)
the_data <- list(data = data.frame(z = 11:15))
ibm_eval(m, list(), list(x = 1:5), data = the_data)
#> [1] 5.943325 -4.993762 -12.869902 -9.151011 4.254933
ibm_eval2(m, list(), list(x = 1:5), data = the_data)
#> $offset
#> [1] 5.943325 -4.993762 -12.869902 -9.151011 4.254933
#>
#> $jacobian
#> 5 x 5 sparse Matrix of class "dgCMatrix"
#>
#> [1,] -9.256184 . . . .
#> [2,] . -10.91157 . . .
#> [3,] . . -1.834554 . .
#> [4,] . . . 10.59524 .
#> [5,] . . . . 14.38386
#>
# Expression with data, root variables, and derived variables.
(m <- bm_expr(
rlang::quo(sin(x_latent) + cos(.effect$y) * .data$z),
labels = list(root = "latent", derived = "effect", suffix = "_latent")
)
)
#> expr(~sin(x_latent) + cos(.effect$y) * .data$z)
ibm_eval(
m,
list(),
derived = list(y = 2:6), # y = x + 1
jacobians = list(y = list(x = Matrix::Diagonal(1.0, 5))),
data = the_data,
state = list(x = 1:5)
)
#> [1] -3.736144 -10.970613 -8.356247 3.214468 13.443630