Learn R Programming

posterior (version 1.6.0)

rdo: 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)

Value

An rvar.

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.

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()).

See Also

Other rfun: rfun(), rvar_rng()

Examples

Run this code

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