rvar_rng {posterior} | R Documentation |
Create random variables from existing random number generators
Description
Specialized alternative to rdo()
or rfun()
for creating rvar
s from
existing random-number generator functions (such as rnorm()
, rbinom()
, etc).
Usage
rvar_rng(.f, n, ..., ndraws = NULL)
Arguments
.f |
(function) A function (or string naming a function) representing a
random-number generating function that follows the pattern of base random
number generators (like
|
n |
(positive integer) The length of the output |
... |
Arguments passed to |
ndraws |
(positive integer) The number of draws used to construct the
returned random variable if no |
Details
This function unwraps the arrays underlying the input rvar
s in
...
and then passes them to .f
, relying on the vectorization of .f
to evaluate it across draws from the input rvar
s. This is why the arguments
of .f
must be vectorized. It asks for n
times the number of draws
in the input rvar
s (or ndraws
if none are given) draws from the
random number generator .f
, then reshapes the output from .f
into an
rvar
with length n
.
rvar_rng()
is a fast alternative to rdo()
or rfun()
, but you must
ensure that .f
satisfies the preconditions described above for the result
to be correct. Most base random number generators satisfy these conditions.
It is advisable to test against rdo()
or rfun()
(which should be correct,
but slower) if you are uncertain.
Value
A single-dimensional rvar
of length n
.
See Also
Examples
mu <- rvar_rng(rnorm, 10, mean = 1:10, sd = 1)
sigma <- rvar_rng(rgamma, 1, shape = 1, rate = 1)
x <- rvar_rng(rnorm, 10, mu, sigma)
x