How to Perform Binomial Distribution in R

Sheeraz Gul Feb 15, 2024
  1. Binomial Distribution in R
  2. Use the dbinom() Method to Perform Binomial Distribution in R
  3. Use the pbinom() Method to Perform Binomial Distribution in R
  4. Use the qbinom() Method to Perform Binomial Distribution in R
  5. Use the rbinom() Method to Perform Binomial Distribution in R
How to Perform Binomial Distribution in R

The binomial distribution, also known as the discrete probability distribution, is used to find the probability of an event’s success. This tutorial demonstrates how to perform binomial distribution in R.

Binomial Distribution in R

The binomial distribution can find the probability of success of an event with only two possible outcomes throughout the series of experiments. For example, tossing a coin can have only two possible outcomes: heads or tails.

The probability of finding three heads while tossing the coin 10 times can be approximated in the binomial distribution.

The binomial distribution functions in R as described below:

dbinom(x, size, prob)
pbinom(x, size, prob)
qbinom(p, size, prob)
rbinom(n, size, prob)

Where:

  • x is a vector of the numbers.
  • p is a vector of the probabilities.
  • n is the number of observations.
  • size is the number of trials.
  • prob is the probability of success for every trial.

Let’s try each function through the examples below.

Use the dbinom() Method to Perform Binomial Distribution in R

This method can calculate the probability density distribution at each point.

Example:

# sample of 100 numbers which are incremented by 1.
x <- seq(0,100,by = 1)

# binomial distribution.
y <- dbinom(x,70,0.7)

# Plot the graph
plot(x,y)

Output:

R Binomial Distribution Using dbinom()

Use the pbinom() Method to Perform Binomial Distribution in R

This method can calculate the probability of an event. It is only a single value that represents the probability.

Example:

# Probability of getting 31 or fewer heads from a 65 tosses
a <- pbinom(31,65,0.7)

print(a)

Output:

[1] 0.000141326

Use the qbinom() Method to Perform Binomial Distribution in R

This method takes a probability value and outputs a number whose cumulative value matches the probability value.

Example:

# the number heads will have a probability of 0.5 will come out when the coin is tossed 65 times
a <- qbinom(0.5,65,1/2)

print(a)

Output:

[1] 33

Use the rbinom() Method to Perform Binomial Distribution in R

This method generates a required number of random values with a given probability and sample.

Example:

# 10 random values from a sample of 200 with a probability of 0.7.
a <- rbinom(10,200,.7)

print(a)

The code above will output 10 random values.

Output:

[1] 136 140 142 143 136 140 139 136 142 146
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - R Distribution