Plotting in R: Probability mass function for a Poisson distribution

26k Views Asked by At

Suppose that I have a Poisson distribution with mean of 6. I would like to plot a probability mass function that includes an overlay of the approximating normal density.

This is what i have tried

plot( dpois( x=0:10, lambda=6 ))

this produces

enter image description here

which is wrong.

How do i go about this.

2

There are 2 best solutions below

1
On

On the graph your $x$ values should start at $0$ not $1$. You should also extend to the right slightly more (there is no upper limit on a Poisson distribution)

For the normal distribution you can produce a suitable density using the curve function. In this case, it is presumably sensible to suppose you want to compare with a $N(\lambda, \lambda)$ distribution which has the same mean and variance as the Poisson distribution. You may also want to extend to the left

You could try something like

plot(0:20, dpois( x=0:20, lambda=6 ), xlim=c(-2,20))
normden <- function(x){dnorm(x, mean=6, sd=sqrt(6))}
curve(normden, from=-4, to=20, add=TRUE, col="red")

looking something like

enter image description here

0
On

Direct plotting of PDFs.

x = 0:20;  pdf = dpois(x, 6)
plot(x, pdf, type="h", lwd=3, col="blue", 
  main="PDF of POIS(6) with Approximating Normal Density")
abline(h=0, col="green2")
curve(dnorm(x, 6, sqrt(6)), lwd=2, col="red", add=T) # 'x' mandatory arg

enter image description here

Use large simulated Poisson sample to make histogram, controlling cutpoints for integer data. Then plot approximating normal density.

y = rpois(10^6, 6);  up=max(y)
hist(y, prob=T, br=(-1:up)+.5, col="skyblue2", xlab="x", 
  main="Simulated Sample from POIS(6) with Normal Approximation")
curve(dnorm(x, mean(y), sd(y)), col="red", lwd=2, add=T) 

enter image description here

How well does $\mathsf{Binom}(n=600, p=.01)$ fit results? Add a couple of lines of code to overlay points. (Maybe change header.)

w=0:36;  pdf=dbinom(w, 600, .01)
points(w, pdf, pch=19, col="darkgreen")

enter image description here