2
$\begingroup$

I tried to draw a log-normal density function by generating random numbers in R. However, the function is not working how I think it should. I draw two similar distribution using two different sample size.

plot(density(rlnorm(1000000,meanlog = -1.43, sdlog = 0.7)))

plot(density(rlnorm(100000000,meanlog = -1.43, sdlog = 0.7)))

For some reason, I got two very different distributions. enter image description here

enter image description here

Am I getting something wrong? I thought that these two density function should be nearly similar.

Edit:

I draw the density function also in excel. I think this should be the correct distribution. Is there some error in R-function?

enter image description here

$\endgroup$
3
  • $\begingroup$ what is the default number of samples? How does that compare to your domain width? $\endgroup$ – EngrStudent Jul 5 '18 at 16:57
  • 1
    $\begingroup$ The area under the first density is around 1.0 while the area under the second is around 3.0. A fix (and NOT an explanation) is to use a larger number of points to estimate the density: plot(density(rlnorm(100000000,meanlog = -1.43, sdlog = 0.7), n=2^12)). $\endgroup$ – JimB Jul 5 '18 at 17:37
  • $\begingroup$ I was a bit sloppy in my terminology: By "use a larger number of points to estimate the density" I didn't mean the number of random sample points but rather the number of points used to created the display. The default number of display points for density is 512 but that just isn't enough in this case. I think the problem is because density calls the function fft with too few sample points for this case. $\endgroup$ – JimB Jul 6 '18 at 15:10
1
$\begingroup$

This is an issue with density() and not with rlnorm(): controlling the support of the estimated density produces a complete agreement for different sample sizes:

plot(density(rlnorm(1e7),from=0,to=10),col="steelblue",lty=1)
lines(density(rlnorm(1e6),from=0,to=10),col="steelblue2",lty=2)
lines(density(rlnorm(1e5),from=0,to=10),col="steelblue3",lty=3)
curve(dlnorm,add=TRUE,col="sienna3",lty=4)

enter image description here

$\endgroup$
0
$\begingroup$

Your density functions are the same. You can sort of eyeball it from the graph. Because the lognormal distribution is (somewhat) heavy tailed, the more random numbers you generate, the higher the chance of generating a very big number.

The density function takes all the numbers it's given and plots the density across all of them, so if you have a number way out at 100, it's going to squish everything else into the corner. Try generating the random numbers again - you'll likely have a graph that looks a little different.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.