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.
Returns
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 <-10000pmat <- matrix(rep(p, n), nrow = n, ncol = length(p), byrow =TRUE)# rcatset.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))