Nothing
# Main fitting function and methods.
combined_bound <- function(z = NULL, p = NULL, alpha = 0.05, block_size = 1,
equal_blocks = FALSE) {
if (is.null(z) == is.null(p)) stop("give either z or p, not both")
if (!is.null(z)) {
if (!is.numeric(z)) stop("z must be numeric")
if (anyNA(z)) stop("z must not contain missing values")
labels <- names(z)
u <- -as.numeric(z)
} else {
if (!is.numeric(p)) stop("p must be numeric")
if (anyNA(p)) stop("p must not contain missing values")
if (any(p < 0 | p > 1)) stop("p-values must lie between 0 and 1")
labels <- names(p)
u <- stats::qnorm(as.numeric(p))
}
m <- length(u)
if (m < 2L) stop("at least two hypotheses are needed")
if (!is.numeric(alpha) || length(alpha) != 1L || is.na(alpha) || alpha <= 0 || alpha >= 1)
stop("alpha must be a single number between 0 and 1")
if (!is.numeric(block_size) || length(block_size) != 1L || is.na(block_size) ||
block_size < 1 || block_size != round(block_size))
stop("block_size must be a positive whole number")
if (block_size > m) stop("block_size cannot exceed the number of hypotheses")
if (!is.logical(equal_blocks) || length(equal_blocks) != 1L || is.na(equal_blocks))
stop("equal_blocks must be TRUE or FALSE")
b <- as.integer(block_size)
g <- .grids(m)
level <- alpha / 4
L <- b * log(2 * (length(g$C) + length(g$A)) / level)
us <- sort(u)
if (b == 1L) {
cutoff <- stats::qnorm(-expm1(log1p(-level) / m), lower.tail = FALSE)
} else {
cutoff <- stats::qnorm(level / m, lower.tail = FALSE)
}
comp <- c(
cutoff = min(m, sum(u > -cutoff)),
max_p = .max_p(u, level, b),
threshold_bernstein = .threshold_bernstein(us, g$C, L),
threshold_chernoff = if (b == 1L || equal_blocks) .threshold_chernoff(us, g$C, L) else NA_real_,
centered = .centered(u, g, L)
)
N <- min(comp, na.rm = TRUE)
false_all <- min(m, floor(N + 1e-9))
fit <- list(
m = m, alpha = alpha, block_size = b, equal_blocks = equal_blocks,
z = -u, p_value = stats::pnorm(u), labels = labels,
components = comp, bound = N, false_all = false_all,
z_cutoff = cutoff,
.u = u, .cutoff = cutoff, .Q = g$Q, .LT = b * log(length(g$Q) / level),
.order = order(u)
)
fit$.false_ranked <- .ranked(fit)
# hypotheses whose selection on their own carries no false discovery
pv <- fit$p_value
single <- rep(Inf, m)
zeta <- N * fit$.Q + sqrt(2 * N * fit$.Q * fit$.LT) + fit$.LT / 3
for (j in seq_along(fit$.Q)) single <- pmin(single, (pv > fit$.Q[j]) + zeta[j])
fit$significant <- (false_all == 0) | (u <= -cutoff) | (floor(single + 1e-9) == 0)
fit$call <- match.call()
class(fit) <- "combined_bound"
fit
}
.fmt_int <- function(x) formatC(x, format = "d", big.mark = ",")
.print_ranked <- function(x, k) {
f <- x$.false_ranked[k]
tab <- data.frame(k = .fmt_int(k), true = .fmt_int(k - f),
fdp = formatC(f / k, format = "f", digits = 3))
names(tab) <- c("Top k", "True discoveries, at least", "FDP, at most")
print(tab, row.names = FALSE, right = TRUE)
}
print.combined_bound <- function(x, ...) {
m <- x$m
conf <- format(100 * (1 - x$alpha), digits = 6)
cat("Post hoc bounds on true discoveries\n\n")
cat(sprintf("Hypotheses: %s one-sided Gaussian tests\n", .fmt_int(m)))
cat(sprintf("Confidence: %s%%, simultaneously for every selection\n", conf))
if (x$block_size > 1L)
cat(sprintf("Dependence: independent blocks of at most %d hypotheses\n", x$block_size))
cat("\n")
cat(sprintf("All %s hypotheses together contain\n", .fmt_int(m)))
cat(sprintf(" at least %s true discoveries\n", .fmt_int(m - x$false_all)))
cat(sprintf(" at most %s false discoveries (FDP at most %s)\n", .fmt_int(x$false_all),
formatC(x$false_all / m, format = "f", digits = 3)))
cat(sprintf("Significant individually (familywise error rate at most %s): %s\n\n",
format(x$alpha), .fmt_int(sum(x$significant))))
cat("The k most significant hypotheses:\n")
k <- 1
while (10 * k[length(k)] < m) k <- c(k, 10 * k[length(k)])
k <- c(k, m)
.print_ranked(x, k)
cat("\nUse true_discoveries() for other selections and summary() for details.\n")
invisible(x)
}
summary.combined_bound <- function(object, ...) {
comp <- object$components
out <- list(m = object$m, alpha = object$alpha, block_size = object$block_size,
components = comp, bound = object$bound, false_all = object$false_all,
z_cutoff = object$z_cutoff, n_significant = sum(object$significant),
ranked = ranked_bounds(object))
class(out) <- "summary.combined_bound"
out
}
print.summary.combined_bound <- function(x, ...) {
m <- x$m
level <- x$alpha / 4
cat("Post hoc bounds on true discoveries: details\n\n")
cat(sprintf("Hypotheses: %s; alpha = %s; block size %d\n\n", .fmt_int(m),
format(x$alpha), x$block_size))
cat(sprintf("Bounds on the number of false discoveries among all hypotheses\n(each valid with probability at least %s)\n",
format(1 - level)))
lab <- c(cutoff = if (x$block_size == 1L) "Sidak cutoff" else "Bonferroni cutoff",
max_p = "Closed testing, largest p-value",
threshold_bernstein = "Threshold counts, Bernstein",
threshold_chernoff = "Threshold counts, Chernoff",
centered = "Centered statistic")
val <- x$components
txt <- ifelse(is.na(val), "needs equal blocks",
ifelse(val == round(val), .fmt_int(round(val)),
formatC(val, format = "f", digits = 1, big.mark = ",")))
comp <- unname(lab[names(val)])
comp <- formatC(comp, width = max(nchar(comp)), flag = "-")
tab <- data.frame(Component = comp, Bound = txt)
names(tab)[1] <- formatC("Component", width = max(nchar(comp)), flag = "-")
print(tab, row.names = FALSE, right = TRUE)
cat(sprintf("\nCombined bound (smallest of the above): %s\n",
formatC(x$bound, format = "f", digits = 1, big.mark = ",")))
cat(sprintf("All %s hypotheses: at most %s false and at least %s true discoveries\n",
.fmt_int(m), .fmt_int(x$false_all), .fmt_int(m - x$false_all)))
cat(sprintf("\nCutoff for individual significance: z >= %s (%s, level alpha/4)\n",
formatC(x$z_cutoff, format = "f", digits = 3),
if (x$block_size == 1L) "Sidak" else "Bonferroni"))
cat(sprintf("Significant individually (familywise error rate at most %s): %s\n",
format(x$alpha), .fmt_int(x$n_significant)))
cat("\nFor a selection, the bound also uses the number of selected hypotheses below the\n",
"cutoff and the selected p-values (reference family bound, level alpha/4).\n", sep = "")
invisible(x)
}
plot.combined_bound <- function(x, log = "", xlab = "Number of most significant hypotheses (k)",
ylab = "True discoveries, at least", main = NULL, ...) {
k <- seq_len(x$m)
td <- k - x$.false_ranked
if (is.null(main)) main <- sprintf("Confidence envelope (%s%%)",
format(100 * (1 - x$alpha), digits = 6))
if (grepl("y", log)) {
keep <- td > 0
graphics::plot(k[keep], td[keep], type = "s", log = log, xlab = xlab, ylab = ylab,
main = main, ...)
} else {
graphics::plot(k, td, type = "s", log = log, xlab = xlab, ylab = ylab, main = main,
ylim = c(0, x$m), ...)
}
graphics::lines(k, k, lty = 2, col = "grey50")
invisible(x)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.