Re: [R] options in 'rnorm' to set the lower bound of normal distribution to 0 ?

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Thu, 27 Mar 2008 13:02:39 +0000 (GMT)

On Thu, 27 Mar 2008, Tom Cohen wrote:

>
> Dear list,

> I have a dataset containing values obtained from two different
> instruments (x and y). I want to generate 5 samples from normal
> distribution for each instrument based on their means and standard
> deviations. The problem is values from both instruments are
> non-negative, so if using rnorm I would get some negative values. Is
> there any options to determine the lower bound of normal distribution to
> be 0 or can I simulate the samples in different ways to avoid the
> negative values?

Well, that would not be a normal distribution.

If you want a _truncated_ normal distribution it is very easy by inversion. E.g.

trunc_rnorm <- function(n, mean = 0, sd = 1, lb = 0) {

lb <- pnorm(lb, mean, sd)
qnorm(runif(n, lb, 1), mean, sd)
}

but I suggest you may rather want samples from a lognormal.

>
>
> > dat
> id x y
> 75 101 0.134 0.1911315
> 79 102 0.170 0.1610306
> 76 103 0.134 0.1911315
> 84 104 0.170 0.1610306
> 74 105 0.134 0.1911315
> 80 106 0.170 0.1610306
> 77 107 0.134 0.1911315
> 81 108 0.170 0.1610306
> 82 109 0.170 0.1610306
> 78 111 0.170 0.1610306
> 83 112 0.170 0.1610306
> 85 113 0.097 0.2777778
> 2 201 1.032 1.5510434
> 1 202 0.803 1.0631001
> 5 203 1.032 1.5510434
>
> mu<-apply(dat[,-1],2,mean)
> sigma<-apply(dat[,-1],2,sd)
> len<-5
> n<-20
> s1<-vector("list",len)
> set.seed(7)
> for(i in 1:len){
> s1[[i]]<-cbind.data.frame(x=rnorm(n*i,mean=mu[1],sd=sigma[1]),
> y=rnorm(n*i,mean=mu[2],sd=sigma[2]))
> }
>
> Thanks for any help,
> Tom
>
>
> ---------------------------------
> S?? efter k??leken!
>
> [[alternative HTML version deleted]]
>
>

```--
Brian D. Ripley,                  ripley_at_stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help