Simulation of Lognormal distribution
This post is a try about how yo simulate lognormal distribution in R. Lognormal distribution is used a lot in cumulative data (e.x. counting), which is very similar with normal distribution except x should be larger than 0. For instance, the number of schools, the number of students. But I always have no idea about how to parameterized this distribution. I’ll update this post as I learn more about lognormal distribution…
1 Simulate lognomal distibution
1.1 Simulation Study 1
This study is to simulate lognormal density distribution based on mean and sd of depedent variable (Y). My simulated mean of y is 891, and sd is 490, N (sample size) is 200000. Then use the formular below:
= log(m^2/phi) # log mean
mu = sqrt(log(1+v/m^2)) # log sd sigma
It could calculate the log mean and log standard deviation for lognormal distribution.
set.seed(20171108)
#### Give Y mean and Y sd, simluate lognormal distribution data.
= 891 # geometric mean
m = 490 # geometric sd
sd = sd ^ 2
v = sqrt(v + m^2)
phi
= log(m^2/phi) # log mean
mu = sqrt(log(1+v/m^2)) # log sd
sigma
<- rlnorm(n = 200000, mu, sigma) %>% round(0)
y <- mean(y) # should be close to 891
m.sim <- sd(y) # should be close to 490
sd.sim
<- c(m, mu,m.sim)
row1 <- c(sd, sigma,sd.sim)
row2 <- rbind(row1, row2)
table colnames(table) <- c("Original", "Log", "Simulated")
rownames(table) <- c("Mean", "SD")
kable(table)
This is the original, log and simulated mean and sd. It could be easily found that simulated ones are very closed to original.
Original | Log | Simulated | |
---|---|---|---|
Mean | 891 | 6.660225 | 891.1468 |
SD | 490 | 0.514041 | 490.4396 |
plot(density(y))
From the density plot below, we can see the mean of X is also close to 891.