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 timesdie_rolls <-rmultinom(n =10, size =1, prob =c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6) )# Print the resultsprint(die_rolls)

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 timesprobability <-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 resultprint(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 timescoin_flips <-rmultinom(n =10, size =1, prob =c(0.5, 0.5))# Calculate the probability of getting exactly five heads and five tailsprobability <-dmultinom(x =c(5, 5), size =10, prob =c(0.5, 0.5))# Print the resultprint(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.