Terran Melconian
2014-Sep-23 02:59 UTC
[R] Confused by dlnorm - densities do not match histogram
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.