Random variables backed by arrays of arbitrary dimension
rvar( x = double(), dim = NULL, dimnames = NULL, nchains = 1L, with_chains = FALSE )
x  (multiple options) The object to convert to an


dim  (integer vector) One or more integers giving the maximal indices
in each dimension to override the dimensions of the 
dimnames  (list) Character vectors giving the names in each dimension
to override the names of the dimensions of the 
nchains  (positive integer) The number of chains. The default is 
with_chains  (logical) Does 
An object of class "rvar"
representing a random variable.
The "rvar"
class internally represents random variables as arrays of arbitrary
dimension, where the first dimension is used to index draws from the distribution.
Most mathematical operators and functions are supported, including efficient matrix
multiplication and vector and arraystyle indexing. The intent is that an rvar
works as closely as possible to how a base vector/matrix/array does, with a few
differences:
The default behavior when subsetting is not to drop extra dimensions (i.e.
the default drop
argument for [
is FALSE
, not TRUE
).
Rather than base Rstyle recycling, rvar
s use a limited form of broadcasting:
if an operation is being performed on two vectors with different size of the same
dimension, the smaller vector will be recycled up to the size of the larger one
along that dimension so long as it has size 1.
For functions that expect base numeric arrays and for which rvar
s cannot be
used directly as arguments, you can use rfun()
or rdo()
to translate your
code into code that executes across draws from one or more random variables
and returns a random variable as output. Typically rdo()
offers the most
straightforward translation.
As rfun()
and rdo()
incur some performance cost, you can also operate directly
on the underlying array using the draws_of()
function. To reuse existing
random number generator functions to efficiently create rvar
s, use rvar_rng()
.
as_rvar()
to convert objects to rvar
s. See rdo()
, rfun()
, and
rvar_rng()
for higherlevel interfaces for creating rvar
s.
set.seed(1234) # To create a "scalar" `rvar`, pass a onedimensional array or a vector # whose length (here `4000`) is the desired number of draws: x < rvar(rnorm(4000, mean = 1, sd = 1)) x#> rvar<4000>[1] mean ± sd: #> [1] 1 ± 1# Create random vectors by adding an additional dimension: n < 4 # length of output vector x < rvar(array(rnorm(4000 * n, mean = rep(1:n, each = 4000), sd = 1), dim = c(4000, n))) x#> rvar<4000>[4] mean ± sd: #> [1] 1 ± 0.99 2 ± 0.99 3 ± 1.00 4 ± 1.02# Create a random matrix: rows < 4 cols < 3 x < rvar(array(rnorm(4000 * rows * cols, mean = 1, sd = 1), dim = c(4000, rows, cols))) x#> rvar<4000>[4,3] mean ± sd: #> [,1] [,2] [,3] #> [1,] 1.00 ± 0.98 1.00 ± 1.00 0.97 ± 1.00 #> [2,] 1.00 ± 1.01 1.01 ± 1.02 0.99 ± 0.99 #> [3,] 1.02 ± 1.01 0.99 ± 1.00 1.00 ± 0.99 #> [4,] 1.01 ± 1.01 1.02 ± 1.00 1.00 ± 1.01# If the input sample comes from multiple chains, we can indicate that using the # nchains argument (here, 1000 draws each from 4 chains): x < rvar(rnorm(4000, mean = 1, sd = 1), nchains = 4) x#> rvar<1000,4>[1] mean ± sd: #> [1] 0.97 ± 1# Or if the input sample has chain information as its second dimension, we can # use with_chains to create the rvar x < rvar(array(rnorm(4000, mean = 1, sd = 1), dim = c(1000, 4)), with_chains = TRUE) x#> rvar<1000,4>[1] mean ± sd: #> [1] 1 ± 1