[R] Generating log transformed random numbers

David Scott d.scott at auckland.ac.nz
Wed Nov 14 14:15:11 CET 2007


If you are after the lognormal distribution, I would recommend you use the 
lognormal functions from R itself. Check them out with

?dlnorm

David Scott


On Wed, 14 Nov 2007, Scionforbai wrote:

> If what you want is a lognormal distribution of n values you can use
> the following transformations:
>
> lognorm1 <- M*exp((rnorm(n)*sigma)-sigma^2/2.)
>
> which gives a lognormal distribution such that:
> mean(lognorm1)=M ;
> var(lognorm1)=M^2*(exp(sigma^2)-1);
> Changing the sigma (standard deviation) you always obtain the same
> arithmetic mean.
>
> Or, alternatively,
>
> lognorm2 <- exp(m + sigma * rnorm(n))
> such that:
> exp(mean(log(lognorm2))=exp(m) [geometric mean]
> mean(lognorm2)=exp(m + sigma^2/2);
> var(lognorm2)=exp(2*m + sigma^2)*(exp(sigma^2/2)-1)
> In this case, for different sigma values is the geometric mean to stay
> constant, not the arithmetic.
>
> Did it answer your question?
>
> ______________________________________________
> 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.
>

_________________________________________________________________
David Scott	Department of Statistics, Tamaki Campus
 		The University of Auckland, PB 92019
 		Auckland 1142,    NEW ZEALAND
Phone: +64 9 373 7599 ext 86830		Fax: +64 9 373 7000
Email:	d.scott at auckland.ac.nz

Graduate Officer, Department of Statistics
Director of Consulting, Department of Statistics



More information about the R-help mailing list