rvar_factor {posterior} | R Documentation |
Factor random variables of arbitrary dimension
Description
Random variables backed by factor-like arrays of arbitrary dimension.
Usage
rvar_factor(
x = factor(),
dim = NULL,
dimnames = NULL,
nchains = NULL,
with_chains = FALSE,
...
)
rvar_ordered(
x = ordered(NULL),
dim = NULL,
dimnames = NULL,
nchains = NULL,
with_chains = FALSE,
...
)
Arguments
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 if |
with_chains |
(logical) Does |
... |
Arguments passed on to
|
Details
A subtype of rvar()
that represents a (possibly multidimensional) sample of
a factor or an ordered factor. It is otherwise very similar to the basic rvar()
:
it is backed by a multidimensional array with draws as the first dimension.
The primary difference is that the backing array has class "factor"
(for rvar_factor()
)
or c("ordered", "factor")
(for rvar_ordered()
). If you
pass a factor or ordered factor to rvar()
it will automatically return
an object with the classes "rvar_factor"
or c("rvar_ordered", "rvar_factor")
.
See rvar()
for more details on the internals of the random variable datatype.
Value
An object of class "rvar_factor"
representing a factor
-like random variable.
See Also
as_rvar_factor()
to convert objects to rvar_factor
s. See rdo()
, rfun()
, and
rvar_rng()
for higher-level interfaces for creating rvar
s.
Examples
set.seed(1234)
# To create a "scalar" `rvar_factor`, pass a one-dimensional array or a vector
# whose length (here `4000`) is the desired number of draws:
x <- rvar(sample(c("a","a","a","b","c"), 4000, replace = TRUE))
x
# Create random vectors by adding an additional dimension:
x_array <- array(c(
sample(c("a","a","a","b","c"), 4000, replace = TRUE),
sample(c("a","a","b","c","c"), 4000, replace = TRUE),
sample(c("b","b","b","b","c"), 4000, replace = TRUE),
sample(c("d","d","b","b","c"), 4000, replace = TRUE)
), dim = c(4000, 4))
rvar_factor(x_array)
# You can also create ordered factors
rvar_ordered(x_array)
# arguments of factor() and ordered() are passed down by the constructor
# e.g. we can reorder levels of an ordered factor:
rvar_ordered(x_array, levels = c("d","c","b","a"))
# Unlike base factors, rvar factors can be matrices or arrays:
rvar_factor(x_array, dim = c(2, 2))
# If the input to rvar_factor() is an array with a `"levels"` attribute, it
# will use those as the levels of the factor
y_array <- t(array(rbinom(3000, 1, c(0.1, 0.5, 0.9)) + 1, dim = c(3, 1000)))
rvar(y_array)
# with levels
attr(y_array, "levels") = c("a", "b")
rvar_factor(y_array)