From d0d241cadc35ecc97c400f635200bd5c63f7f62d Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 6 Nov 2024 23:22:57 +0100 Subject: [PATCH] fix --- R/format_bf.R | 12 ++++++++---- R/format_p.R | 10 ++++++---- man/format_bf.Rd | 11 +++++++++-- man/format_p.Rd | 3 ++- man/format_pd.Rd | 3 ++- tests/testthat/test-format.R | 4 ++-- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/R/format_bf.R b/R/format_bf.R index 29f9686a3c..ac3d63e46d 100644 --- a/R/format_bf.R +++ b/R/format_bf.R @@ -6,6 +6,9 @@ #' @param exact Should very large or very small values be reported with a #' scientific format (e.g., 4.24e5), or as truncated values (as "> 1000" and #' "< 1/1000"). +#' @param inferiority_star String, indicating the symbol that is used to +#' indicate inferiority, i.e. when the Bayes Factor is smaller than one third +#' (the thresholds are smaller than one third, 1/10 and 1/30). #' @inheritParams format_p #' #' @return A formatted string. @@ -22,6 +25,7 @@ format_bf <- function(bf, stars = FALSE, stars_only = FALSE, + inferiority_star = "°", name = "BF", protect_ratio = FALSE, na_reference = NA, @@ -84,13 +88,13 @@ format_bf <- function(bf, ) ## Add stars - bf_text <- ifelse(bf_orig < (1 / 30), paste0(bf_text, "\u00B0\u00B0\u00B0"), - ifelse(bf_orig < 0.1, paste0(bf_text, "\u00B0\u00B0"), # nolint - ifelse(bf_orig < (1 / 3), paste0(bf_text, "\u00B0"), bf_text) # nolint + bf_text <- ifelse(bf_orig < (1 / 30), paste0(bf_text, paste(rep_len(inferiority_star, 3), collapse = "")), # nolint + ifelse(bf_orig < 0.1, paste0(bf_text, paste(rep_len(inferiority_star, 2), collapse = "")), # nolint + ifelse(bf_orig < (1 / 3), paste0(bf_text, inferiority_star), bf_text) # nolint ) ) - out <- .add_prefix_and_remove_stars(p_text = bf_text, stars, stars_only, name) + out <- .add_prefix_and_remove_stars(p_text = bf_text, stars, stars_only, name, inferiority_star) if (is.na(na_reference)) out[bad_bf] <- "" out } diff --git a/R/format_p.R b/R/format_p.R index 42e45c9a90..432858a0e5 100644 --- a/R/format_p.R +++ b/R/format_p.R @@ -3,7 +3,8 @@ #' Format p-values. #' #' @param p value or vector of p-values. -#' @param stars Add significance stars (e.g., p < .001***). +#' @param stars Add significance stars (e.g., p < .001***). For Bayes factors, +#' the thresholds for "significant" results are values larger than 3, 10, and 30. #' @param stars_only Return only significance stars. #' @param whitespace Logical, if `TRUE` (default), preserves whitespaces. Else, #' all whitespace characters are removed from the returned string. @@ -111,7 +112,8 @@ format_p <- function(p, name, missing = "", whitespace = TRUE, - decimal_separator = NULL) { + decimal_separator = NULL, + inferiority_star = "°") { missing_index <- is.na(p_text) if (is.null(name)) { @@ -121,10 +123,10 @@ format_p <- function(p, } if (stars_only) { - p_text <- gsub("[^(\\*|°)]", "", p_text) + p_text <- gsub(paste0("[^(\\*|", inferiority_star, ")]"), "", p_text) } else if (!stars) { p_text <- gsub("*", "", p_text, fixed = TRUE) - p_text <- gsub("\u00B0", "", p_text, fixed = TRUE) + p_text <- gsub(inferiority_star, "", p_text, fixed = TRUE) } # replace missing with related string diff --git a/man/format_bf.Rd b/man/format_bf.Rd index ca5e77f294..75dbc0f019 100644 --- a/man/format_bf.Rd +++ b/man/format_bf.Rd @@ -8,6 +8,7 @@ format_bf( bf, stars = FALSE, stars_only = FALSE, + inferiority_star = "°", name = "BF", protect_ratio = FALSE, na_reference = NA, @@ -17,10 +18,15 @@ format_bf( \arguments{ \item{bf}{Bayes Factor.} -\item{stars}{Add significance stars (e.g., p < .001***).} +\item{stars}{Add significance stars (e.g., p < .001***). For Bayes factors, +the thresholds for "significant" results are values larger than 3, 10, and 30.} \item{stars_only}{Return only significance stars.} +\item{inferiority_star}{String, indicating the symbol that is used to +indicate inferiority, i.e. when the Bayes Factor is smaller than one third +(the thresholds are smaller than one third, 1/10 and 1/30).} + \item{name}{Name prefixing the text. Can be \code{NULL}.} \item{protect_ratio}{Should values smaller than 1 be represented as ratios?} @@ -38,7 +44,8 @@ A formatted string. Bayes Factor formatting } \examples{ -format_bf(bfs <- c(0.000045, 0.033, NA, 1557, 3.54)) +bfs <- c(0.000045, 0.033, NA, 1557, 3.54) +format_bf(bfs) format_bf(bfs, exact = TRUE, name = NULL) format_bf(bfs, stars = TRUE) format_bf(bfs, protect_ratio = TRUE) diff --git a/man/format_p.Rd b/man/format_p.Rd index ea8a24e8e2..25e030e9e7 100644 --- a/man/format_p.Rd +++ b/man/format_p.Rd @@ -19,7 +19,8 @@ format_p( \arguments{ \item{p}{value or vector of p-values.} -\item{stars}{Add significance stars (e.g., p < .001***).} +\item{stars}{Add significance stars (e.g., p < .001***). For Bayes factors, +the thresholds for "significant" results are values larger than 3, 10, and 30.} \item{stars_only}{Return only significance stars.} diff --git a/man/format_pd.Rd b/man/format_pd.Rd index 7d34e75d62..94136d7b59 100644 --- a/man/format_pd.Rd +++ b/man/format_pd.Rd @@ -9,7 +9,8 @@ format_pd(pd, stars = FALSE, stars_only = FALSE, name = "pd") \arguments{ \item{pd}{Probability of direction (pd).} -\item{stars}{Add significance stars (e.g., p < .001***).} +\item{stars}{Add significance stars (e.g., p < .001***). For Bayes factors, +the thresholds for "significant" results are values larger than 3, 10, and 30.} \item{stars_only}{Return only significance stars.} diff --git a/tests/testthat/test-format.R b/tests/testthat/test-format.R index a135ff090c..87d5263840 100644 --- a/tests/testthat/test-format.R +++ b/tests/testthat/test-format.R @@ -111,11 +111,11 @@ test_that("format others", { c("< 0.001", "0.033", "", "> 1000", "3.54") ) expect_identical( - format_bf(c(0.000045, 0.033, NA, 1557, 3.54), stars = TRUE), + format_bf(c(0.000045, 0.233, NA, 1557, 3.54), stars = TRUE), c("BF < 0.001°°°", "BF = 0.233°", "", "BF > 1000***", "BF = 3.54*") ) expect_identical( - format_bf(c(0.000045, 0.033, NA, 1557, 3.54), stars = TRUE, stars_only = TRUE), + format_bf(c(0.000045, 0.233, NA, 1557, 3.54), stars = TRUE, stars_only = TRUE), c("°°°", "°", "", "***", "*") ) expect_identical(