dimanche 20 mars 2022

Generate totals of multinomial distribution directly

Let's assume we want to generate n samples from a multinomial distribution from given probabilities p. This works well with sample or rmultinorm. The totals can then be counted with table. Now I wonder if there is a direct way (or another distribution) available to get the result of table directly without generating complete sample vectors.

Here an example:

set.seed(123)
n <- 10000              # sample size
p <- c(0.1, 0.2, 0.7)   # probabilities, sum up to 1.0

## 1) approach with sample
x <- sample(1:3, size = n, prob = p, replace = TRUE)
table(x)
# x
# 1    2    3 
# 945 2007 7048 

## 2) approach with rmultinorm
x <- rmultinom(n, size = 1, prob = p) * 1:3
table(x[x != 0])
# 1    2    3 
# 987 1967 7046 



Aucun commentaire:

Enregistrer un commentaire