From: Berwin A Turlach <berwin_at_maths.uwa.edu.au>

Date: Thu, 24 Jul 2008 14:47:46 +0800

> > Yes, I know. I mean if I want to generate 100 numbers from

*> > N(0,1)I((0,1),(5,10)). There are two intervals (0,1) and (5,10).
*

*> > Then the function will give 50 numbers in the first interval and 50
*

*> > in the other.
*

ind <- runif(100) < p

finalsample <- samp1

finalsample[!ind] <- samp2[!ind]

R-help_at_r-project.org 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 Thu 24 Jul 2008 - 06:52:30 GMT

Date: Thu, 24 Jul 2008 14:47:46 +0800

G'day all,

On Wed, 23 Jul 2008 20:48:59 -0400

Duncan Murdoch <murdoch_at_stats.uwo.ca> wrote:

> On 23/07/2008 8:17 PM, cindy Guo wrote:

> > Yes, I know. I mean if I want to generate 100 numbers from

I can think of two strategies:

- Use rtnorm from the msm *package* to sample from the normal distribution truncated to the interval from your smallest lower bound and your largest upper bound; and then use rejection sampling.

I.e. for your original example N(0,1)I((0,1),(2,4)) sample from a N(0,1)I(0,4) distribution and reject all observations that are between 1 and 2, sample sufficiently many points until you have kept the required number. But this could be rather inefficient for your second example N(0,1)I((0,1),(5,10)).

2) If I am not mistaken, truncating the normal distribution to more than one interval essentially creates a mixture distribution where the components of the mixture are normal distributions truncated to a single interval. The weights of the mixture are given by the relative probability with which an observation from a normal distribution falls into each of the intervals. Thus, an alternative strategy, exemplified using your second example, would be (code untested):

samp1 <- rtnorm(100,mean=0,sd=1, lower=0,upper=1)
samp2 <- rtnorm(100,mean=0,sd=1, lower=5,upper=10)
p1 <- pnorm(1)-pnorm(0)

p2 <- pnorm(10)-pnorm(5) ## Take heed about Duncan's remark on

## evaluating such quantitiesp <- p1/(p1+p2)

ind <- runif(100) < p

finalsample <- samp1

finalsample[!ind] <- samp2[!ind]

**HTH.
**
Cheers,

Berwin

- Full address =============================
Berwin A Turlach Tel.: +65 6516 4416 (secr)
Dept of Statistics and Applied Probability +65 6516 6650 (self)
Faculty of Science FAX : +65 6872 3919
National University of Singapore
6 Science Drive 2, Blk S16, Level 7 e-mail: statba_at_nus.edu.sg
Singapore 117546 http://www.stat.nus.edu.sg/~statba

R-help_at_r-project.org 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 Thu 24 Jul 2008 - 06:52:30 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 Thu 24 Jul 2008 - 07:32:09 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.
*