[R] Generating log transformed random numbers

Scionforbai scionforbai at gmail.com
Wed Nov 14 13:32:07 CET 2007


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?



More information about the R-help mailing list