Binomial Distribution in R

Binomial distribution in R is a probability distribution used in statistics. The binomial distribution is a discrete distribution and has only two outcomes i.e. success or failure. All its trials are independent, the probability of success remains the same and the previous outcome does not affect the next outcome. The outcomes from different trials are independent. Binomial distribution helps us to find the individual probabilities as well as cumulative probabilities over a certain range. It is also used in many real-life scenarios such as in determining whether a particular lottery ticket has won or not, whether a drug is able to cure a person or not, it can be used to determine the number of heads or tails in a finite number of tosses, for analyzing the outcome of a die, etc.

Binomial distribution for any random variable x is given by
P(x) = nCx · px (1 − p)nx


Functions for Binomial Distribution

We have four functions for handling binomial distribution in R namely:

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


Following is the description of the parameters used −

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

dbinom() Function

This function is used to find probability at a particular value for a data that follows binomial distribution i.e. it finds:
    P(X = k)

Syntax:
        dbinom(k, n, p)

Example:
# Create a sample of 50 numbers which are incremented by 1.
x <- seq(0,50,by = 1)

# Create the binomial distribution.
y <- dbinom(x,50,0.5)

# Give the chart file a name.
#png(file = "dbinom.png")

# Plot the graph for this sample.
plot(x,y)

# Save the file.
#dev.off()




pbinom() Function

The function pbinom() is used to find the cumulative probability of a data following binomial distribution till a given value ie it findsP(X <= k)

Syntax:
pbinom(k, n, p)

Example:

This function gives the cumulative probability of an event. It is a single value representing the probability.
# Probability of getting 26 or less heads from a 51 tosses of a coin. 
x <- pbinom(26,51,0.5) 
 print(x)

Output:
[1] 0.610116

Example:
pbinom(3, size = 13, prob = 1 / 6)
plot(0:10, pbinom(0:10, size = 10, prob = 1 / 6), type = "l")




qbinom() Function

This function takes the probability value and gives a number whose cumulative value matches the probability value.
# How many heads will have a probability of 0.25 will come out when a coin 
# is tossed 51 times. 
x <- qbinom(0.25,51,1/2) print(x)

When we execute the above code, it produces the following result −
[1] 23

Example:
qbinom(0.8419226, size = 13, prob = 1 / 6)
x <- seq(0, 1, by = 0.1)
y <- qbinom(x, size = 13, prob = 1 / 6)
plot(x, y, type = 'l')
\Output:
[1] 3



rbinom()

This function generates required number of random values of given probability from a given sample.
# Find 8 random values from a sample of 150 with probability of 0.4. 
x <- rbinom(8,150,0.4) 
 print(x)
hist(x)
When we execute the above code, it produces the following result −
[1] 58 61 59 66 55 60 61 67



Comments

Popular posts from this blog

Programming in R - Dr Binu V P

Introduction

R Data Types