R/plot1DSigWindows.R

Defines functions plot1DSigWindows

Documented in plot1DSigWindows

#' Plot one-dimensional windows selected by Bayesian FDR
#'
#' Selects representative-tree windows whose posterior alternative
#' probabilities satisfy a requested Bayesian false discovery rate, then
#' displays them with \code{\link{plot1D}}.
#'
#' @param ans_mrs An \code{mrs} object.
#' @param fdr Desired Bayesian false discovery rate between zero and one.
#' @param precision Step size used to search posterior-probability thresholds.
#' @param ... Additional arguments passed to \code{\link{plot1D}}.
#' @return Invisibly, a list containing the selected node indices and threshold.
#' @export
plot1DSigWindows <- function(ans_mrs, fdr = 0.1, precision = 0.001, ...) {
  if (!inherits(ans_mrs, "mrs")) {
    stop("'ans_mrs' must be an 'mrs' object.", call. = FALSE)
  }
  fdr <- .validate_probability(fdr, "fdr")
  if (!is.numeric(precision) || length(precision) != 1L || is.na(precision) ||
      !is.finite(precision) || precision <= 0 || precision > 1) {
    stop("'precision' must be a number in (0, 1].", call. = FALSE)
  }

  thresholds <- unique(c(seq(0, 1, by = precision), 1))
  false_discovery_rates <- vapply(thresholds, function(threshold) {
    bFDR(ans_mrs, threshold)
  }, numeric(1L))
  valid <- which(false_discovery_rates <= fdr)
  threshold <- if (length(valid)) thresholds[min(valid)] else 1
  selected <- which(ans_mrs$RepresentativeTree$AltProbs > threshold)

  plotted <- ans_mrs
  excluded <- setdiff(seq_along(plotted$RepresentativeTree$AltProbs), selected)
  plotted$RepresentativeTree$AltProbs[excluded] <- 0
  plotted$RepresentativeTree$EffectSizes[excluded, ] <- 0
  plot1D(plotted, ...)

  invisible(list(indices = selected, threshold = threshold))
}

Try the MRS package in your browser

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

MRS documentation built on July 22, 2026, 5:10 p.m.