Learn R Programming

posterior (version 1.6.0)

draws_of: Get/set array of draws underlying a random variable

Description

Gets/sets the array-representation that backs an rvar. Should be used rarely.

Usage

draws_of(x, with_chains = FALSE)

draws_of(x, with_chains = FALSE) <- value

Value

If with_chains = FALSE, an array with dimensions c(ndraws(x), dim(x)).

If with_chains = TRUE, an array with dimensions c(niterations(x), nchains(x), dim(x)).

Arguments

x

(rvar) An rvar object.

with_chains

(logical) Should the array of draws include a dimension for chains? If FALSE (the default), chains are not included and the array has dimension c(ndraws(x), dim(x)). If TRUE, chains are included and the array has dimension c(niterations(x), nchains(x), dim(x)).

value

(array) An array of values to use as the backing array of x.

Details

While rvars implement fast versions of basic math operations (including matrix multiplication), sometimes you may need to bypass the rvar abstraction to do what you need to do more efficiently. draws_of() allows you to get / set the underlying array of draws in order to do that.

rvars represent draws internally using arrays of arbitrary dimension, which is returned by draws_of(x) and can be set using draws_of(x) <- value. The first dimension of these arrays is the index of the draws. If with_chains = TRUE, then the dimensions of the returned array are modified so that the first dimension is the index of the iterations and the second dimension is the index of the chains.

Examples

Run this code

x <- rvar(1:10, nchains = 2)
x

# draws_of() without arguments will return the array of draws without
# chain information (first dimension is draw)
draws_of(x)

# draws_of() with with_chains = TRUE will reshape the returned array to
# include chain information in the second dimension
draws_of(x, with_chains = TRUE)

# you can also set draws using draws_of(). When with_chains = FALSE the
# existing chain information will be retained ...
draws_of(x) <- 2:11
x

# when with_chains = TRUE the chain information will be set by the
# second dimension of the assigned array
draws_of(x, with_chains = TRUE) <- array(2:11, dim = c(2,5))
x

Run the code above in your browser using DataLab