Skip to content

Add marginal dist. to PPD plots#429

Open
mattansb wants to merge 28 commits intostan-dev:masterfrom
mattansb:master
Open

Add marginal dist. to PPD plots#429
mattansb wants to merge 28 commits intostan-dev:masterfrom
mattansb:master

Conversation

@mattansb
Copy link

@mattansb mattansb commented Mar 8, 2026

Addresses #425

This PR adds the show_marginal argument (default FALSE) to the PPD-distributions and the PPD-test-statistics functions (the PPD-intervals functions de-facto show the marginal PPD(s)).

I think we need a better default PPD color - which?

Here are all of these functions (default plots -- show_marginal=FALSE -- are unchanged!):

library(bayesplot)
#> This is bayesplot version 1.15.0.9000
#> - Online documentation and vignettes at mc-stan.org/bayesplot
#> - bayesplot theme set to bayesplot::theme_default()
#>    * Does _not_ affect other ggplot2 plots
#>    * See ?bayesplot_theme_set for details on theme setting
library(ggplot2)

set.seed(20260225)
mu_posterior <- rnorm(20, mean = 3, sd = 1)
sigma_posterior <- rgamma(20, shape = 2, rate = 1)
ypred <- t(mapply(rnorm, mu_posterior, sigma_posterior, n = 50))
g <- gl(2, 25)
x <- rnorm(50)

ppd-distributions

# ppd_dens_overlay(ypred)
ppd_dens_overlay(ypred, show_marginal = TRUE)

# ppd_ecdf_overlay(ypred)
ppd_ecdf_overlay(ypred, show_marginal = TRUE)

# ppd_dens(ypred)
ppd_dens(ypred, show_marginal = TRUE)

# ppd_hist(ypred)
ppd_hist(ypred, show_marginal = TRUE)
#> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

# ppd_dots(ypred)
ppd_dots(ypred, show_marginal = TRUE)

# ppd_freqpoly(ypred)
ppd_freqpoly(ypred, show_marginal = TRUE)
#> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

# ppd_freqpoly_grouped(ypred[1:5,], group = g)
ppd_freqpoly_grouped(ypred[1:5,], group = g, show_marginal = TRUE)
#> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

# ppd_boxplot(ypred)
ppd_boxplot(ypred, show_marginal = TRUE)

ppd-test-statistics

# ppd_stat(ypred)
ppd_stat(ypred, show_marginal = TRUE)
#> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

# ppd_stat_grouped(ypred, g)
ppd_stat_grouped(ypred, g, show_marginal = TRUE)
#> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

# ppd_stat_freqpoly(ypred)
ppd_stat_freqpoly(ypred, show_marginal = TRUE)
#> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

# ppd_stat_freqpoly_grouped(ypred, g)
ppd_stat_freqpoly_grouped(ypred, g, show_marginal = TRUE)
#> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

# ppd_stat_2d(ypred)
ppd_stat_2d(ypred, show_marginal = TRUE)

Created on 2026-03-08 with reprex v2.1.1

@jgabry
Copy link
Member

jgabry commented Mar 9, 2026

Thanks for the PR! I haven't had a chance to look at the code yet (I'm a bit swamped with work at the moment), but a couple quick comments:

  1. I just triggered the GHA workflows to run and it looks like there are some failures.

  2. Regarding the color issue you mentioned, this is my fault due to lack for foresight! When I added the PPD plots I was assuming no overlays since we wouldn't be comparing to data. So instead of the light and dark distinction used for the PPC plots, I went with a color somewhere in between. But now this makes it harder to find a good color for overlays.

@mattansb
Copy link
Author

@jgabry I fixed the error that popped up.

Well the color "issue" can be resolved quite simply if we switch from "mid" to "light" for PPD, like PPC.
Or with some logic we can use "light"/"dark" only if show_marginal = TRUE and "mid" otherwise.
WDYT?

@jgabry
Copy link
Member

jgabry commented Mar 10, 2026

Well the color "issue" can be resolved quite simply if we switch from "mid" to "light" for PPD, like PPC.
Or with some logic we can use "light"/"dark" only if show_marginal = TRUE and "mid" otherwise.
WDYT?

@tjmahr Any preference on this? The first option would keep the code simpler, but it would change the appearance of all the PPD plots (could we call this breaking visual backwards compatibility?). I would lean towards the second option but I'm not really sure.

@jgabry
Copy link
Member

jgabry commented Mar 10, 2026

I just tried using this branch and show_marginal seems to work well for the plots I tried, but I don't think the new argument is documented yet.

@mattansb
Copy link
Author

Sorry, it was only documented in PPD-distributions. Added to PPD-test-statistics now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants