[R] Is my understanding of rlnorm correct?

Berwin A Turlach berwin at maths.uwa.edu.au
Sun May 4 07:11:59 CEST 2008


G'day Phil,

On Sun, 4 May 2008 14:05:09 +1000
phil colbourn <philcolbourn at gmail.com> wrote:

> rlnorm takes two 'shaping' parameters: meanlog and sdlog.
> 
> meanlog would appear from the documentation to be the log of the
> mean. eg if the desired mean is 1 then meanlog=0.

These to parameters are the mean and the sd on the log scale of the
variate, i.e. if you take the logarithm of the produced numbers then
those values will have the given mean and sd.  

If X has an N(mu, sd^2) distribution, then Y=exp(X) has a log-normal
distribution with parameters mu and sd.  

R> set.seed(1)
R> y <- rlnorm(10000, mean=3, sd=2)
R> summary(log(y))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 -4.343   1.653   2.968   2.987   4.355  10.620 
R> mean(log(y))
[1] 2.986926
R> sd(log(y))
[1] 2.024713


> I noticed on wikipedia lognormal page that the median is exp(mu) and  
> that the mean is exp(mu + sigma^2/2)
> 
> http://en.wikipedia.org/wiki/Log-normal_distribution

Where mu and sigma are the mean and standard deviation of a normal
variate which is exponentiated to obtain a log normal variate.  And
this holds for the above example (upto sampling variation):

R> mean(y)
[1] 143.1624
R> exp(3+2^2/2)
[1] 148.4132

> So, does this mean that if i want a mean of 100 that the meanlog
> value needs to be log(100) - log(sd)^2/2?

A mean of 100 for the log-normal variate?  In this case any set of mu
and sd for which exp(mu+sd^2/2)=100 (or mu+sd^2/2=log(100)) would do
the trick:

R> mu <- 2
R> sd <- sqrt(2*(log(100)-mu))
R> summary(rlnorm(10000, mean=mu, sd=sd))
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
4.010e-04 1.551e+00 7.075e+00 1.006e+02 3.344e+01 3.666e+04 
R> mu <- 4
R> sd <- sqrt(2*(log(100)-mu))
R> summary(rlnorm(10000, mean=mu, sd=sd))
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
   0.9965   25.9400   56.0200  101.2000  115.5000 3030.0000 
R> mu <- 1
R> sd <- sqrt(2*(log(100)-mu))
R> summary(rlnorm(10000, mean=mu, sd=sd))
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
9.408e-05 4.218e-01 2.797e+00 8.845e+01 1.591e+01 7.538e+04 

Note that given the variation we would expect in the mean in the last
example, the mean is actually "close enough" to the theoretical value
of 100:

R> sqrt((exp(sd^2)-1)*exp(2*mu + sd^2)/10000)
[1] 36.77435

HTH.

Cheers,
	
	Berwin

=========================== Full address =============================
Berwin A Turlach                            Tel.: +65 6515 4416 (secr)
Dept of Statistics and Applied Probability        +65 6515 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



More information about the R-help mailing list