[R] simulate zero-truncated Poisson distribution
(Ted Harding)
Ted.Harding at nessie.mcc.ac.uk
Sun May 1 22:33:23 CEST 2005
On 01-May-05 Peter Dalgaard wrote:
> (Ted Harding) <Ted.Harding at 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 at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 01-May-05 Time: 21:20:43
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list