Skip to content

Conversation

behramulukir
Copy link
Collaborator

@behramulukir behramulukir commented Aug 28, 2025

As stated by @santikka at #330, currently, stat functions in bayesplot, such as ppc_stat and ppd_stat, completely rely on histograms while plotting. This is problematic since histograms are not optimal for discrete data, which the user might be working with. To overcome this issue, this PR adds a discrete argument to four stat functions: ppc_stat, ppc_stat_grouped, ppd_stat, and ppd_stat_grouped. The argument is by default set to FALSE, so the functions behave as previously. However, when it is set to TRUE, it calls geom_bar and plots a bar plot.

Tasks:

  • implement the argument
  • update tests
  • update documentation

Examples

y <- example_y_data
yrep <- example_yrep_draws
ppc_stat(y, yrep, stat = "median")

group <- example_group_data
ppc_stat_grouped(y, yrep, group, "median")

ppc_stat ppc_stat_grouped
So, the cases presented in the documentation are working as they should.

Now, let's look at the example case of @santikka:

y_discrete <- rbinom(20, 1, 0.2)
yrep_discrete <- matrix(rbinom(2000, 1, prob = 0.4), 1000, 20, byrow = TRUE)
ppc_stat(y_discrete, yrep_discrete, function(x) sum(abs(diff(x))))

group_discrete <- rep(c("First Half","Second Half"), each = 10)
ppc_stat_grouped(y_discrete, yrep_discrete, group_discrete, function(x) sum(abs(diff(x))))

ppc_stat_discrete_false ppc_stat_group_discrete_false

On the other hand, when we use the discrete argument, they are plotted nicely as bar plots.

ppc_stat(y_discrete, yrep_discrete, function(x) sum(abs(diff(x))), discrete = TRUE)
ppc_stat_grouped(y_discrete, yrep_discrete, group_discrete, function(x) sum(abs(diff(x))), discrete = TRUE)

ppc_stat_discrete_true ppc_stat_grouped_discrete_true

A similar logic applies to ppd_stat versions as well.

ppd_stat(yrep, "median")
ppd_stat_grouped(yrep, group, "median")

ppd_stat ppd_stat_grouped

ppd_stat(yrep_discrete, "median")
ppd_stat_grouped(yrep_discrete, group_discrete, "median")

ppd_stat_discrete_false ppd_stat_grouped_discrete_false

ppd_stat(yrep_discrete, "median", discrete = TRUE)
ppd_stat_grouped(yrep_discrete, group_discrete, "median", discrete = TRUE)

ppd_stat_discrete_true ppd_stat_grouped_discrete_true

@behramulukir behramulukir self-assigned this Aug 28, 2025
@behramulukir behramulukir linked an issue Aug 28, 2025 that may be closed by this pull request
@codecov-commenter
Copy link

codecov-commenter commented Aug 28, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.62%. Comparing base (e7d10d9) to head (68ceb49).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #369   +/-   ##
=======================================
  Coverage   98.62%   98.62%           
=======================================
  Files          35       35           
  Lines        5729     5748   +19     
=======================================
+ Hits         5650     5669   +19     
  Misses         79       79           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jgabry
Copy link
Member

jgabry commented Aug 28, 2025

Great, thanks @behramulukir, this looks good.

@behramulukir behramulukir marked this pull request as ready for review August 28, 2025 21:47
@behramulukir behramulukir changed the title adding discrete argument to stat functions adding discrete argument to stat functions Aug 28, 2025
Copy link
Member

@jgabry jgabry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @behramulukir, this is great. Merging now.

@jgabry jgabry merged commit fc0559c into master Aug 29, 2025
6 checks passed
@jgabry jgabry deleted the ppc_stat branch August 29, 2025 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ppc_stat for discrete test statistics?
3 participants