Random variables backed by factor-like arrays of arbitrary dimension.
Arguments
- x
(multiple options) The object to convert to an
rvar:A vector of draws from a distribution.
An array where the first dimension represents draws from a distribution. The resulting
rvarwill have dimensiondim(x)[-1]; that is, everything except the first dimension is used for the shape of the variable, and the first dimension is used to index draws from the distribution (see Examples). Optionally, ifwith_chains == TRUE, the first dimension indexes the iteration and the second dimension indexes the chain (seewith_chains).An
rvar.
- dim
(integer vector) One or more integers giving the maximal indices in each dimension to override the dimensions of the
rvarto be created (seedim()). IfNULL(the default),dimis determined by the input. NOTE: This argument controls the dimensions of thervar, not the underlying array, so you cannot change the number of draws using this argument.- dimnames
(list) Character vectors giving the names in each dimension to override the names of the dimensions of the
rvarto be created (seedimnames()). IfNULL(the default), this is determined by the input. NOTE: This argument controls the names of the dimensions of thervar, not the underlying array.- nchains
(positive integer) The number of chains. The if
NULL(the default),1is used unlessxis already anrvar, in which case the number of chains it has is used.- with_chains
(logical) Does
xinclude a dimension for chains? IfFALSE(the default), chains are not included, the first dimension of the input array should index draws, and thenchainsargument can be used to determine the number of chains. IfTRUE, thenchainsargument is ignored and the second dimension ofxis used to index chains. Internally, the array will be converted to a format without the chain index. Ignored whenxis already anrvar.- ...
Arguments passed on to
base::factorlevelsan optional vector of the unique values (as character strings) that
xmight have taken. The default is the unique set of values taken byas.character(x), sorted into increasing order ofx. Note that this set can be specified as smaller thansort(unique(x)).labelseither an optional character vector of labels for the levels (in the same order as
levelsafter removing those inexclude), or a character string of length 1. Duplicated values inlabelscan be used to map different values ofxto the same factor level.excludea vector of values to be excluded when forming the set of levels. This may be factor with the same level set as
xor should be acharacter.orderedlogical flag to determine if the levels should be regarded as ordered (in the order given).
nmaxan upper bound on the number of levels; see ‘Details’.
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.
See also
as_rvar_factor() to convert objects to rvar_factors. See rdo(), rfun(), and
rvar_rng() for higher-level interfaces for creating rvars.
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
#> rvar_factor<4000>[1] mode <entropy>:
#> [1] a <0.87>
#> 3 levels: a b c
# 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)
#> rvar_factor<4000>[4] mode <entropy>:
#> [1] a <0.68> c <0.76> b <0.36> b <0.76>
#> 4 levels: a b c d
# You can also create ordered factors
rvar_ordered(x_array)
#> rvar_ordered<4000>[4] mode <dissent>:
#> [1] a <0.41> c <0.47> b <0.17> b <0.47>
#> 4 levels: a < b < c < d
# 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"))
#> rvar_ordered<4000>[4] mode <dissent>:
#> [1] a <0.41> c <0.47> b <0.17> b <0.47>
#> 4 levels: d < c < b < a
# Unlike base factors, rvar factors can be matrices or arrays:
rvar_factor(x_array, dim = c(2, 2))
#> rvar_factor<4000>[2,2] mode <entropy>:
#> [,1] [,2]
#> [1,] a <0.68> b <0.36>
#> [2,] c <0.76> b <0.76>
#> 4 levels: a b c d
# 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)
#> rvar<1000>[3] mean ± sd:
#> [1] 1.1 ± 0.30 1.5 ± 0.50 1.9 ± 0.29
# with levels
attr(y_array, "levels") = c("a", "b")
rvar_factor(y_array)
#> rvar_factor<1000>[3] mode <entropy>:
#> [1] a <0.46> a <1.00> b <0.45>
#> 2 levels: a b