| rdo {posterior} | R Documentation |
Execute expressions of random variables
Description
Execute (nearly) arbitrary R expressions that may include rvars,
producing a new rvar.
Usage
rdo(expr, dim = NULL, ndraws = NULL)
Arguments
expr |
(expression) A bare expression that can (optionally) contain
|
dim |
(integer vector) One or more integers giving the maximal indices
in each dimension to override the dimensions of the |
ndraws |
(positive integer) The number of draws used to construct new
random variables if no |
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()).
Value
An rvar.
See Also
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