LMc
LMc

Reputation: 18662

Plotting log normal density in R has wrong height

I have a log-normal density with a mean of -0.4 and standard deviation of 2.5.

At x = 0.001 the height is over 5 (I double checked this value with the formula for the log-normal PDF):

dlnorm(0.001, -0.4, 2.5)

5.389517

When I plot it using the curve function over the input range 0-6 it looks like with a height just over 1.5:

curve(dlnorm(x, -.4, 2.5), xlim = c(0, 6), ylim = c(0, 6))

enter image description here

When I adjust the input range to 0-1 the height is nearly 4:

curve(dlnorm(x, -.4, 2.5), xlim = c(0, 1), ylim = c(0, 6))

enter image description here


Similarly with ggplot2 (output not shown, but looks like the curve plots above):

library(ggplot2)

ggplot(data = data.frame(x = 0), mapping = aes(x = x)) + 
  stat_function(fun = function(x) dlnorm(x, -0.4, 2.5)) + 
  xlim(0, 6) + 
  ylim(0, 6)

ggplot(data = data.frame(x = 0), mapping = aes(x = x)) + 
  stat_function(fun = function(x) dlnorm(x, -0.4, 2.5)) + 
  xlim(0, 1) + 
  ylim(0, 6)

Does someone know why the density height is changing when the x-axis scale is adjusted? And why neither attempt above seems to reach the correct height? I tried this with just the normal density and this doesn't happen.

Upvotes: 0

Views: 91

Answers (1)

Leonardo
Leonardo

Reputation: 2485

curves generates a set of discrete points in the range you give it. By default it generates n = 101 points, so there is a step problem. If you increase the number of points you will have almost the correct value:

curve(dlnorm(x, -.4, 2.5), xlim = c(0, 1), ylim = c(0, 6), n = 1000)

In the first case you propose curve generates 101 points in the interval x <- c(0,6), while in the second case generates 101 points in the interval x <- c(0,1), so the step is more dense

Upvotes: 1

Related Questions