Skip to content
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

Discrete pit in ppc pit ecdf #316

Merged
merged 6 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Suggests:
survival,
testthat (>= 2.0.0),
vdiffr (>= 1.0.2)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.0
VignetteBuilder: knitr
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# bayesplot 1.10.0.9000

* `ppc_pit_ecdf()` and `ppc_pit_ecdf_grouped()` now support discrete variables, and their default method for selecting the number of ECDF evaluation points has been updated.
* Items for next release here

# bayesplot 1.10.0
Expand Down
4 changes: 2 additions & 2 deletions R/helpers-ppc.R
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,12 @@ adjust_gamma_simulate <- function(N, L, K, prob, M) {
#' simultaneous coverage of the ECDF traces.
#' @noRd
interpolate_gamma <- function(N, K, prob, L) {
# Find the precomputed values ueful for the interpolation task.
# Find the precomputed values useful for the interpolation task.
vals <- get_interpolation_values(N, K, L, prob)
# Largest lower bound and smalles upper bound for N among precomputed values.
N_lb <- max(vals[vals$N <= N, ]$N)
N_ub <- min(vals[vals$N >= N, ]$N)
# Approximate largest lower bound and smalles upper bound for gamma.
# Approximate largest lower bound and smallest upper bound for gamma.
log_gamma_lb <- approx(
x = log(vals[vals$N == N_lb, ]$K),
y = log(vals[vals$N == N_lb, ]$val),
Expand Down
42 changes: 24 additions & 18 deletions R/ppc-distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -606,13 +606,13 @@ ppc_pit_ecdf <- function(y,
if (is.null(pit)) {
pit <- ppc_data(y, yrep) %>%
group_by(.data$y_id) %>%
dplyr::group_map(~ mean(.x$value[.x$is_y] >= .x$value[!.x$is_y])) %>%
dplyr::group_map(
~ mean(.x$value[.x$is_y] > .x$value[!.x$is_y]) +
runif(1, max = mean(.x$value[.x$is_y] == .x$value[!.x$is_y]))
) %>%
unlist()
if (is.null(K)) {
K <- min(
length(unique(ppc_data(y, yrep)$rep_id)) + 1,
length(pit)
)
K <- nrow(yrep) + 1
}
} else {
inform("'pit' specified so ignoring 'y', and 'yrep' if specified.")
Expand All @@ -632,20 +632,22 @@ ppc_pit_ecdf <- function(y,
ggplot() +
aes(
x = 1:K / K,
y = ecdf(pit)(seq(0, 1, length.out = K)) - (plot_diff == TRUE) * 1:K / K,
y = ecdf(pit)(seq(0, 1, length.out = K)) -
(plot_diff == TRUE) * seq(0, 1, length.out = K),
color = "y"
) +
geom_step(show.legend = FALSE) +
geom_step(aes(
y = lims$upper[-1] / N - (plot_diff == TRUE) * 1:K / K,
y = lims$upper[-1] / N - (plot_diff == TRUE) * seq(0, 1, length.out = K),
color = "yrep"
), show.legend = FALSE) +
),
linetype = 2, show.legend = FALSE) +
geom_step(aes(
y = lims$lower[-1] / N - (plot_diff == TRUE) * 1:K / K,
y = lims$lower[-1] / N - (plot_diff == TRUE) * seq(0, 1, length.out = K),
color = "yrep"
), show.legend = FALSE) +
yaxis_title(FALSE) +
xaxis_title(FALSE) +
),
linetype = 2, show.legend = FALSE) +
labs(y = ifelse(plot_diff,"ECDF - difference","ECDF"), x = "PIT") +
yaxis_ticks(FALSE) +
scale_color_ppc() +
bayesplot_theme_get()
Expand All @@ -671,10 +673,13 @@ ppc_pit_ecdf_grouped <-
if (is.null(pit)) {
pit <- ppc_data(y, yrep, group) %>%
group_by(.data$y_id) %>%
dplyr::group_map(~ mean(.x$value[.x$is_y] >= .x$value[!.x$is_y])) %>%
dplyr::group_map(
~ mean(.x$value[.x$is_y] > .x$value[!.x$is_y]) +
runif(1, max = mean(.x$value[.x$is_y] == .x$value[!.x$is_y]))
) %>%
unlist()
if (is.null(K)) {
K <- length(unique(ppc_data(y, yrep)$rep_id)) + 1
K <- nrow(yrep) + 1
}
} else {
inform("'pit' specified so ignoring 'y' and 'yrep' if specified.")
Expand Down Expand Up @@ -723,13 +728,14 @@ ppc_pit_ecdf_grouped <-
geom_step(aes(
y = .data$lims_upper - (plot_diff == TRUE) * .data$x,
color = "yrep"
), show.legend = FALSE) +
),
linetype = 2, show.legend = FALSE) +
geom_step(aes(
y = .data$lims_lower - (plot_diff == TRUE) * .data$x,
color = "yrep"
), show.legend = FALSE) +
xaxis_title(FALSE) +
yaxis_title(FALSE) +
),
linetype = 2, show.legend = FALSE) +
labs(y = ifelse(plot_diff,"ECDF - difference","ECDF"), x = "PIT") +
yaxis_ticks(FALSE) +
bayesplot_theme_get() +
facet_wrap("group") +
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
8 changes: 5 additions & 3 deletions man-roxygen/args-pit-ecdf.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#' @param K An optional integer defining the number of equally spaced evaluation
#' points for the ECDF. If the submitted PIT values are known to be discrete,
#' this should be the number of the discrete cases. Defaults to the smaller of
#' `length(y)` and `ncol(yrep)` when applicable.
#' points for the ECDF. Reducing K when using `interpolate_adj = FALSE` makes
#' computing the confidence bands faster. For `ppc_pit_ecdf` and
#' `ppc_pit_ecdf_grouped`, defaults to `ncol(yrep) + 1`, or `length(pit)` if PIT
#' values are supplied. For `mcmc_rank_ecdf` defaults to the number of
#' iterations per chain in `x`.
#' @param prob The desired simultaneous coverage level of the bands around the
#' ECDF. A value in (0,1).
#' @param plot_diff A boolean defining whether to plot the difference between
Expand Down
8 changes: 5 additions & 3 deletions man/MCMC-traces.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions man/PPC-distributions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/bayesplot-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 31 additions & 29 deletions tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-default.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading