Skip to content

Instantly share code, notes, and snippets.

@klmr
Created December 29, 2013 16:14
Show Gist options
  • Save klmr/8171954 to your computer and use it in GitHub Desktop.
Save klmr/8171954 to your computer and use it in GitHub Desktop.
Pretty-print p-values for plots in R
#' Print relevant digits of a p-value.
#'
#' The idea is, under the assumption that H0 holds, to print as many digits of
#' p as are strictly necessary to demonstrate H0. Turning this around, if we
#' need to print many digits this probably means that we can reject H0.
#'
#' @param pvalue a single p-value
#' @param max_precision limit the maximum number of leading zeros to display
#' @param p typographic representation of the variable “p”
pretty_p <- function (pvalue, max_precision = 5, p = bquote(italic(p))) {
required_precision <- ceiling(-log10(pvalue))
if (required_precision > max_precision)
bquote(.(p) < .(sprintf('%.*f', max_precision, 0.1 ** max_precision)))
else
bquote(.(p) %~~% .(sprintf('%.*f', required_precision + 1, pvalue)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment