Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
`component` argument gains the `"full"` option, to return the full
variance-covariance matrix, including the random effects (theta parameters).

* `format_p()` now also gets a `lead_zero` argument, to keep or drop the leading
zero of a formatted p-value.

* `format_table()` now also formats ROPE columns for superiority and inferiority.

* `format_table()` protects integer columns for non-specific column types.
Expand Down
31 changes: 22 additions & 9 deletions R/format_p.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#' If `"scientific"`, control the number of digits by adding the value as
#' a suffix, e.g.m `digits = "scientific4"` to have scientific notation
#' with 4 decimal places.
#' @param lead_zero Logical, if `FALSE` (default), removes leading zero
#' before decimal separator. Otherwise, retain leading zero except when
#' p value is truncated
#' @param ... Arguments from other methods.
#' @inheritParams format_value
#'
Expand All @@ -40,6 +43,7 @@ format_p <- function(p,
missing = "",
decimal_separator = NULL,
digits = 3,
lead_zero = FALSE,
...) {
# only convert p if it's a valid numeric, or at least coercible to
# valid numeric values...
Expand All @@ -59,6 +63,15 @@ format_p <- function(p,
digits <- 3
}

# hard-coded thresholds for small p-values, including or excluding leading zero
if (lead_zero) {
low_p <- "< 0.001***"
high_p <- "> 0.999"
} else {
low_p <- "< .001***"
high_p <- "> .999"
}

if (is.character(digits) && grepl("scientific", digits, fixed = TRUE)) {
digits <- tryCatch(as.numeric(gsub("scientific", "", digits, fixed = TRUE)),
error = function(e) NA
Expand All @@ -79,22 +92,22 @@ format_p <- function(p,
)
} else if (digits <= 3) {
p_text <- ifelse(is.na(p), NA,
ifelse(p < 0.001, "< .001***", # nolint
ifelse(p < 0.01, paste0("= ", format_value(p, digits), "**"), # nolint
ifelse(p < 0.05, paste0("= ", format_value(p, digits), "*"), # nolint
ifelse(p > 0.999, "> .999", # nolint
paste0("= ", format_value(p, digits))
ifelse(p < 0.001, low_p, # nolint
ifelse(p < 0.01, paste0("= ", format_value(p, digits, lead_zero = lead_zero), "**"), # nolint
ifelse(p < 0.05, paste0("= ", format_value(p, digits, lead_zero = lead_zero), "*"), # nolint
ifelse(p > 0.999, high_p, # nolint
paste0("= ", format_value(p, digits, lead_zero = lead_zero))
)
)
)
)
)
} else {
p_text <- ifelse(is.na(p), NA,
ifelse(p < 0.001, paste0("= ", format_value(p, digits), "***"), # nolint
ifelse(p < 0.01, paste0("= ", format_value(p, digits), "**"), # nolint
ifelse(p < 0.05, paste0("= ", format_value(p, digits), "*"), # nolint
paste0("= ", format_value(p, digits))
ifelse(p < 0.001, paste0("= ", format_value(p, digits, lead_zero = lead_zero), "***"), # nolint
ifelse(p < 0.01, paste0("= ", format_value(p, digits, lead_zero = lead_zero), "**"), # nolint
ifelse(p < 0.05, paste0("= ", format_value(p, digits, lead_zero = lead_zero), "*"), # nolint
paste0("= ", format_value(p, digits, lead_zero = lead_zero))
)
)
)
Expand Down
5 changes: 5 additions & 0 deletions man/format_p.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/testthat/test-format.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
expect_identical(format_value(0.0000000123, digits = 8), "0.00000001")
expect_identical(format_value(c(0.012, 0.45, -0.03), lead_zero = FALSE), c(".01", ".45", "-.03"))
expect_identical(format_value(c(1.012, 0.45, -0.03), lead_zero = FALSE), c("1.01", ".45", "-.03"))
expect_identical(format_value(c(1.012, 0.45, -0.03), lead_zero = FALSE, decimal_point = ","), c("1,01", ",45", "-,03"))

Check warning on line 15 in tests/testthat/test-format.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-format.R,line=15,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 121 characters.
expect_identical(format_value(c(0.45, -0.03), style_positive = "plus"), c("+0.45", "-0.03"))
expect_identical(format_value(c(0.45, -0.03), style_positive = "plus", lead_zero = FALSE), c("+.45", "-.03"))
expect_equal(
Expand Down Expand Up @@ -44,7 +44,7 @@
test_that("format_ci", {
expect_identical(
format_ci(c(123, 123, 123, 123), c(123, 12345, 123456, 123456789012), width = "auto"),
c("95% CI [123.00, 123.00]", "95% CI [123.00, 12345.00]", "95% CI [123.00, 1.23e+05]", "95% CI [123.00, 1.23e+11]")

Check warning on line 47 in tests/testthat/test-format.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-format.R,line=47,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 121 characters.
)
expect_identical(
format_ci(c(123, 123, 123, 123), c(123, 12345, 123456, 123456789012), width = "auto", digits = 5),
Expand Down Expand Up @@ -143,6 +143,17 @@
expect_identical(nchar(format_p(0.02)), 9L)
expect_identical(nchar(format_p(0.02, stars = TRUE)), 10L)
expect_identical(nchar(format_p(0.02, stars_only = TRUE)), 1L)

expect_identical(format_p(0.02), "p = .020")
expect_identical(format_p(0.02, lead_zero = TRUE), "p = 0.020")
expect_identical(format_p(0.02, stars = TRUE), "p = .020*")
expect_identical(format_p(0.02, stars = TRUE, lead_zero = TRUE), "p = 0.020*")
expect_identical(format_p(0.0214568, digits = "apa"), "p = .021")
expect_identical(format_p(0.0214568, digits = "apa", lead_zero = TRUE), "p = 0.021")
expect_identical(format_p(0.00003, digits = "apa"), "p < .001")
expect_identical(format_p(0.00003, digits = "apa", lead_zero = TRUE), "p < 0.001")
expect_identical(format_p(0.00003, digits = 5), "p = .00003")
expect_identical(format_p(0.00003, digits = 5, lead_zero = TRUE), "p = 0.00003")
})

test_that("format_table, other CI columns", {
Expand Down
Loading