Re: [R] Simulation using parts of density function

From: Thür Brigitte <>
Date: Wed, 02 May 2007 09:08:40 +0200

Thanks for your code! It is not exactly what I really want - but it is my fault, because my description was wrong...

It is not "sim" but rhater exp(rgamma(...)) that should not exceed 5000000. So I tried to modify your code but it doesn't really work. "sim.test" returns just 1 value and not 999.

My modified code:

sim.test <- NULL
for(i in 1:999)
  remain <- rpois(1,2000)
  x <- remain

       exp(rgamma(1, scale = 0.5, shape = 12))

    remain<-(x - length(sim))
sim.test <- rbind(sim.test, c(value=sum(sim)))

Thanks for any help,

-----Ursprüngliche Nachricht-----
Von: [] Gesendet: Dienstag, 1. Mai 2007 20:18
An: Thür Brigitte
Betreff: RE: [R] Simulation using parts of density function

On 01-May-07 17:03:46, Thür Brigitte wrote:
> Hi
> My simulation with the followin R code works perfectly:
> sim <- replicate(999, sum(exp(rgamma(rpois(1,2000),
> scale = 0.5, shape = 12))))
> But now I do not want to have values in object "sim" exceeding
> 5'000'000, that means that I am just using the beginning of
> densitiy function gamma x < 15.4. Is there a possibility to
> modify my code in an easy way?
> Thanks for any help!
> Regards, Brigitte

A somewhat extreme problem!

The easiest way to modify the code is as below -- certiainly easier than writing a special function to draw random samples from the truncated gamma distribution.

A bit of experimentation shows that, from your code above, about 10% of the results are <= 5000000. So:

  remain <- 999

       sum(exp(rgamma(rpois(1,2000), scale = 0.5, shape = 12)))

    remain<-(999 - length(sim))

Results of a run:

  [1] 0

  [1] 4999696

  [1] 999

It may be on the slow side (though not hugely -- on a quite slow machine the above run was completed in 2min 5sec, while the 999-replicate in your original took 15sec. So about 8 times as long. Most of this, of course, is taken up with the first round.

Hoping this helps,

E-Mail: (Ted Harding) <> Fax-to-email: +44 (0)870 094 0861
Date: 01-May-07                                       Time: 19:18:01
------------------------------ XFMail ------------------------------
*** eSafe_at_SWICA scanned this email for malicious content and found it to be clean *** mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Wed 02 May 2007 - 07:15:53 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 02 May 2007 - 08:31:52 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.