stan-dev / bayesplot

bayesplot R package for plotting Bayesian models
https://mc-stan.org/bayesplot
GNU General Public License v3.0
432 stars 83 forks source link

`ppc_stat` for discrete test statistics? #330

Open santikka opened 1 week ago

santikka commented 1 week ago

It seems that ppc_stat always assumes that the test statistic defined by stat is continuous, and thus draws a histogram, which is fine but sometimes looks quite ugly, for example:

set.seed(0)
y <- rbinom(20, 1, 0.2)
yrep <- matrix(rbinom(2000, 1, prob = 0.4), 1000, 20, byrow = TRUE)
bayesplot::ppc_stat(y, yrep, function(x) sum(abs(diff(x))))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Created on 2024-10-14 with reprex v2.1.1

Is there an option (or another function) to specify that the test statistic is discrete, so that ppc_stat would produce a barplot instead?

jgabry commented 1 week ago

Yeah you're right that it would look nicer as a bar plot when the test statistic is discrete. There's not a way to tell ppc_stat to do that right now, but I think it's a good idea. If you or anyone else is interested in adding it I would be happy to review a pull request. Otherwise I can certainly do it myself when I have time, but I'm not sure when that will be at the moment.