Gianni D'Adova
Gianni D'Adova

Reputation: 39

How to draw Poisson density curve in R?

I need to show that the amount of events in Poisson process are distributed by Poisson distribution with parameter lambda * t. Here is the Poisson process generator:

 ppGen <- function(lambda, maxTime){
      taos <- taosGen(lambda, maxTime)
      pp <- NULL
      for(i in 1:maxTime){
        pp[i] <- sum(taos <= i)
      }
      return(pp)
    }

Here I try to replicate the process 1000 times and vectorisee the total occurrences in each realisation:

 d <- ppGen(0.5,100)
    tail(d,n=1)
    reps <- 1000
    x1 <- replicate(reps, tail(ppGen(0.5,100), n=1))
    hist(x1)

Here is the histogram:

enter image description here

Here I am trying to draw a theoretical Poisson density curve with parameter lambda * t:

xfit<-seq(1,100,length=100)
yfit<-dpois(xfit,lambda = 0.5*100)

lines(xfit,yfit)

But the curve doesn't appear anywhere near the histogram. Can anyone suggest on the right way to do this?

Upvotes: 0

Views: 982

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101343

Maybe you can try curve like below

x <- rpois(1000, 0.5 * 100)
dp <- function(x, lbd = 0.5 * 100) dpois(x, lambda = lbd)
curve(dp, 0, 100)
hist(x, freq = FALSE, add = TRUE)

enter image description here

Upvotes: 1

Related Questions