[R] Confused by dlnorm - densities do not match histogram

Terran Melconian terran at consistent.org
Tue Sep 23 04:59:19 CEST 2014


Good evening!  I'm running into some surprising behavior with dlnorm() and
trying to understand it.

To set the stage, I'll plot the density and overlay a normal distribution.
This works exactly as expected; the two graphs align quite closely:

qplot(data=data.frame(x=rnorm(1e5,4,2)),x=x,stat='density',geom='area') +
stat_function(fun=dnorm,args=list(4,2),colour='blue')

but then I change to a log normal distribution and the behaviour gets
odd.  The distribution looks nothing like the density plot:

qplot(data=data.frame(x=rlnorm(1e5,4,2)),x=x,log='x',stat='density',geom='area') + stat_function(fun=dlnorm,args=list(4,2),colour='blue')

I thought the issue might be scale transformation - if dlnorm is giving the
density per unit x this is not the same as the density after transforming
to log(x).  So I tried to effect this scale transformation manually by
dividing by the derivative of log(x) - i.e. by multiplying by x - but this
also did not match:

qplot(data=data.frame(x=rlnorm(1e5,4,2)),x=x,log='x',stat='density',geom='area') + stat_function(fun=function(x,...){dlnorm(x,...)*x},args=list(4,2),colour='blue')

I also tried plotting without the log scale to eliminate that
transformation as a source of discrepancy, and they still don't match:

qplot(data=data.frame(x=rlnorm(1e5,4,2)),x=x,stat='density',geom='area',xlim=c(0,50)) + stat_function(fun=dlnorm,args=list(4,2),colour='blue')

I'd appreciate any help in understanding what I'm missing.



More information about the R-help mailing list