Search code examples
rhistogrampoisson

plot histogram and pdf in same plot for poisson


I have a dataset which i need to analyse with poisson regression. I try to plot the histrogram of the respons in the same plot as the pdf but i don't find how..

i plot my histogram with the following code:

library(lattice) 
tot <- mydata[, 1] 
histogram(tot) 

and get a nice histogram. To add the pdf i've tried some codes lie the one below:

xlines <-seq(min(tot),max(tot),length.out=100)  
lines(x = xlines,y=dpois(xlines,21))

I've tried a few other codes as well but can't find one that works...
Somebody has some suggestions?


Solution

  • You can start working on this code:

    library(lattice) 
    set.seed(1)
    tot <- rpois(100,21)
    xlines <- seq(min(tot),max(tot),by=1)
    histogram(tot, type="density", 
      panel=function(x, ...){
        panel.histogram(x,...)
        panel.lines(x = xlines, y=dpois(xlines,21), lwd=2, col="red")
      }
    ) 
    

    enter image description here