Re: [R] simulate zero-truncated Poisson distribution

From: Ted Harding <Ted.Harding_at_nessie.mcc.ac.uk>
Date: Mon 02 May 2005 - 06:33:23 EST


On 01-May-05 Peter Dalgaard wrote:

> (Ted Harding) <Ted.Harding@nessie.mcc.ac.uk> writes:

>> Something like the following code will do the job vectorially:
>>
>> n<-1000 # desired size of sample
>> T<-3.5 # pre-truncation mean of Poisson
>> U<-runif(n) # the uniform sample
>> t = -log(1 - U*(1 - exp(-T))) # the "first" event-times
>> T1<-(T - t) # the set of (T-t)
>>
>> X <- rpois(n,T1)+1 # the final truncated Poisson sample
>>
>> The expected value of your truncated distribution is of course
>> related to the mean of the pre-truncated Poisson by
>>
>> E(X) = T/(1 - exp(-T))
> 
> There must be an easier way... Anything wrong with
> 
>  rtpois <- function(N, lambda)
>      qpois(runif(N, dpois(0, lambda), 1), lambda)
> 
>  rtpois(100,5)
> 
> ?

Well, that's neat! (When I saw that, I had to switch in backup-brain to think clearly about qpois, but there you are ... )

Best wishes,
Ted.



E-Mail: (Ted Harding) <Ted.Harding@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861
Date: 01-May-05                                       Time: 21:20:43
------------------------------ XFMail ------------------------------

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Mon May 02 06:51:19 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:31 EST