mjskay / tidybayes

Bayesian analysis + tidy data + geoms (R package)
http://mjskay.github.io/tidybayes
GNU General Public License v3.0
712 stars 59 forks source link

stat_dist_slabh not normalized correctly #247

Closed JackCaster closed 4 years ago

JackCaster commented 4 years ago

Hi, I suspect stat_dist_slabh does not normalize the pdf of a distribution correctly. For example, for a standard normal distribution

pdf <- tibble(x = seq(-5, 5, 0.1), pdf = dnorm(x))

ggplot() +
  stat_dist_slabh(aes(y = 0, 
                      dist = "norm", 
                      arg1 = 0, 
                      arg2 = 1, color='stat_dist_slabh'),
                  limits = c(-5, 5),
                  normalize = 'none') + 
  geom_line(aes(x = x, y = pdf,  color = 'dnorm'), data=pdf) + theme(legend.position="right")

gives

image

in which the blue line is the pdf generated by stat_dist_slabh and the red line is the pdf generated by dnorm. I would expect the two to overlap.

Am I missing something?

mjskay commented 4 years ago

Ah, you also need to set scale = 1 if you want the density to line up to the raw values. After normalization the density is scaled according to that value; the default is scale = 0.9 so that there's a bit of a gap between densities when you plot multiple ones.

JackCaster commented 4 years ago

I see. That fixed it. I saw the scale parameter was not listed in the stat_dist_slabh documentation, but I found it in the geom_slabinterval one. Thank you!

mjskay commented 4 years ago

No problem! Sorry about the unclear docs --- unfortunately due to the way the function documentation works those parameters are not automatically copied over. It's on my TODO to find a better solution to that so that the documentation is better.

Closing this for now.

JackCaster commented 4 years ago

Nothing to be sorry about! Great package 👌