Skip to content
Merged
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
2 changes: 1 addition & 1 deletion R/convert_between_d_to_r.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ logoddsratio_to_r <- function(logOR, p0, n1, n2, log = TRUE, ...) {
#' @rdname d_to_r
#' @export
r_to_oddsratio <- function(r, n1, n2, log = FALSE, ...) {
d_to_oddsratio(r_to_d(r), log = log, n1, n2)
d_to_oddsratio(r_to_d(r, n1, n2), log = log)
}

#' @rdname d_to_r
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-convert_between.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,31 @@
r_to_logoddsratio(0.5),
tolerance = 1e-4
)

# n1 and n2 are forwarded to r_to_d()
expect_equal(
r_to_oddsratio(0.5, n1 = 10, n2 = 40),
d_to_oddsratio(r_to_d(0.5, n1 = 10, n2 = 40)),
tolerance = 1e-12
)
expect_equal(
r_to_logoddsratio(0.5, n1 = 10, n2 = 40),
d_to_logoddsratio(r_to_d(0.5, n1 = 10, n2 = 40)),
tolerance = 1e-12
)
expect_equal(
oddsratio_to_r(r_to_oddsratio(0.5, n1 = 10, n2 = 40), n1 = 10, n2 = 40),
0.5,
tolerance = 1e-12
)
expect_identical(
r_to_oddsratio(0.5, n1 = 10),
r_to_oddsratio(0.5, 10, 10)
)
expect_identical(
r_to_logoddsratio(0.5, n2 = 10),
r_to_logoddsratio(0.5, 10, 10)
)
})

# OR, RR, ARR, NNT -------------------------
Expand Down Expand Up @@ -156,14 +181,14 @@
# -- GLMs --
data(mtcars)

m1 <<- glm(am ~ factor(cyl), data = mtcars, family = binomial("logit"))

Check warning on line 184 in tests/testthat/test-convert_between.R

View workflow job for this annotation

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

file=tests/testthat/test-convert_between.R,line=184,col=6,[assignment_linter] Replace <<- by assigning to a specific environment (with assign() or <-) to avoid hard-to-predict behavior.

expect_warning(RR <- oddsratio_to_riskratio(m1, ci = NULL), "p0") # nolint
expect_false("(Intercept)" %in% RR$Parameter)
expect_true("(p0)" %in% RR$Parameter)

expect_warning(
RR <- oddsratio_to_riskratio(m1, ci_method = "wald", p0 = 0.7272727),

Check warning on line 191 in tests/testthat/test-convert_between.R

View workflow job for this annotation

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

file=tests/testthat/test-convert_between.R,line=191,col=5,[implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`.
NA
) # nolint
expect_false("(Intercept)" %in% RR$Parameter)
Expand All @@ -182,7 +207,7 @@
# -- GLMs2 --
data(mtcars)

m2 <<- glm(am ~ factor(cyl), data = mtcars, family = binomial("log"))

Check warning on line 210 in tests/testthat/test-convert_between.R

View workflow job for this annotation

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

file=tests/testthat/test-convert_between.R,line=210,col=6,[assignment_linter] Replace <<- by assigning to a specific environment (with assign() or <-) to avoid hard-to-predict behavior.

ORt <- parameters::model_parameters(m1, exp = TRUE)

Expand All @@ -191,7 +216,7 @@
expect_true("(p0)" %in% OR$Parameter)

expect_warning(
OR <- riskratio_to_oddsratio(m2, ci_method = "wald", p0 = 0.7272727),

Check warning on line 219 in tests/testthat/test-convert_between.R

View workflow job for this annotation

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

file=tests/testthat/test-convert_between.R,line=219,col=5,[implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`.
NA
) # nolint
expect_false("(Intercept)" %in% OR$Parameter)
Expand All @@ -209,7 +234,7 @@

# -- GLMMs --
skip_if_not_installed("lme4", minimum_version = "2.1.0")
m <<- lme4::glmer(

Check warning on line 237 in tests/testthat/test-convert_between.R

View workflow job for this annotation

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

file=tests/testthat/test-convert_between.R,line=237,col=5,[assignment_linter] Replace <<- by assigning to a specific environment (with assign() or <-) to avoid hard-to-predict behavior.
am ~ factor(cyl) + (1 | gear),
data = mtcars,
family = binomial(link = "logit"),
Expand All @@ -221,7 +246,7 @@
expect_true("(p0)" %in% RR$Parameter)

expect_no_warning(
RR <- oddsratio_to_riskratio(m, ci_method = "wald", p0 = 0.7047536)

Check warning on line 249 in tests/testthat/test-convert_between.R

View workflow job for this annotation

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

file=tests/testthat/test-convert_between.R,line=249,col=5,[implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`.
) # nolint
expect_false("(Intercept)" %in% RR$Parameter)
expect_true("(p0)" %in% RR$Parameter)
Expand Down Expand Up @@ -253,12 +278,12 @@
ARR <- p1 - p0
NNT <- arr_to_nnt(ARR)

expect_equal(riskratio_to_probs(RR, p0 = p0), p1)

Check warning on line 281 in tests/testthat/test-convert_between.R

View workflow job for this annotation

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

file=tests/testthat/test-convert_between.R,line=281,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
expect_equal(oddsratio_to_probs(OR, p0 = p0), p1)

Check warning on line 282 in tests/testthat/test-convert_between.R

View workflow job for this annotation

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

file=tests/testthat/test-convert_between.R,line=282,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.

expect_equal(nnt_to_probs(NNT, p0 = p0, odds = TRUE), probs_to_odds(p1))

Check warning on line 284 in tests/testthat/test-convert_between.R

View workflow job for this annotation

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

file=tests/testthat/test-convert_between.R,line=284,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.

expect_equal(arr_to_probs(-ARR, p0 = p1), p0)

Check warning on line 286 in tests/testthat/test-convert_between.R

View workflow job for this annotation

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

file=tests/testthat/test-convert_between.R,line=286,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
expect_equal(nnt_to_probs(-NNT, p0 = p1), p0)
})

Expand Down
Loading