Skip to content

Commit

Permalink
Draft report_s() (#384)
Browse files Browse the repository at this point in the history
* Draft `report_s()`

* test, fix

* pkgdown

* check args

* tests

* Update R/report_s.R

Co-authored-by: Brenton M. Wiernik <[email protected]>

* Update R/report_s.R

Co-authored-by: Brenton M. Wiernik <[email protected]>

* Update R/report_s.R

Co-authored-by: Brenton M. Wiernik <[email protected]>

* fix

---------

Co-authored-by: Brenton M. Wiernik <[email protected]>
Co-authored-by: Rémi Thériault <[email protected]>
  • Loading branch information
3 people authored Dec 7, 2023
1 parent b36b657 commit 16afa3b
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ Collate:
'report_performance.R'
'report_priors.R'
'report_random.R'
'report_s.R'
'report_sample.R'
'report_statistics.R'
'report_table.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ export(report_participants)
export(report_performance)
export(report_priors)
export(report_random)
export(report_s)
export(report_sample)
export(report_statistics)
export(report_story)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# report 0.5.8

New features

* `report_s()` to report the interpretation of S- and p-values in an easy-to-understand
language.

Major Changes

* This release changes the licensing model of `{report}` to an MIT license.
Expand Down
38 changes: 38 additions & 0 deletions R/report_s.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#' Report S- and p-values in easy language.
#'
#' Reports interpretation of S- and p-values in easy language.
#'
#' @param s An S-value. Either `s` or `p` must be provided.
#' @param p A p-value. Either `s` or `p` must be provided.
#' @param test_value The value of the test parameter under the null hypothesis.
#' @param test_parameter The name of the test parameter under the null hypothesis.
#'
#' @return A string with the interpretation of the S- or p-value.
#'
#' @examples
#' report_s(s = 1.5)
#' report_s(p = 0.05)
#' @export
report_s <- function(s = NULL, p = NULL, test_value = 0, test_parameter = "parameter") {
# sanity check arguments
if ((is.null(s) || all(is.na(s))) && (is.null(p) || all(is.na(p)))) {
insight::format_error("You must provide either `s` or `p`.")
}
if (length(s) > 1 || length(p) > 1) {
insight::format_error("You must provide a single value for `s` or `p`.")
}
# make sure we have both s and p
if (!is.null(p) && !is.na(p)) {
s <- -log2(p)
} else {
p <- 2^(-s)
}
all_heads <- round(s)
chance <- sprintf("%.2g", 100 * p)
msg <- paste0(
"If the test hypothesis (", test_parameter, " = ", test_value, ") and all model assumptions were true, ",
"there is a ", chance, "% chance of observing this outcome. How weird is that? ",
"It's hardly more surprising than getting ", all_heads, " heads in a row with fair coin tosses."
)
insight::format_alert(msg)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ reference:
- report_performance
- report_priors
- report_random
- report_s
- report_sample
- report_statistics

Expand Down
27 changes: 27 additions & 0 deletions man/report_s.Rd

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

20 changes: 20 additions & 0 deletions tests/testthat/_snaps/windows/report_s.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# report_s

Code
report_s(s = 4.2)
Message <simpleMessage>
If the test hypothesis (parameter = 0) and all model assumptions were
true, there is a 5.4% chance of observing this outcome. How weird is
that? It's hardly more surprising than getting 4 heads in a row with
fair coin tosses.

---

Code
report_s(p = 0.06)
Message <simpleMessage>
If the test hypothesis (parameter = 0) and all model assumptions were
true, there is a 6% chance of observing this outcome. How weird is that?
It's hardly more surprising than getting 4 heads in a row with fair coin
tosses.

9 changes: 9 additions & 0 deletions tests/testthat/test-report_s.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test_that("report_s", {
expect_snapshot(report_s(s = 4.2), variant = "windows")
expect_snapshot(report_s(p = 0.06), variant = "windows")
})

test_that("report_s, arguments", {
expect_error(report_s())
expect_error(report_s(s = 1:2), "single value")
})

0 comments on commit 16afa3b

Please sign in to comment.