Skip to content

Commit 21fc13c

Browse files
authored
add p0 to OR conversion
* Update convert_between_d_to_r.R * support interperting marix/array * add tests * remove chen2010 rule * fix tests * syler * lintr * fix example * lintr [skip]
1 parent 5ffe7ba commit 21fc13c

14 files changed

+200
-141
lines changed

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
## New features
44

55
- `oddsratio_to_d()` and related functions gain a `p0` argument for exact conversion between odds ratios and Cohen's _d_ (thanks @KohlRaphael for the suggestion).
6+
- `interpret*()` now accept (and return) matrices and arrays.
7+
8+
## Breaking Changes
9+
10+
- `interpret_oddsratio()` drops the default `"chen2010"` as it was used incorrectly (thanks to @KohlRaphael).
611

712
# effectsize 0.8.9
813

R/convert_between_d_to_r.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ r_to_d <- function(r, n1, n2, ...) {
7272
#' @rdname d_to_r
7373
#' @export
7474
oddsratio_to_d <- function(OR, p0, log = FALSE, ...) {
75-
if (missing(p0)) {
75+
if (missing(p0) || !is.numeric(p0)) {
7676
# Use approximation
7777
if (log) {
7878
log_OR <- OR
@@ -90,7 +90,7 @@ oddsratio_to_d <- function(OR, p0, log = FALSE, ...) {
9090

9191
odds1 <- OR * probs_to_odds(p0)
9292
p1 <- odds_to_probs(odds1)
93-
qnorm(p1) - qnorm(p0)
93+
stats::qnorm(p1) - stats::qnorm(p0)
9494
}
9595

9696
#' @rdname d_to_r

R/interpret.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ rules <- function(values, labels = NULL, name = NULL, right = TRUE) {
4343
insight::format_error("Too many labels for the number of reference values!")
4444
}
4545

46+
if (!is.numeric(values)) {
47+
insight::format_error("Reference values must be numeric.")
48+
}
49+
4650
if (length(values) == length(labels) - 1) {
4751
if (is.unsorted(values)) {
4852
insight::format_error("Reference values must be sorted.")
@@ -129,8 +133,8 @@ is.rules <- function(x) inherits(x, "rules")
129133
#' interpret(eta2, rules = "field2013")
130134
#'
131135
#' X <- chisq.test(mtcars$am, mtcars$cyl == 8)
132-
#' interpret(oddsratio(X), rules = "chen2010")
133-
#' interpret(cramers_v(X), "lovakov2021")
136+
#' interpret(oddsratio(X), rules = "cohen1988")
137+
#' interpret(cramers_v(X), rules = "lovakov2021")
134138
#' @export
135139
interpret <- function(x, ...) {
136140
UseMethod("interpret")
@@ -159,6 +163,9 @@ interpret.numeric <- function(x, rules, name = attr(rules, "rule_name"),
159163

160164
if (length(x_tran) > 1) {
161165
out <- vapply(x_tran, .interpret, rules = rules, FUN.VALUE = character(1L))
166+
if (is.matrix(x_tran) || is.array(x_tran)) {
167+
out <- structure(out, dim = dim(x_tran), dimnames = dimnames(x_tran))
168+
}
162169
} else {
163170
out <- .interpret(x_tran, rules = rules)
164171
}

R/interpret_bf.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ interpret_bf <- function(bf,
6969
)
7070
)
7171

72-
interpretation <- interpret(bf, rules, transform = function(.x) exp(abs(.x)))
72+
interpretation <- interpret(bf, rules, transform = function(.x) exp(ifelse(.x < 0, -.x, .x)))
7373
interpretation[bf == 0] <- "no"
7474

7575
# interpret direction
76-
dir <- interpret(bf, rules(0, c("against", "in favour of")))
77-
dir[bf == 0] <- "against or in favour of"
76+
direction <- interpret(bf, rules(0, c("against", "in favour of")))
77+
direction[bf == 0] <- "against or in favour of"
7878

7979
# Format text
8080
if (include_value) {
8181
bf_fmt <- insight::format_bf(exp(bf), protect_ratio = protect_ratio, exact = exact)
82-
interpretation[] <- sprintf("%s evidence (%s) %s", interpretation, bf_fmt, dir)
82+
interpretation[] <- sprintf("%s evidence (%s) %s", interpretation, bf_fmt, direction)
8383
} else {
84-
interpretation[] <- paste0(interpretation, " evidence ", dir)
84+
interpretation[] <- paste0(interpretation, " evidence ", direction)
8585
}
8686

8787
interpretation[is.na(bf)] <- ""

R/interpret_direction.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
#' @examples
77
#' interpret_direction(.02)
88
#' interpret_direction(c(.5, -.02))
9+
#' interpret_direction(0)
910
#'
1011
#' @keywords interpreters
1112
#' @export
1213
interpret_direction <- function(x) {
13-
interpret(x, rules(0, c("negative", "positive"), name = "math", right = FALSE))
14+
interpret(x, rules(0, c("negative", "positive"), name = "math", right = FALSE),
15+
transform = function(.x) {
16+
s <- sign(.x)
17+
replace(s, s == 0, NA_real_)
18+
}
19+
)
1420
}

R/interpret_oddsratio.R

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
#' Interpret Odds Ratio
22
#'
33
#' @param OR Value or vector of (log) odds ratio values.
4-
#' @param rules Can be "`chen2010"` (default), `"cohen1988"` (through
5-
#' transformation to standardized difference, see [oddsratio_to_d()]) or custom set
6-
#' of [rules()].
4+
#' @param rules If `"cohen1988"` (default), `OR` is transformed to a
5+
#' standardized difference (via [oddsratio_to_d()]) and interpreted according
6+
#' to Cohen's rules (see [interpret_cohens_d()]; see Chen et al., 2010). If a
7+
#' custom set of [rules()] is used, OR is interpreted as is.
78
#' @param log Are the provided values log odds ratio.
89
#' @inheritParams interpret
10+
#' @inheritParams oddsratio_to_d
911
#'
1012
#' @section Rules:
1113
#'
1214
#' Rules apply to OR as ratios, so OR of 10 is as extreme as a OR of 0.1 (1/10).
1315
#'
14-
#' - Chen et al. (2010) (`"chen2010"`; default)
15-
#' - **OR < 1.68** - Very small
16-
#' - **1.68 <= OR < 3.47** - Small
17-
#' - **3.47 <= OR < 6.71** - Medium
18-
#' - **OR >= 6.71 ** - Large
1916
#' - Cohen (1988) (`"cohen1988"`, based on the [oddsratio_to_d()] conversion, see [interpret_cohens_d()])
2017
#' - **OR < 1.44** - Very small
2118
#' - **1.44 <= OR < 2.48** - Small
2219
#' - **2.48 <= OR < 4.27** - Medium
23-
#' - **OR >= 4.27 ** - Large
20+
#' - **OR >= 4.27** - Large
2421
#'
2522
#' @examples
2623
#' interpret_oddsratio(1)
@@ -40,28 +37,15 @@
4037
#'
4138
#' @keywords interpreters
4239
#' @export
43-
interpret_oddsratio <- function(OR, rules = "chen2010", log = FALSE, ...) {
44-
if (log) {
45-
f_transform <- function(.x) exp(abs(.x))
46-
} else {
47-
f_transform <- function(.x) exp(abs(log(.x)))
48-
}
49-
50-
40+
interpret_oddsratio <- function(OR, rules = "cohen1988", p0 = NULL, log = FALSE, ...) {
5141
if (is.character(rules) && rules == "cohen1988") {
52-
d <- oddsratio_to_d(OR, log = log)
42+
d <- oddsratio_to_d(OR, p0, log = log)
5343
return(interpret_cohens_d(d, rules = rules))
5444
}
5545

56-
rules <- .match.rules(
57-
rules,
58-
list(
59-
chen2010 = rules(c(1.68, 3.47, 6.71), c("very small", "small", "medium", "large"),
60-
name = "chen2010", right = FALSE
61-
),
62-
cohen1988 = NA # for correct error msg
63-
)
64-
)
46+
if (log) {
47+
OR <- exp(OR)
48+
}
6549

66-
interpret(OR, rules, transform = f_transform)
50+
interpret(OR, rules, transform = function(.x) ifelse(.x < 1, 1 / .x, .x))
6751
}

R/interpret_rope.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#' Interpretation of
44
#'
55
#' @param rope Value or vector of percentages in ROPE.
6-
#' @param ci The Credible Interval (CI) probability, corresponding to the proportion of HDI, that was used. Can be `1` in the case of "full ROPE".
6+
#' @param ci The Credible Interval (CI) probability, corresponding to the
7+
#' proportion of HDI, that was used. Can be `1` in the case of "full ROPE".
78
#' @param rules A character string (see details) or a custom set of [rules()].
89
#'
910
#' @section Rules:
@@ -29,7 +30,7 @@
2930
#'
3031
#' @keywords interpreters
3132
#' @export
32-
interpret_rope <- function(rope, ci = 0.9, rules = "default") {
33+
interpret_rope <- function(rope, rules = "default", ci = 0.9) {
3334
if (ci < 1) {
3435
e <- .Machine$double.eps
3536

R/rank_ANOVA.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,12 @@ kendalls_w <- function(x, groups, blocks, data = NULL,
280280

281281
boot_fun <- function(.data, .i) {
282282
split(.data$x, .data$groups) <-
283-
lapply(split(.data$x, .data$groups),
283+
lapply(
284+
split(.data$x, .data$groups),
284285
function(v) {
285-
if (length(v) < 2L) return(v)
286+
if (length(v) < 2L) {
287+
return(v)
288+
}
286289
sample(v, size = length(v), replace = TRUE)
287290
}
288291
)

man/interpret.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/interpret_direction.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)