rcat {hesim} | R Documentation |
Random generation for categorical distribution
Description
Draw random samples from a categorical distribution given a matrix of probabilities.
rcat
is vectorized and written in C++ for speed.
Usage
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
prop.table(table(samp1))
# 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
prop.table(table(samp2))
[Package hesim version 0.5.4 Index]