Skip to content

Commit

Permalink
add p0 to oddsratio_to_d
Browse files Browse the repository at this point in the history
  • Loading branch information
mattansb committed Dec 5, 2024
1 parent ff82079 commit 31b5c94
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 37 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: effectsize
Title: Indices of Effect Size
Version: 0.8.9
Version: 0.8.9.1
Authors@R:
c(person(given = "Mattan S.",
family = "Ben-Shachar",
Expand Down Expand Up @@ -105,7 +105,7 @@ VignetteBuilder:
Encoding: UTF-8
Language: en-US
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Config/testthat/edition: 3
Config/testthat/parallel: true
Config/Needs/website:
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# effectsize 0.8.9.xxx

## New features

- `oddsratio_to_d()` and related functions gain a `p0` argument for exact conversion between odds ratios and Cohen's _d_.

# effectsize 0.8.9

## Bug fixes
Expand Down
47 changes: 29 additions & 18 deletions R/convert_between_d_to_r.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @param d,r,OR,logOR Standardized difference value (Cohen's d), correlation
#' coefficient (r), Odds ratio, or logged Odds ratio.
#' @param n1,n2 Group sample sizes. If either is missing, groups are assumed to be of equal size.
#' @param p0 Baseline risk. If not specified, the _d_ to _OR_ conversion uses am approximation (see details).
#' @param log Take in or output the log of the ratio (such as in logistic models),
#' e.g. when the desired input or output are log odds ratios instead odds ratios.
#' @param ... Arguments passed to or from other methods.
Expand Down Expand Up @@ -70,20 +71,32 @@ r_to_d <- function(r, n1, n2, ...) {

#' @rdname d_to_r
#' @export
oddsratio_to_d <- function(OR, log = FALSE, ...) {
oddsratio_to_d <- function(OR, p0, log = FALSE, ...) {
if (missing(p0)) {
# Use approximation
if (log) {
log_OR <- OR
} else {
log_OR <- log(OR)
}

return(log_OR * (sqrt(3) / pi))
}


if (log) {
log_OR <- OR
} else {
log_OR <- log(OR)
OR <- exp(OR)
}

log_OR * (sqrt(3) / pi)
odds1 <- OR * probs_to_odds(p0)
p1 <- odds_to_probs(odds1)
qnorm(p1) - qnorm(p0)
}

#' @rdname d_to_r
#' @export
logoddsratio_to_d <- function(logOR, log = TRUE, ...) {
oddsratio_to_d(logOR, log = log, ...)
logoddsratio_to_d <- function(logOR, p0, log = TRUE, ...) {
oddsratio_to_d(logOR, p0, log = log, ...)
}

#' @rdname d_to_r
Expand Down Expand Up @@ -111,14 +124,14 @@ d_to_logoddsratio <- function(d, log = TRUE, ...) {

#' @rdname d_to_r
#' @export
oddsratio_to_r <- function(OR, n1, n2, log = FALSE, ...) {
d_to_r(oddsratio_to_d(OR, log = log), n1, n2)
oddsratio_to_r <- function(OR, p0, n1, n2, log = FALSE, ...) {
d_to_r(oddsratio_to_d(OR, p0, log = log), n1, n2)
}

#' @rdname d_to_r
#' @export
logoddsratio_to_r <- function(logOR, log = TRUE, ...) {
oddsratio_to_r(logOR, log = log, ...)
logoddsratio_to_r <- function(logOR, p0, n1, n2, log = TRUE, ...) {
oddsratio_to_r(logOR, p0, n1, n2, log = log, ...)
}


Expand All @@ -140,13 +153,11 @@ r_to_logoddsratio <- function(r, n1, n2, log = TRUE, ...) {
#' @keywords internal
.get_rd_h <- function(n1, n2) {
if (missing(n1) && missing(n2)) {
h <- 4
} else {
if (missing(n1)) n1 <- n2
if (missing(n2)) n2 <- n1
m <- n1 + n2 - 2
h <- m / n1 + m / n2
return(4)
}

h
if (missing(n1)) n1 <- n2
if (missing(n2)) n2 <- n1
m <- n1 + n2 - 2
m / n1 + m / n2
}
10 changes: 6 additions & 4 deletions man/d_to_r.Rd

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

8 changes: 4 additions & 4 deletions man/effectsize.Rd

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

12 changes: 3 additions & 9 deletions man/effectsize_CIs.Rd

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

15 changes: 15 additions & 0 deletions tests/testthat/test-convert_between.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ test_that("oddsratio_to_d", {
expect_equal(d_to_oddsratio(-0.7994, log = TRUE), -1.45, tolerance = 0.01)
})

test_that("exact OR to d", {
d <- c(0, 0.2, 0.5, 0.8)
p0 <- pnorm(0, lower.tail = FALSE)
p1 <- pnorm(0, mean = d, lower.tail = FALSE)
OR <- probs_to_odds(p1) / probs_to_odds(p0)

expect_equal(cor(oddsratio_to_d(OR), d), 1, tolerance = 0.0001)
expect_equal(oddsratio_to_d(1), 0, tolerance = 0.0001)
expect_equal(oddsratio_to_d(OR, p0), d)

expect_equal(cor(oddsratio_to_r(OR), d_to_r(d)), 1, tolerance = 0.0002)
expect_equal(oddsratio_to_r(1), 0, tolerance = 0.0001)
expect_equal(oddsratio_to_r(OR, p0), d_to_r(d))
})


test_that("d_to_r", {
expect_equal(d_to_r(1.1547), 0.5, tolerance = 0.01)
Expand Down

0 comments on commit 31b5c94

Please sign in to comment.