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.

So to generate random values that fit a lognormal distribution I would  
do this:

rlnorm(N , meanlog = log(mean) , sdlog = log(sd))

But when I check the mean I don't get it when sdlog>0. Interestingly I  
found that the median is close to what I want the mean to be.

 > par( mfrow = c(2,2) )
 > set.seed( 1 )
 > x <- 1:100000
 > y <- rlnorm( x , meanlog = log(100) , sdlog = log(2) )
 > plot( x , y )
 > hist( y , breaks = 100)
 > summary( y )
     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
    4.292   62.360  100.100  127.100  159.300 1989.000

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

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?

It seems to give me the result I am after.

 > par( mfrow = c(2,2) )
 > set.seed( 1 )
 > x <- 1:100000
 > y <- rlnorm( x , meanlog = log(100) , sdlog = log(2) )
 > plot( x , y )
 > hist( y , breaks = 100)
 > summary( y )
     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
    4.292   62.360  100.100  127.100  159.300 1989.000
 > set.seed( 1 )
 > y <- rlnorm( x , meanlog = log(100) - log(2)^2/2 , sdlog = log(2) )
 > plot( x , y )
 > hist( y , breaks = 100)
 > summary( y )
     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
    3.376   49.040   78.690   99.970  125.300 1564.000 
        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to