Execute (nearly) arbitrary R expressions that may include rvars, producing a new rvar.

rdo(expr, dim = NULL, ndraws = NULL)

## Arguments

expr

(expression) A bare expression that can (optionally) contain rvars. The expression supports quasiquotation.

dim

(integer vector) One or more integers giving the maximal indices in each dimension to override the dimensions of the rvar to be created (see dim()). If NULL (the default), dim is determined by the input. NOTE: This argument controls the dimensions of the rvar, not the underlying array, so you cannot change the number of draws using this argument.

ndraws

(positive integer) The number of draws used to construct new random variables if no rvars are supplied in expr. If NULL, getOption("posterior.rvar_ndraws") is used (default 4000). If expr contains rvars, the number of draws in the provided rvars is used instead of the value of this argument.

## Value

An rvar.

## Details

This function evaluates expr possibly multiple times, once for each draw of the rvars it contains, then returns a new rvar representing the output of those expressions. To identify rvars, rdo() searches the calling environment for any variables named in expr for which is_rvar() evaluates to TRUE. If expr contains no rvars, then it will be executed ndraws times and an rvar with that many draws returned.

rdo() is not necessarily fast (in fact in some cases it may be very slow), but it has the advantage of allowing a nearly arbitrary R expression to be executed against rvars simply by wrapping it with rdo( ... ). This makes it especially useful as a prototyping tool. If you create code with rdo() and it is unacceptably slow for your application, consider rewriting it using math operations directly on rvars (which should be fast), using rvar_rng(), and/or using operations directly on the arrays that back the rvars (via draws_of()).

Other rfun: rfun(), rvar_rng()

## Examples


mu <- rdo(rnorm(10, mean = 1:10, sd = 1))
sigma <- rdo(rgamma(1, shape = 1, rate = 1))
x <- rdo(rnorm(10, mu, sigma))
x
#> rvar<4000>[10] mean ± sd:
#>  [1]  1.0 ± 1.7   1.9 ± 1.7   3.0 ± 1.7   4.0 ± 1.6   5.0 ± 1.7   6.0 ± 1.7
#>  [7]  7.0 ± 1.7   8.1 ± 1.7   9.0 ± 1.7  10.0 ± 1.8