+34 616 71 29 85 carsten@dataz4s.com

The Poisson distribution in R

This is a training session of how calculate probabilities for the Poisson distribution in R with dpois, ppois, rpois and qpois functions.

 

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 =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 12  6  6  9  6  9  6 13 13  5

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, .

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.

 

RPubs

View a better format of this page in my RPubs document: https://rpubs.com/CarstenGrube/Poisson_distribution

 

Learning R programming

I have found these tutorials helpful for this subject>

– Mike Marin’s 4 minutes video: Probabilities in the Poisson Distribution….  

http://www.programmingr.com/

 

 

Carsten Grube

Carsten Grube

Freelance Data Analyst

0 Comments

+34 616 71 29 85

Call me

Spain: Ctra. 404, km 2, 29100 Coín, Malaga

...........

Denmark: c/o Musvitvej 4, 3660 Stenløse

Drop me a line

What are you working on just now? Can I help you, and can you help me? 

About me

Learning statistics. Doing statistics. Freelance since 2005. Dane. Living in Spain. With my Spanish wife and two children. 

What they say

20 years in sales, analysis, journalism and startups. See what my customers and partners say about me.