I need to generate random values that represent times (in seconds) that follow a lognormal distribution with:
Min: 120 seconds
Max: 1260 seconds
Mean: 356 seconds
SD: 98 seconds
I am generating 100 random numbers:
library(EnvStats)
sample1 <- rlnormTrunc(100,356,98,120,1260)
and when I calculate the mean, it is not 356
, but higher, about 490
seconds. Why?
I don't understand what I am doing wrong as I though I was going to get the same mean.
Does anyone has an answer for this?
The reason is that you compare different distributions, so when you create random numbers out of these distributions, their mean is different. If we take as an example the normal distribution then
set.seed(111)
sample1 <- rnorm(n=10000,mean=356,sd=98)
mean(sample1) #355.7724
the mean would indeed almost 356. But if we took the truncated Normal Distribution then
set.seed(111)
sample2<-rnormTrunc(n=100000,mean=356,sd=98,min=120 ,max=1260)
mean(sample2) #357.9636
the mean would be slightly different, around 358 but not 356 . The reason why the difference is so small is because, as seen in the histogram
hist(rnorm(n=10000,mean=356,sd=98),breaks=100,xlim=c(0,1300))
abline(v=120,col="red")
abline(v=1260,col="red")
by truncating, you take out very infrequent values ( smaller than 120 and bigger than 1260).
LogNormal is a fat-tailed distribution, skewed to the right. This means that it includes far more infrequent values than the normal distribution, far beyond 1260. If you truncate the distribution between 120 and 1260
hist(rlnormTrunc(10000,meanlog=356,sdlog=98,min=120,max=1260),breaks=100)
you get
set.seed(111)
mean(rlnormTrunc(10000,meanlog=356,sdlog=98,min=120,max=1260)) #493.3903
In each of the examples above you calculate the mean for a random set of values of different ranges because of different distributions, that's why you end up with different mean values.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.