The Poisson distribution in R

This is a training session of how dpois, ppois, rpois and qpois functions can be applied in R:

Sources

For the dpois and ppois function I use the Mike Marin’s 4 minutes tutorial video: https://www.youtube.com/watch?v=778WK1Pf8eI&feature=youtu.be. For the rpois and qpois, I follow the examples from http://www.programmingr.com/

dpois

The dpois function finds values for the probability density function of X, f(x). Let’s run an example where X follows a Poisson distribution with a known rate of \(\lambda\)=8: \[X\sim Poisson(\lambda=8)\]

# P(X=4)
dpois(x=5, lambda = 8)
## [1] 0.09160366
# P(X=0) & P(X=1) &...& P(X=5)
dpois(x=0:5, lambda=8)
## [1] 0.0003354626 0.0026837010 0.0107348041 0.0286261442 0.0572522885
## [6] 0.0916036616
# P(X <= 5)
# This can be done with dpois:
sum( dpois(x=0:5, lambda=8) )
## [1] 0.1912361

and it can be done with the ppois function:

ppois

The ppois function returns probabilities associated with the probability distribution function, F(x)

# P(X <= 5)
ppois(q=5, lambda = 8, lower.tail = T)
## [1] 0.1912361
# P(X >= 13)
ppois(q=13, lambda = 8, lower.tail = F)
## [1] 0.0341807

rpois

To run a random sample in the Poisson distribution we can use ‘rpois’ function. It models the number of expected events occurring within a certain time interval.

# Say that we wish to take a random sample of 11 from a Poisson distribution with a known rate of lambda = 8. 
# Say we have observed a number of 8 cars passing a certain point per minute and that we wish to generate a simulation of the number of cars per minute for the next 11 minutes:
rpois(11,8)
##  [1]  8  9  5  4  3  8  8  6 11 10 11

Our sample shows 8 cars the first minute, 6 cars the second, 8 the third and so forth.

qpois

The qpois function finds quantiles for the Poisson distribution. As such, it is the inverse of the operation performed by ppois. We percentile and it generates the number of events associated with that cumulative probability:

qpois(0.25,7)
## [1] 5

Webshop example

Say we have a webshop and that we wish to estimate the number of sales of a certain product during the next hour. We have observed an average number of sales of this product of 2.3 per hour, \(X\sim Poisson(\lambda=2.3)\).

We wish to see estimate the different probabilities of selling 1; 2; 3 and 4 during the next hour.

# Selling 0, 1, 2, 3 or 4 units during the next hour:
dpois(x=0:4, lambda=2.3)
## [1] 0.1002588 0.2305953 0.2651846 0.2033082 0.1169022

What is the probability of doing between 0 and 3 sales during the next hour?

# Selling between 0 and 3:
ppois(q=3, lambda = 2.3, lower.tail = T)
## [1] 0.7993471
sum( dpois(x=0:3, lambda=2.3) )
## [1] 0.7993471

So, there is approximately 70% probability of selling between 1 and 3 units during the next hour.

View this page on my site: https://dataz4s.com/r-statistical-programming/poisson-distribution-in-r/