Learn R Programming

stats (version 3.3.2)

Multinom: The Multinomial Distribution

Description

Generate multinomially distributed random number vectors and compute multinomial probabilities.

Usage

rmultinom(n, size, prob)
dmultinom(x, size = NULL, prob, log = FALSE)

Arguments

x
vector of length \(K\) of integers in 0:size.
n
number of random vectors to draw.
size
integer, say \(N\), specifying the total number of objects that are put into \(K\) boxes in the typical multinomial experiment. For dmultinom, it defaults to sum(x).
prob
numeric non-negative vector of length \(K\), specifying the probability for the \(K\) classes; is internally normalized to sum 1. Infinite and missing values are not allowed.
log
logical; if TRUE, log probabilities are computed.

Value

For rmultinom(), an integer \(K \times n\) matrix where each column is a random vector generated according to the desired multinomial law, and hence summing to size. Whereas the transposed result would seem more natural at first, the returned matrix is more efficient because of columnwise storage.

Details

If x is a \(K\)-component vector, dmultinom(x, prob) is the probability $$P(X_1=x_1,\ldots,X_K=x_k) = C \times \prod_{j=1}^K \pi_j^{x_j}$$ where \(C\) is the ‘multinomial coefficient’ \(C = N! / (x_1! \cdots x_K!)\) and \(N = \sum_{j=1}^K x_j\). By definition, each component \(X_j\) is binomially distributed as Bin(size, prob[j]) for \(j = 1, \ldots, K\). The rmultinom() algorithm draws binomials \(X_j\) from \(Bin(n_j,P_j)\) sequentially, where \(n_1 = N\) (N := size), \(P_1 = \pi_1\) (\(\pi\) is prob scaled to sum 1), and for \(j \ge 2\), recursively, \(n_j = N - \sum_{k=1}^{j-1} X_k\) and \(P_j = \pi_j / (1 - \sum_{k=1}^{j-1} \pi_k)\).

See Also

Distributions for standard distributions, including dbinom which is a special case conceptually.

Examples

Run this code
rmultinom(10, size = 12, prob = c(0.1,0.2,0.8))

pr <- c(1,3,6,10) # normalization not necessary for generation
rmultinom(10, 20, prob = pr)

## all possible outcomes of Multinom(N = 3, K = 3)
X <- t(as.matrix(expand.grid(0:3, 0:3))); X <- X[, colSums(X) <= 3]
X <- rbind(X, 3:3 - colSums(X)); dimnames(X) <- list(letters[1:3], NULL)
X
round(apply(X, 2, function(x) dmultinom(x, prob = c(1,2,5))), 3)

Run the code above in your browser using DataLab