Generate Random Draws from a Discrete Set of Labels with Associated Probabilities
rcategorical.Rd
Allows different class probabilities for each person by supplying a matrix with one column for each class and one row for each person.
Arguments
- n
How many draws to make. Passed to the
size
argument of thesample
function ifprobs
is not a matrix.- probs
Either a numeric vector of probabilities which sums to one or a matrix with one column for each desired class and
n
rows. Passed to theprobs
argument of thesample
function if a numeric vector is passed.- labels
A vector of labels to draw from. If
NULL
(default), it simply uses integers starting from 1. Passed to thex
argument of thesample
function ifprobs
is not a matrix.- output
A single character string specifying the output format of the results. Must be either
"numeric"
(default),"character"
or"factor"
. If labels are supplied, the output will be parsed as characters by default.
Details
In case of a simple numeric vector (class probabilities should be the same for all draws), this function is only a wrapper for the sample
function, to make the code more consistent. It uses weighted sampling with replacement. Otherwise, custom code is used which is faster than the standard rmultinom
function.