From: Thür Brigitte <Brigitte.Thuer_at_swica.ch>

Date: Wed, 02 May 2007 09:08:40 +0200

sim<-c(sim,sim0[sim0<=5000000])

remain<-(x - length(sim))

}

sim<-sim[1:x]}

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

sim<-c(sim,sim0[sim0<=5000000])

remain<-(999 - length(sim))

}

sim<-sim[1:999]

E-Mail: (Ted Harding) <ted.harding_at_nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861

R-help_at_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 and provide commented, minimal, self-contained, reproducible code. Received on Wed 02 May 2007 - 07:15:53 GMT

Date: Wed, 02 May 2007 09:08:40 +0200

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

sim<-c(sim,sim0[sim0<=5000000])

remain<-(x - length(sim))

}

sim<-sim[1:x]}

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

Thanks for any help,

Brigitte

-----Ursprüngliche Nachricht-----

Von: ted.harding_at_nessie.mcc.ac.uk [mailto:ted.harding_at_nessie.mcc.ac.uk]
Gesendet: Dienstag, 1. Mai 2007 20:18

An: Thür Brigitte

Cc: r-help_at_stat.math.ethz.ch

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:

sim<-NULL

remain <- 999

while(remain>0){

sim0<-replicate(10*remain,

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

sim<-c(sim,sim0[sim0<=5000000])

remain<-(999 - length(sim))

}

sim<-sim[1:999]

Results of a run:

sum(sim>5000000)

[1] 0

max(sim)

[1] 4999696

length(sim)

[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,

Ted.

E-Mail: (Ted Harding) <ted.harding_at_nessie.mcc.ac.uk> 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 ***

R-help_at_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 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 https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*