R/selections.R

Defines functions select_by_fdp hypothesis_table ranked_bounds true_discoveries .check_fit

Documented in hypothesis_table ranked_bounds select_by_fdp true_discoveries

# Bounds for selections, for the most significant hypotheses, and per hypothesis.

.check_fit <- function(fit) {
  if (!inherits(fit, "combined_bound")) stop("fit must be the result of combined_bound()")
}

true_discoveries <- function(fit, select = NULL, by = NULL, details = FALSE) {
  .check_fit(fit)
  if (!is.null(by)) {
    if (!is.null(select)) stop("give either select or by, not both")
    if (length(by) != fit$m) stop("by must have one value per hypothesis")
    if (anyNA(by)) stop("by must not contain missing values")
    select <- split(seq_len(fit$m), by)
  }
  if (is.null(select)) select <- list(all = seq_len(fit$m))
  if (!is.list(select)) select <- list(select)
  nm <- names(select)
  if (is.null(nm)) nm <- rep("", length(select))
  nm[nm == ""] <- paste0("selection ", seq_along(select))[nm == ""]
  parts <- vapply(select, function(s) {
    idx <- .as_index(s, fit)
    c(selected = length(idx), .false_parts(fit, idx))
  }, numeric(5))
  parts <- matrix(parts, nrow = 5)
  out <- data.frame(
    selection = nm,
    selected = as.integer(parts[1, ]),
    true_at_least = as.integer(parts[1, ] - parts[2, ]),
    false_at_most = as.integer(parts[2, ]),
    fdp_at_most = ifelse(parts[1, ] > 0, parts[2, ] / parts[1, ], 0),
    stringsAsFactors = FALSE
  )
  if (details) {
    out$from_all <- as.integer(parts[3, ])
    out$from_cutoff <- as.integer(parts[4, ])
    out$from_pvalues <- as.integer(parts[5, ])
  }
  out
}

ranked_bounds <- function(fit, k = NULL) {
  .check_fit(fit)
  m <- fit$m
  if (is.null(k)) k <- seq_len(m)
  if (!is.numeric(k) || anyNA(k) || any(k != round(k)) || any(k < 1) || any(k > m))
    stop("k must contain whole numbers between 1 and ", m)
  k <- as.integer(k)
  f <- fit$.false_ranked[k]
  data.frame(k = k, z_k = fit$z[fit$.order[k]], true_at_least = as.integer(k - f),
             false_at_most = as.integer(f), fdp_at_most = f / k)
}

hypothesis_table <- function(fit, sort = FALSE) {
  .check_fit(fit)
  m <- fit$m
  rank <- integer(m)
  rank[fit$.order] <- seq_len(m)
  r <- fit$.false_ranked / seq_len(m)
  fdp <- rev(cummin(rev(r)))
  out <- data.frame(
    hypothesis = if (is.null(fit$labels)) seq_len(m) else fit$labels,
    z = fit$z, p_value = fit$p_value, rank = rank,
    significant = fit$significant, fdp_at_most = fdp[rank],
    stringsAsFactors = FALSE
  )
  if (sort) {
    out <- out[fit$.order, , drop = FALSE]
    rownames(out) <- NULL
  }
  out
}

select_by_fdp <- function(fit, fdp = 0.1) {
  .check_fit(fit)
  if (!is.numeric(fdp) || length(fdp) != 1L || is.na(fdp) || fdp < 0 || fdp > 1)
    stop("fdp must be a single number between 0 and 1")
  r <- fit$.false_ranked / seq_len(fit$m)
  ok <- which(r <= fdp)
  if (length(ok) == 0L) return(integer(0))
  idx <- fit$.order[seq_len(max(ok))]
  if (!is.null(fit$labels)) names(idx) <- fit$labels[idx]
  idx
}

Try the posthocbounds package in your browser

Any scripts or data that you put into this service are public.

posthocbounds documentation built on Nov. 28, 2026, 1:08 a.m.