Multinomial Distribution in R

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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    0    0    0    0    0     0
[2,]    0    1    0    0    0    0    0    0    0     0
[3,]    1    0    0    0    0    0    0    1    0     0
[4,]    0    0    0    0    0    0    0    0    0     0
[5,]    0    0    0    0    0    1    1    0    1     0
[6,]    0    0    1    1    1    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, 2, 2, 2, 2), 
  size = 10, 
  prob = c(1/6, 1/6, 1/6, 1/6, 1/6)
  )

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

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.

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)