[R] dpois().......bizarre warning messages

Ben Bolker bbolker at gmail.com
Sun Oct 17 20:45:43 CEST 2010


Federico Bonofiglio <bonoricus <at> gmail.com> writes:

> consider the following script
> 
> m<-4.95
> obs<-rpois(36,m) # i generate 36 realization from a poisson(m)
> 
> hist(obs,freq=F)
> curve(dpois(x,m),add=T,col="red") #i wish to overlay on the histogram the
> theorical poisson density function
> 
> errors are returned saing the x vector doesn't contain integers....
> 
> really bizarre i can't give explanation (R version 2.11.1, no source codes)

  I don't know about 'really bizarre'.
By default curve() evaluates the expression it is given at 101 equally
spaced points between 'from' and 'to' (see ?curve).  In general these
points won't be integers, so dpois() will quite reasonably complain ...

  curve(dpois(x,m),add=TRUE,col="red",from=0,to=9, n=10) would be
reasonable.
  
  However, you will then run into another problem, which is that
it's a bit tricky to get histograms to bin discrete data correctly.
I would instead suggest something like

  plot(table(obs)/length(obs))
  curve(dpois(x,m),add=TRUE,col="red",from=0,to=9,n=10,type="p")

  Getting in the habit of using TRUE and FALSE rather than T and F
will save you some grief at some point in the future ...



More information about the R-help mailing list