Skip to content

Commit b493f32

Browse files
Added documentation to helper functions.
1 parent 979b679 commit b493f32

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

R/helpers-ppc.R

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,18 @@ all_counts <- function(x, ...) {
276276
all_whole_number(x, ...) && min(x) >= 0
277277
}
278278

279-
279+
#' Obtain the coverage parameter for simultaneous confidence bands for ECDFs
280+
#'
281+
#' @param N Length of sample.
282+
#' @param L Number of chains. Used for MCMC, defaults to 1 for ppc.
283+
#' @param K Number of equally spaced evaluation points (1:K / K). Defaults to N.
284+
#' @param prob Desired simultaneous coverage (0,1).
285+
#' @param M number of simulations to run, if simulation method is used.
286+
#' @param adj_method String defining the desired adjustment method. By default
287+
#' "interpolate" is used. Other available options "optimize" and "simulate".
288+
#' @return The adjusted coverage parameter yielding the desired simultaneous
289+
#' coverage of the ECDF traces.
290+
#' @noRd
280291
adjust_gamma <- function(N,
281292
L = 1,
282293
K = N,
@@ -298,11 +309,7 @@ adjust_gamma <- function(N,
298309
gamma
299310
}
300311

301-
# Adjust coverage parameter to find simultaneous confidence intervals for the
302-
# ECDF of a sample from the uniform distribution.
303-
# N - length of samples
304-
# K - number of equally spaced evaluation points, i.e. the right ends of the
305-
# partition intervals.
312+
# Adjust coverage parameter for single sample using the optimization method.
306313
adjust_gamma_optimize <- function(N, K, prob=0.99) {
307314
target <- function(gamma, prob, N, K) {
308315
z <- 1:(K - 1) / K
@@ -321,7 +328,7 @@ adjust_gamma_optimize <- function(N, K, prob=0.99) {
321328
for (i in seq_along(z1)) {
322329
tmp <- p_interior(
323330
p_int, x1 = x1, x2 = x2_lower[i]: x2_upper[i],
324-
z1 = z1[i], z2 = z2[i], gamma = gamma, N = N
331+
z1 = z1[i], z2 = z2[i], N = N
325332
)
326333
x1 <- tmp$x1
327334
p_int <- tmp$p_int
@@ -331,21 +338,14 @@ adjust_gamma_optimize <- function(N, K, prob=0.99) {
331338
optimize(target, c(0, 1 - prob), prob, N = N, K = K)$minimum
332339
}
333340

334-
# Adjust coverage parameter to find silmultaneous confidence intervals for the
335-
# ECDFs of multiple samples (chains) from the uniform distribution.
336-
# N - length of samples (chains).
337-
# L - number of samples (chains).
338-
# K - number of equally spaced evaluation points, i.e. the right ends of the
339-
# partition intervals.
340-
# M - number of simulations used to determine the 'prob' middle quantile.
341-
#'
341+
# Adjust coverage parameter for multiple chains using simulation method.
342342
adjust_gamma_simulate <- function(N, L, K, prob = 0.99, M = 1000) {
343343
gamma <- numeric(M)
344344
z <- (1:(K - 1)) / K
345345
n <- N * (L - 1)
346346
k <- floor(z * N * L)
347347
for (m in seq_len(M)) {
348-
u <- replicate(L, runif(N)) %>% u_scale
348+
u <- u_scale(replicate(L, runif(N)))
349349
scaled_ecdfs <- apply(outer(u, z, "<="), c(2, 3), sum)
350350
gamma[m] <- 2 * min(
351351
apply(
@@ -359,7 +359,10 @@ adjust_gamma_simulate <- function(N, L, K, prob = 0.99, M = 1000) {
359359
alpha_quantile(gamma, 1 - prob)
360360
}
361361

362-
p_interior <- function(p_int, x1, x2, z1, z2, gamma, N) {
362+
#' A helper function for 'adjust_gamma_optimize' defining the probability that
363+
#' an ECDF stays within the supplied bounds between z1 and z2.
364+
#' @noRd
365+
p_interior <- function(p_int, x1, x2, z1, z2, N) {
363366
z_tilde <- (z2 - z1) / (1 - z1)
364367
N_tilde <- rep(N - x1, each = length(x2))
365368
p_int <- rep(p_int, each = length(x2))
@@ -369,9 +372,11 @@ p_interior <- function(p_int, x1, x2, z1, z2, gamma, N) {
369372
list(p_int = rowSums(p_x2_int), x1 = x2)
370373
}
371374

372-
# 100 * `alpha` percent of the trials are allowed to be rejected.
373-
# In case of ties, return the largest value dominating at most
374-
# 100 * (alpha + tol) percent of the values.
375+
#' A helper function for 'adjust_alpha_simulate'
376+
#' 100 * `alpha` percent of the trials are allowed to be rejected.
377+
#' In case of ties, return the largest value dominating at most
378+
#' 100 * (alpha + tol) percent of the values.
379+
#' @noRd
375380
alpha_quantile <- function(gamma, alpha, tol = 0.001) {
376381
a <- unname(quantile(gamma, probs = alpha))
377382
a_tol <- unname(quantile(gamma, probs = alpha + tol))
@@ -384,17 +389,17 @@ alpha_quantile <- function(gamma, alpha, tol = 0.001) {
384389
a
385390
}
386391

387-
# Compute simultaneous confidence intervals for one or more samples from the
388-
# standard uniform distribution.
389-
# N - sample length
390-
# L - number of samples
391-
# K - size of uniform partition defining the ECDF evaluation points.
392-
# gamma - coverage parameter for the marginal distribution (binomial for
393-
# one sample and hypergeometric for multiple rank transformed chains).
392+
#' Compute simultaneous confidence intervals with the given adjusted coverage
393+
#' parameter gamma.
394+
#' @param N Sample length.
395+
#' @param L Number of MCMC-chains. (1 for ppc)
396+
#' @param K Number of uniformly spaced evaluation points.
397+
#' @param gamma Adjusted coverage parameter for the marginal distribution
398+
#' (binomial for PIT values and hypergeometric for rank transformed chains).
394399
#' @noRd
395400
ecdf_intervals <- function(N, L=1, K, gamma) {
396401
lims <- list()
397-
z <- seq(0,1, length.out = K + 1)
402+
z <- seq(0, 1, length.out = K + 1)
398403
if (L == 1) {
399404
lims$lower <- qbinom(gamma / 2, N, z)
400405
lims$upper <- qbinom(1 - gamma / 2, N, z)
@@ -407,7 +412,8 @@ ecdf_intervals <- function(N, L=1, K, gamma) {
407412
lims
408413
}
409414

410-
#' Transform observations in 'x' into their corresponding fractional ranks.
415+
#' Helper for 'adjust_gamma_simulate`
416+
#' Transforms observations in 'x' into their corresponding fractional ranks.
411417
#' @noRd
412418
u_scale <- function(x) {
413419
array(rank(x) / length(x), dim = dim(x), dimnames = dimnames(x))

0 commit comments

Comments
 (0)