dbinom() Function In R: Binomial Distribution

This tutorial will discuss using the dbinom() function of the binomial distribution in the R programming language together. A probability distribution called binomial distribution is utilized in statistics. Success or failure are the only two possible outcomes in the discrete binomial.

What is the dbinom() function in R?

The dbinom() function in the R programming language is often used in statistics. This function returns the value of the binomial distribution that probability density function (pdf) given that x is a random value, size is the number of trials, and prop is the probability of success on each trial.

Syntax:

dbinom(x, s, p, log = FALSE)

Parameters:

• x: The value.
• s: The number of trials.
• p: The probability of success of each trial.
• log: If TRUE, the probabilities are given as logarithms.

How to use the dbinom() function in R?

We will learn how to use this function through examples that will help you understand it better.

Example 1

Here, we are using the binomial distribution example to find the probability that a person makes 85% of his throw attempts. If he shoots 30 throws, what is the probability that the person makes precisely 20 of those attempts? Here we are simply using the dbinom() function and passing the given statement properties as its parameters, and then getting the result. See the code example below:

# Calculate the binomial probability
res <- dbinom(x = 20, size = 30, prob = 0.85)

# Print result binomial probability
res

Output

[1] 0.006715271

Example 2

In this example, we are just using the dbinom() function to determine the chance of getting heads exactly 30 times if the coin is tossed 40 times properly. The function with prob parameter is set to 0.5 because the coin is tossed somewhat.

# Calculate probability
res <- dbinom(x = 30, size = 40, prob = 0.5)

# Print result using the dbinom() function
res

Output

[1] 0.0007709428

Example 3

The binomial probability function can be demonstrated by passing the result of the dbinom() function along with a value as the first parameter and specifying type = “h” in the R language.

First, we will have an x-axis value grid with a vector from 1 to 100, size is 100, and probability is 0.25, 0.35, 0.5, 0.6.

# Data initialization
data1 <- dbinom(x = c(1:80), size = 100, prob = 0.25)
data2 <- dbinom(x = c(1:80), size = 100, prob = 0.35)
data3 <- dbinom(x = c(1:80), size = 100, prob = 0.5)
data4 <- dbinom(x = c(1:80), size = 100, prob = 0.6)

Now, we will plot with this data as follows:

# Data initialization
data1 <- dbinom(x = c(1:100), size = 100, prob = 0.25)
data2 <- dbinom(x = c(1:100), size = 100, prob = 0.35)
data3 <- dbinom(x = c(1:100), size = 100, prob = 0.5)
data4 <- dbinom(x = c(1:100), size = 100, prob = 0.6)

# Plot size = 100, prob = 0.25
plot(data1,
lwd = 2, type = "h",
xlab = "Number of successes", ylab = "P(X = x)",
main = "Binomial probability "
)

# Plot size = 100, prob = 0.35
lines(data2,
type = "h",
lwd = 2, col = rgb(1, 0, 0, 0.7)
)

# Plot size = 100, prob = 0.5
lines(data3,
type = "h",
lwd = 2, col = rgb(0, 1, 0, 0.8)
)

# Plot size = 100, prob = 0.6
lines(data4,
type = "h",
lwd = 2, col = rgb(0, 0, 1, 0.9)
)

legend("topright",
title = "size prob", title.adj = 0.95,
legend = c("100 0.25", "100 0.35", "100 0.5", "100 0.6"),
lty = 1, col = 1:3, lwd = 2, box.lty = 0
)

Output