[R] Simulation

gb gb at stat.umu.se
Wed Dec 6 14:13:33 CET 2000


I want to draw a random sample Y_1, ..., Y_n, iid Poisson,
but condition on their sum being equal to  k. Two
suggestions:

1)   lambda <- k / n ## optimal value
     ysum <- -1
     while (ysum != k){
        y <- rpois(n, lambda)
        ysum <- sum(y)
     }
     y

2) Aiming at Multinom(k, 1/n, ..., 1/n):

   y <- as.vector(table(factor(sample(n, k, replace = T), levels = 1:n)))

1) seems to be faster than 2), but my question is whether there is
something even better?  'sample' is fast enough, but how can I do
the rest (factor, table) smarter in 2)?

Thanks for any suggestions!

Göran
-- 
 Göran Broström                      tel: +46 90 786 5223
 professor                           fax: +46 90 786 6614
 Department of Statistics            http://www.stat.umu.se/egna/gb/
 Umeå University
 SE-90187 Umeå, Sweden             e-mail: gb at stat.umu.se

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list