Generates a random factor (i.e. a categorical random variable), given the probabilities of each category and their corresponding labels.
rvcat(n = 1, prob, levels = NULL)
integer, number of random variables to generate
vector of probabilities of successes of each trial (may be constant or an rv object)
(character) labels for the categories
A random factor of length length(prob)
.
The length of prob
determines the number of bins.
The vector prob
will be normalized to have sum 1.
Kerman, J. and Gelman, A. (2007). Manipulating and Summarizing Posterior Simulations Using Random Variable Objects. Statistics and Computing 17:3, 235-244.
See also vignette("rv")
.
rvcat(1, prob=c(0.5, 0.3, 0.2)) # default levels: 1, 2, 3
#> 1 2 3 sims
#> [1] 0.47 0.315 0.215 200
rvcat(1, prob=c(5, 3, 2)) # same as above
#> 1 2 3 sims
#> [1] 0.475 0.33 0.195 200
p <- rvdirichlet(1, alpha=c(0.7, 0.3)) # prior probabilities
rvcat(1, prob=p, levels=c("Group 1", "Group 2"))
#> Group 1 Group 2 sims
#> [1] 0.71 0.29 200