Skip to content

Commit 0545e16

Browse files
preparation for interpolation of adjustments
1 parent 617a769 commit 0545e16

File tree

3 files changed

+58
-41
lines changed

3 files changed

+58
-41
lines changed

R/helpers-ppc.R

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -293,24 +293,38 @@ adjust_gamma <- function(N,
293293
K = N,
294294
prob = 0.99,
295295
M = 1000,
296-
adj_method = "interpolate") {
297-
if (any(c(K, N, L) < 1)) {
296+
interpolate_adj = FALSE) {
297+
if (any(
298+
c(K, N, L) < 1,
299+
all_counts(c(K, N, L))
300+
)) {
298301
abort("Parameters 'N', 'L' and 'K' must be positive integers.")
299302
}
300303
if (prob >= 1 || prob <= 0) {
301304
abort("Value of 'prob' must be in (0,1).")
302305
}
303306
if (L == 1) {
304-
gamma <- adjust_gamma_optimize(N, K, prob)
305-
}
306-
else {
307-
gamma <- adjust_gamma_simulate(N, L, K, prob, M)
307+
if (interpolate_adj == TRUE) {
308+
gamma <- 0
309+
} else {
310+
gamma <- adjust_gamma_optimize(N, K, prob)
311+
}
312+
} else {
313+
if (interpolate_adj == TRUE) {
314+
gamma <- 0
315+
} else {
316+
gamma <- adjust_gamma_simulate(N,
317+
L,
318+
K,
319+
prob,
320+
M)
321+
}
308322
}
309323
gamma
310324
}
311325

312326
# Adjust coverage parameter for single sample using the optimization method.
313-
adjust_gamma_optimize <- function(N, K, prob=0.99) {
327+
adjust_gamma_optimize <- function(N, K, prob) {
314328
target <- function(gamma, prob, N, K) {
315329
z <- 1:(K - 1) / K
316330
z1 <- c(0, z)
@@ -339,7 +353,7 @@ adjust_gamma_optimize <- function(N, K, prob=0.99) {
339353
}
340354

341355
# Adjust coverage parameter for multiple chains using simulation method.
342-
adjust_gamma_simulate <- function(N, L, K, prob = 0.99, M = 1000) {
356+
adjust_gamma_simulate <- function(N, L, K, prob, M) {
343357
gamma <- numeric(M)
344358
z <- (1:(K - 1)) / K
345359
n <- N * (L - 1)
@@ -397,7 +411,7 @@ alpha_quantile <- function(gamma, alpha, tol = 0.001) {
397411
#' @param gamma Adjusted coverage parameter for the marginal distribution
398412
#' (binomial for PIT values and hypergeometric for rank transformed chains).
399413
#' @noRd
400-
ecdf_intervals <- function(N, L=1, K, gamma) {
414+
ecdf_intervals <- function(N, L = 1, K, gamma) {
401415
lims <- list()
402416
z <- seq(0, 1, length.out = K + 1)
403417
if (L == 1) {

R/mcmc-traces.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,10 @@ mcmc_rank_ecdf <-
453453
facet_args = list(),
454454
prob = 0.99,
455455
plot_diff = TRUE,
456-
adj_method = "interpolate") {
457-
check_ignored_arguments(..., ok_args = c("M"))
456+
interpolate_adj = FALSE) {
457+
check_ignored_arguments(...,
458+
ok_args = c("K", "pit", "prob", "plot_diff", "interpolate_adj", "M")
459+
)
458460
data <- mcmc_trace_data(
459461
x,
460462
pars = pars,
@@ -479,8 +481,8 @@ mcmc_rank_ecdf <-
479481
K
480482
},
481483
prob = prob,
482-
...,
483-
adj_method = adj_method
484+
interpolate_adj = interpolate_adj,
485+
...
484486
)
485487
lims <- ecdf_intervals(
486488
N = n_iter,

R/ppc-distributions.R

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,9 @@ ppc_pit_ecdf <- function(y,
558558
K = NULL,
559559
prob = .99,
560560
plot_diff = TRUE,
561-
adj_method = "interpolate") {
562-
check_ignored_arguments(...)
561+
interpolate_adj = FALSE) {
562+
check_ignored_arguments(...,
563+
ok_args = c("K", "pit", "prob", "plot_diff", "interpolate_adj"))
563564

564565
if (is.null(pit)) {
565566
pit <- ppc_data(y, yrep) %>%
@@ -578,24 +579,23 @@ ppc_pit_ecdf <- function(y,
578579
}
579580
N <- length(pit)
580581
gamma <- adjust_gamma(N,
581-
K = K,
582-
conf_level = prob,
583-
adj_method = adj_method
584-
)
582+
K = K,
583+
prob = prob,
584+
interpolate_adj = interpolate_adj)
585585
lims <- ecdf_intervals(N, K = K, gamma = gamma)
586586
ggplot() +
587587
aes_(
588-
x = 0:K / K,
589-
y = ecdf(pit)(0:K / K) - (plot_diff == TRUE) * 0:K / K,
588+
x = 1:K / K,
589+
y = ecdf(pit)(seq(0, 1, length.out = K)) - (plot_diff == TRUE) * 1:K / K,
590590
color = "y"
591591
) +
592592
geom_step(show.legend = FALSE) +
593593
geom_step(aes(
594-
y = lims$upper / N - (plot_diff == TRUE) * 0:K / K,
594+
y = lims$upper[-1] / N - (plot_diff == TRUE) * 1:K / K,
595595
color = "yrep"
596596
), show.legend = FALSE) +
597597
geom_step(aes(
598-
y = lims$lower / N - (plot_diff == TRUE) * 0:K / K,
598+
y = lims$lower[-1] / N - (plot_diff == TRUE) * 1:K / K,
599599
color = "yrep"
600600
), show.legend = FALSE) +
601601
yaxis_title(FALSE) +
@@ -613,20 +613,21 @@ ppc_pit_ecdf_grouped <-
613613
yrep,
614614
group,
615615
...,
616-
K,
617-
pit,
616+
K = NULL,
617+
pit = NULL,
618618
prob = .99,
619619
plot_diff = TRUE,
620-
adj_method = "interpolate") {
621-
check_ignored_arguments(...)
620+
interpolate_adj = FALSE) {
621+
check_ignored_arguments(...,
622+
ok_args = c("K", "pit", "prob", "plot_diff", "interpolate_adj"))
622623

623-
if (missing(pit)) {
624+
if (is.null(pit)) {
624625
pit <- ppc_data(y, yrep, group) %>%
625626
group_by(y_id) %>%
626627
dplyr::group_map(~ mean(.x$value[.x$is_y] >= .x$value[!.x$is_y])) %>%
627628
unlist()
628-
if (missing(K)) {
629-
K0 <- length(unique(ppc_data(y, yrep)$rep_id))
629+
if (is.null(K)) {
630+
K <- length(unique(ppc_data(y, yrep)$rep_id)) + 1
630631
}
631632
} else {
632633
inform("'pit' specified so ignoring 'y', and 'yrep' if specified.")
@@ -638,9 +639,9 @@ ppc_pit_ecdf_grouped <-
638639
N_g <- sum(group == g)
639640
adjust_gamma(
640641
N_g,
641-
K = min(N_g, K0),
642-
conf_level = prob,
643-
adj_method = adj_method
642+
K = min(N_g, K),
643+
prob = prob,
644+
interpolate_adj = interpolate_adj
644645
)
645646
})
646647
names(gammas) <- unique(group)
@@ -649,26 +650,26 @@ ppc_pit_ecdf_grouped <-
649650
group_by(group) %>%
650651
dplyr::group_map(~ data.frame(
651652
ecdf_value = ecdf(.x$pit)(
652-
seq(0, 1, length.out = min(nrow(.x) + 1, K0 + 1))),
653+
seq(0, 1, length.out = min(nrow(.x), K))),
653654
group = .y[1],
654655
lims_upper = ecdf_intervals(
655656
N = nrow(.x),
656-
K = min(nrow(.x), K0),
657+
K = min(nrow(.x), K),
657658
gamma = gammas[[unlist(.y[1])]]
658-
)$upper / nrow(.x),
659+
)$upper[-1] / nrow(.x),
659660
lims_lower = ecdf_intervals(
660661
N = nrow(.x),
661-
K = min(nrow(.x), K0),
662+
K = min(nrow(.x), K),
662663
gamma = gammas[[unlist(.y[1])]]
663-
)$lower / nrow(.x),
664-
x = seq(0, 1, length.out = min(nrow(.x) + 1, K0 + 1))
664+
)$lower[-1] / nrow(.x),
665+
x = seq(0, 1, length.out = min(nrow(.x), K))
665666
)) %>%
666667
dplyr::bind_rows()
667668
ggplot(data) +
668669
aes_(
669-
x = ~x,
670+
x = ~ x,
670671
y = ~ ecdf_value - (plot_diff == TRUE) * x,
671-
group = ~group,
672+
group = ~ group,
672673
color = "y"
673674
) +
674675
geom_step(show.legend = FALSE) +

0 commit comments

Comments
 (0)