Draw random samples from a categorical distribution given a matrix of probabilities. rcat is vectorized and written in C++ for speed.

rcat(n, prob)

Arguments

n

Number of random observations to draw.

prob

A matrix of probabilities where rows correspond to observations and columns correspond to categories.

Value

A vector of random samples from the categorical distribution. The length of the sample is determined by n. The numerical arguments other than n are recycled so that the number of samples is equal to n.

Examples

p <- c(.2, .5, .3)
n <- 10000
pmat <- matrix(rep(p, n), nrow = n, ncol = length(p), byrow = TRUE)

# rcat
set.seed(100)
ptm <- proc.time()
samp1 <- rcat(n, pmat)
proc.time() - ptm
#>    user  system elapsed 
#>   0.003   0.000   0.003 
prop.table(table(samp1))
#> samp1
#>      1      2      3 
#> 0.2015 0.5089 0.2896 

# rmultinom from base R 
set.seed(100)
ptm <- proc.time()
samp2 <- t(apply(pmat, 1, rmultinom, n = 1, size = 1))
samp2 <- apply(samp2, 1, function(x) which(x == 1))
proc.time() - ptm
#>    user  system elapsed 
#>   0.067   0.000   0.067 
prop.table(table(samp2))
#> samp2
#>      1      2      3 
#> 0.2015 0.5089 0.2896