Learn R Programming

rv (version 0.949)

rvcat: Generate Categorical Random Variables

Description

Generates a random factor (i.e. a categorical random variable), given the probabilities of each category and their corresponding labels.

Usage

rvcat(n=1, prob, levels=NULL)

Arguments

n
integer, number of random variables to generate
prob
vector of probabilities of successes of each trial (may be constant or an rv object)
levels
(character) labels for the categories

Value

  • A random factor of length length(prob).

Details

The length of prob determines the number of bins.

The vector prob will be normalized to have sum 1.

References

Kerman, Jouni and Gelman, Andrew. Manipulating and Summarizing Posterior Simulations Using Random Variable Objects. Technical report, Columbia University, New York.

See Also

rvfactor

Examples

Run this code
rvcat(1, prob=c(0.5, 0.3, 0.2)) # default levels: 1, 2, 3
  rvcat(1, prob=c(5, 3, 2)) # same as above
  p <- rvdirichlet(1, alpha=c(0.7, 0.3)) # prior probabilities
  rvcat(1, prob=p, levels=c("Group 1", "Group 2"))

Run the code above in your browser using DataLab