Post by Malte Möser and Matthew Salganik

There’s an activity in *Bit by Bit: Social Research in the Digital Age* that requires generating random draws from a log-normal distribution. Unfortunately, the rlnorm() function in R doesn’t work exactly how many people expect. So, we wanted to write a little post about it. That way, if you are working on the activity—which is about power analysis—you can focus on power analysis and not the rlnorm() function.

Let’s imagine that you wanted to generate one million draws from a log-normal distribution with a mean of 7 and a standard deviation of 75.

We’ll start by setting the seed so that this analysis is reproducible.

set.seed(123)

You might try:

draws1 <- rlnorm(n = 1000000, mean = 7, sd = 75)

But, what you would find is that this doesn’t quite do what you want.

Then, you might look at the help file for rlnorm() and try this:

draws2 <- rlnorm(n = 1000000, meanlog = log(7), sdlog = log(75))

Unfortunately, that doesn’t do what you want either.

The problem here is that, by definition, the logarithm of the log-normal distribution follows a normal distribution with the mean and standard deviation we just specified. Essentially, rlnorm(n = 1000000, meanlog = 7, sdlog = 75) and exp(rnorm(n = 1000000, mean = 7, sd = 75)) produce the same result.

To get a sample of random data that follows a log-normal distribution and has arithmetic mean of 7 and a standard deviation of 75, you need to reparameterize things. Roughly, you need to figure out what parameters should go into to the normal distribution such that when you exponentiate the draws, you end up with a mean of 7 and a standard deviation of 75. Fortunately, that algebra has already been done and you can find the result on Wikipedia. Here’s how you do it:

m <- 7
s <- 75
location <- log(m^2 / sqrt(s^2 + m^2))
shape <- sqrt(log(1 + (s^2 / m^2)))
print(paste("location:", location))
print(paste("shape:", shape))
draws3 <- rlnorm(n=1000000, location, shape)

If you want to try, here’s a file with all the steps. And, if you would like to read more about the problem that requires this, check out the activities in Chapter 4.

### Like this:

Like Loading...

*Related*