Execute (nearly) arbitrary R expressions that may include rvar
s,
producing a new rvar
.
rdo(expr, dim = NULL, ndraws = NULL)
An rvar
.
(expression) A bare expression that can (optionally) contain
rvar
s. The expression supports quasiquotation.
(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.
(positive integer) The number of draws used to construct new
random variables if no rvar
s are supplied in expr
. If NULL
,
getOption("posterior.rvar_ndraws")
is used (default 4000). If expr
contains rvar
s, the number of draws in the provided rvar
s is used
instead of the value of this argument.
This function evaluates expr
possibly multiple times, once for each draw of
the rvar
s it contains, then returns a new rvar
representing the output of those
expressions. To identify rvar
s, rdo()
searches the calling environment for any variables
named in expr
for which is_rvar()
evaluates to TRUE
. If expr
contains no rvar
s,
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 rvar
s
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 rvar
s (which should be fast),
using rvar_rng()
, and/or using operations directly on the arrays that back the rvar
s
(via draws_of()
).
Other rfun:
rfun()
,
rvar_rng()
mu <- rdo(rnorm(10, mean = 1:10, sd = 1))
sigma <- rdo(rgamma(1, shape = 1, rate = 1))
x <- rdo(rnorm(10, mu, sigma))
x
Run the code above in your browser using DataLab