Skip to content

Commit 55e5b20

Browse files
authored
Merge pull request #316 from stan-dev/discrete_PIT_in_ppc_pit_ecdf
Discrete pit in ppc pit ecdf
2 parents 07df2ef + d97e7d9 commit 55e5b20

13 files changed

+305
-262
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Suggests:
5555
survival,
5656
testthat (>= 2.0.0),
5757
vdiffr (>= 1.0.2)
58-
RoxygenNote: 7.2.3
58+
RoxygenNote: 7.3.0
5959
VignetteBuilder: knitr
6060
Encoding: UTF-8
6161
Roxygen: list(markdown = TRUE)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# bayesplot 1.10.0.9000
44

5+
* `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.
56
* Items for next release here
67

78
# bayesplot 1.10.0

R/helpers-ppc.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,12 @@ adjust_gamma_simulate <- function(N, L, K, prob, M) {
416416
#' simultaneous coverage of the ECDF traces.
417417
#' @noRd
418418
interpolate_gamma <- function(N, K, prob, L) {
419-
# Find the precomputed values ueful for the interpolation task.
419+
# Find the precomputed values useful for the interpolation task.
420420
vals <- get_interpolation_values(N, K, L, prob)
421421
# Largest lower bound and smalles upper bound for N among precomputed values.
422422
N_lb <- max(vals[vals$N <= N, ]$N)
423423
N_ub <- min(vals[vals$N >= N, ]$N)
424-
# Approximate largest lower bound and smalles upper bound for gamma.
424+
# Approximate largest lower bound and smallest upper bound for gamma.
425425
log_gamma_lb <- approx(
426426
x = log(vals[vals$N == N_lb, ]$K),
427427
y = log(vals[vals$N == N_lb, ]$val),

R/ppc-distributions.R

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -606,13 +606,13 @@ ppc_pit_ecdf <- function(y,
606606
if (is.null(pit)) {
607607
pit <- ppc_data(y, yrep) %>%
608608
group_by(.data$y_id) %>%
609-
dplyr::group_map(~ mean(.x$value[.x$is_y] >= .x$value[!.x$is_y])) %>%
609+
dplyr::group_map(
610+
~ mean(.x$value[.x$is_y] > .x$value[!.x$is_y]) +
611+
runif(1, max = mean(.x$value[.x$is_y] == .x$value[!.x$is_y]))
612+
) %>%
610613
unlist()
611614
if (is.null(K)) {
612-
K <- min(
613-
length(unique(ppc_data(y, yrep)$rep_id)) + 1,
614-
length(pit)
615-
)
615+
K <- nrow(yrep) + 1
616616
}
617617
} else {
618618
inform("'pit' specified so ignoring 'y', and 'yrep' if specified.")
@@ -632,20 +632,22 @@ ppc_pit_ecdf <- function(y,
632632
ggplot() +
633633
aes(
634634
x = 1:K / K,
635-
y = ecdf(pit)(seq(0, 1, length.out = K)) - (plot_diff == TRUE) * 1:K / K,
635+
y = ecdf(pit)(seq(0, 1, length.out = K)) -
636+
(plot_diff == TRUE) * seq(0, 1, length.out = K),
636637
color = "y"
637638
) +
638639
geom_step(show.legend = FALSE) +
639640
geom_step(aes(
640-
y = lims$upper[-1] / N - (plot_diff == TRUE) * 1:K / K,
641+
y = lims$upper[-1] / N - (plot_diff == TRUE) * seq(0, 1, length.out = K),
641642
color = "yrep"
642-
), show.legend = FALSE) +
643+
),
644+
linetype = 2, show.legend = FALSE) +
643645
geom_step(aes(
644-
y = lims$lower[-1] / N - (plot_diff == TRUE) * 1:K / K,
646+
y = lims$lower[-1] / N - (plot_diff == TRUE) * seq(0, 1, length.out = K),
645647
color = "yrep"
646-
), show.legend = FALSE) +
647-
yaxis_title(FALSE) +
648-
xaxis_title(FALSE) +
648+
),
649+
linetype = 2, show.legend = FALSE) +
650+
labs(y = ifelse(plot_diff,"ECDF - difference","ECDF"), x = "PIT") +
649651
yaxis_ticks(FALSE) +
650652
scale_color_ppc() +
651653
bayesplot_theme_get()
@@ -671,10 +673,13 @@ ppc_pit_ecdf_grouped <-
671673
if (is.null(pit)) {
672674
pit <- ppc_data(y, yrep, group) %>%
673675
group_by(.data$y_id) %>%
674-
dplyr::group_map(~ mean(.x$value[.x$is_y] >= .x$value[!.x$is_y])) %>%
676+
dplyr::group_map(
677+
~ mean(.x$value[.x$is_y] > .x$value[!.x$is_y]) +
678+
runif(1, max = mean(.x$value[.x$is_y] == .x$value[!.x$is_y]))
679+
) %>%
675680
unlist()
676681
if (is.null(K)) {
677-
K <- length(unique(ppc_data(y, yrep)$rep_id)) + 1
682+
K <- nrow(yrep) + 1
678683
}
679684
} else {
680685
inform("'pit' specified so ignoring 'y' and 'yrep' if specified.")
@@ -723,13 +728,14 @@ ppc_pit_ecdf_grouped <-
723728
geom_step(aes(
724729
y = .data$lims_upper - (plot_diff == TRUE) * .data$x,
725730
color = "yrep"
726-
), show.legend = FALSE) +
731+
),
732+
linetype = 2, show.legend = FALSE) +
727733
geom_step(aes(
728734
y = .data$lims_lower - (plot_diff == TRUE) * .data$x,
729735
color = "yrep"
730-
), show.legend = FALSE) +
731-
xaxis_title(FALSE) +
732-
yaxis_title(FALSE) +
736+
),
737+
linetype = 2, show.legend = FALSE) +
738+
labs(y = ifelse(plot_diff,"ECDF - difference","ECDF"), x = "PIT") +
733739
yaxis_ticks(FALSE) +
734740
bayesplot_theme_get() +
735741
facet_wrap("group") +

R/sysdata.rda

991 Bytes
Binary file not shown.

man-roxygen/args-pit-ecdf.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#' @param K An optional integer defining the number of equally spaced evaluation
2-
#' points for the ECDF. If the submitted PIT values are known to be discrete,
3-
#' this should be the number of the discrete cases. Defaults to the smaller of
4-
#' `length(y)` and `ncol(yrep)` when applicable.
2+
#' points for the ECDF. Reducing K when using `interpolate_adj = FALSE` makes
3+
#' computing the confidence bands faster. For `ppc_pit_ecdf` and
4+
#' `ppc_pit_ecdf_grouped`, defaults to `ncol(yrep) + 1`, or `length(pit)` if PIT
5+
#' values are supplied. For `mcmc_rank_ecdf` defaults to the number of
6+
#' iterations per chain in `x`.
57
#' @param prob The desired simultaneous coverage level of the bands around the
68
#' ECDF. A value in (0,1).
79
#' @param plot_diff A boolean defining whether to plot the difference between

man/MCMC-traces.Rd

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/PPC-distributions.Rd

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/bayesplot-package.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/ppc-distributions/ppc-pit-ecdf-default.svg

Lines changed: 31 additions & 29 deletions
Loading

0 commit comments

Comments
 (0)