rcategorical {simDAG}R Documentation

Generate Random Draws from a Discrete Set of Labels with Associated Probabilities

Description

Allows different class probabilities for each person by supplying a matrix with one column for each class and one row for each person.

Usage

rcategorical(n, probs, labels=NULL, coerce2factor=FALSE)

Arguments

n

How many draws to make. Passed to the size argument of the sample function if probs 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 the probs argument of the sample 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 the x argument of the sample function if probs is not a matrix.

coerce2factor

A single logical value specifying whether to return the drawn events as a factor or not.

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.

Value

Returns a numeric vector (or factor vector if coerce2factor=TRUE) of length n.

Author(s)

Robin Denz

Examples

library(simDAG)

rcategorical(n=5, labels=c("A", "B", "C"), probs=c(0.1, 0.2, 0.7))

rcategorical(n=2, probs=matrix(c(0.1, 0.2, 0.5, 0.7, 0.4, 0.1), nrow=2))

[Package simDAG version 0.1.1 Index]