Skip to content

add x as argument to ppc_error_binned #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

behramulukir
Copy link
Collaborator

@behramulukir behramulukir commented Jul 9, 2025

In #358, one of the two tasks that I mentioned was adding x as an optional argument to ppc_error_binned so that it supports covariates in the x-axis. As x argument would be optional, it wouldn't be breaking any existing graphs.

This PR is my work on the first task of #358.

My current implementation passes existing tests without any issue, so I think I am not breaking anything.

  • Add x argument
  • Add new tests
  • Update documentation

Examples

Here are a few examples of it working.

Default version

This default version simply follows the example presented at the PPC errors documentation.

> suppressPackageStartupMessages(library(rstanarm))
> suppressWarnings(example("example_model", package = "rstanarm"))
> formula(example_model)
cbind(incidence, size - incidence) ~ size + period + (1 | herd)
> y <- example_model$y  # matrix of "success" and "failure" counts
> trials <- rowSums(y)
> y_prop <- y[, 1] / trials  # proportions
> 
> # get predicted success proportions
> yrep <- posterior_predict(example_model)
> yrep_prop <- sweep(yrep, 2, trials, "/")
> ppc_error_binned(y_prop, yrep_prop[1:6, ])

binned_default

Herd

This is the plot of average errors against herd variable.

> herd <- as.numeric(example_model$data$herd)
> ppc_error_binned(y_prop, yrep_prop[1:6, ], herd)

binned_herd

Size

This is the plot of average errors against size variable.

> size <- as.numeric(example_model$data$size)
> ppc_error_binned(y_prop, yrep_prop[1:6, ], size)

binned_size

@behramulukir behramulukir self-assigned this Jul 9, 2025
@codecov-commenter
Copy link

codecov-commenter commented Jul 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.60%. Comparing base (527c48c) to head (67b863c).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #359   +/-   ##
=======================================
  Coverage   98.60%   98.60%           
=======================================
  Files          35       35           
  Lines        5650     5660   +10     
=======================================
+ Hits         5571     5581   +10     
  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 Jul 9, 2025

At first glance this looks good. I will try it out tomorrow when I have more time.

My current implementation passes existing tests without any issue, so I think I am not breaking anything.

Yeah, the only reason the R-CMD-check github actions are failing is because there's a new argument that isn't documented yet: https://github.com/stan-dev/bayesplot/actions/runs/16175724060/job/45660276284?pr=359#step:6:142.

The unit tests themselves are passing so I agree that this doesn't seem to break anything.

@behramulukir behramulukir marked this pull request as ready for review July 10, 2025 14:47
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.

3 participants