R Tutorial R Charts & Graphs R Statistics
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

R - Poisson Distribution



Poisson Distribution is a discrete probability distribution and it expresses the probability of a given number of events occurring in a fixed interval of time or space if these events occur with a known constant mean rate and independently of the time since the last event.

The probability mass function (pmf) of poisson distribution is defined as:

Poisson Distribution

Where, k is the number of occurrences (k=0,1,2...), λ is average number of events.

An poisson distribution has mean λ and variance λ.

The cumulative distribution function (cdf) evaluated at k, is the probability that the random variable (X) will take a value less than or equal to k. The cdf of poisson distribution is defined as:

Poisson Distribution

Where, [k] is the greatest integer less than or equal to k.

In R, there are four functions which can be used to generate poisson distribution.

Syntax

dpois(x, lambda)
ppois(q, lambda)
qpois(p, lambda)
rpois(n, lambda)

Parameters

x Required. Specify a vector of numbers.
q Required. Specify a vector of numbers.
p Required. Specify a vector of probabilities.
n Required. Specify number of observation (sample size).
lambda Required. Specify a vector of (non-negative) means.

dpois()

The dpois() function measures probability mass function (pmf) of the distribution.

#creating a sequence of values between
#0 to 20 with a difference of 1
x = seq(0, 20, by=1)
   
y = dpois(x, 10)
   
#naming the file
png(file = "poisson.png")

#plotting the graph
plot(x, y, col="blue")

#saving the file
dev.off()

The output of the above code will be:

Poisson Distribution

ppois()

The ppois() function returns cumulative distribution function (cdf) of the distribution.

#creating a sequence of values between
#0 to 20 with a difference of 0.1
x = seq(0, 20, by=0.1)
   
y = ppois(x, 10)
   
#naming the file
png(file = "poisson.png")

#plotting the graph
plot(x, y, col="blue")

#saving the file
dev.off()

The output of the above code will be:

Poisson Distribution

qpois()

The qpois() function takes the probability value and returns cumulative value corresponding to probability value of the distribution.

#creating a sequence of probability from
#0 to 1 with a difference of 0.01
x = seq(0, 1, by=0.01)
   
y = qpois(x, 10)
   
#naming the file
png(file = "poisson.png")

#plotting the graph
plot(x, y, col="blue")

#saving the file
dev.off()

The output of the above code will be:

Poisson Distribution

rpois()

The rpois() function generates a vector containing specified number of random values from the given poisson distribution. In the example below, a histogram is plotted to visualize the result.

#fixing the seed to maintain the
#reproducibility of the result
set.seed(10)
x = 10000

#creating a vector containing 10000 random
#values from the given poisson distribution
y = rpois(x, 10)
   
#naming the file
png(file = "poisson.png")

#plotting the graph
hist(y, col="blue")

#saving the file
dev.off()

The output of the above code will be:

Poisson Distribution

5