Multinomial Distribution in R

rtip
distribution
Author

Steven P. Sanderson II, MPH

Published

October 31, 2023

Introduction

The multinomial distribution is a probability distribution that describes the probability of obtaining a specific number of counts for k different outcomes, when each outcome has a fixed probability of occurring.

In R, we can use the rmultinom() function to simulate random samples from a multinomial distribution, and the dmultinom() function to calculate the probability of a specific outcome.

Examples

Example 1

Suppose we have a fair die, and we want to simulate rolling the die 10 times. We can use the rmultinom() function to do this as follows:

# Simulate rolling a fair die 10 times
die_rolls <- rmultinom(
  n = 10, size = 1, 
  prob = c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6)
  )

# Print the results
print(die_rolls)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    0    0    0    1    0    0    0    0     0
[2,]    0    1    0    0    0    0    0    0    0     0
[3,]    0    0    0    0    0    0    1    0    0     0
[4,]    0    0    1    0    0    1    0    0    0     0
[5,]    0    0    0    1    0    0    0    1    1     0
[6,]    1    0    0    0    0    0    0    0    0     1

Example 2

Suppose we want to calculate the probability of getting exactly two ones, two threes, two fours, two fives, and two sixes when rolling a fair die 10 times. We can use the dmultinom() function to do this as follows:

# Calculate the probability of getting exactly two ones, two threes, two fours, two fives, and two sixes when rolling a fair die 10 times
probability <- dmultinom(
  x = c(2, 0, 2, 2, 2, 2), 
  size = 10, 
  prob = c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6)
  )

# Print the result
print(probability)
[1] 0.001875429

Try it on your own!

I encourage readers to try using the rmultinom() and dmultinom() functions on their own data. For example, you could simulate rolling a die 100 times and see how often each outcome occurs. Or, you could calculate the probability of getting a certain number of heads and tails when flipping a coin 10 times.

Here is an example of how to use the rmultinom() function to simulate flipping a coin 10 times and calculate the probability of getting exactly five heads and five tails:

# Simulate flipping a coin 10 times
coin_flips <- rmultinom(n = 10, size = 1, prob = c(0.5, 0.5))

# Calculate the probability of getting exactly five heads and five tails
probability <- dmultinom(x = c(5, 5), size = 10, prob = c(0.5, 0.5))

# Print the result
print(probability)
[1] 0.2460938

I hope this blog post has helped you learn how to use the multinomial distribution in R. Please feel free to leave a comment if you have any questions.