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 DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: modelbased
Title: Estimation of Model-Based Predictions, Contrasts and Means
Version: 0.17.0.8
Version: 0.17.0.9
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
now `predict = "prob"`, which returns the same probabilities as the
`"marginaleffects"` backend.

* `comparison = "ratio"` is a new shortcut to compute pairwise comparisons
as ratios.

## Bug fixes

* Fixed issue in `estimate_contrasts()` with wrong assignment of estimates in
Expand Down
12 changes: 8 additions & 4 deletions R/estimate_contrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#' described below, see documentation of [marginaleffects::comparisons],
#' [this website](https://marginaleffects.com/bonus/hypothesis.html) and
#' section _Comparison options_ below.
#' * String: One of `"pairwise"`, `"revpairwise"`, `"reference"`,
#' * String: One of `"pairwise"`, `"ratio"`, `"revpairwise"`, `"reference"`,
#' `"sequential"`, `"meandev"` `"meanotherdev"`, `"poly"`, `"helmert"`, or
#' `"trt_vs_ctrl"`. To test multiple hypotheses jointly (usually used for
#' factorial designs), `comparison` can also be `"joint"` or `"omnibus"`. In
Expand Down Expand Up @@ -116,11 +116,15 @@
#'
#' @section Comparison options:
#'
#' - `comparison = "pairwise"`: This method computes all possible unique
#' differences between pairs of levels of the focal predictor. For example, if
#' a factor has levels A, B, and C, it would compute A-B, A-C, and B-C.
#' - `comparison = "pairwise"` (default): This method computes all possible
#' unique differences between pairs of levels of the focal predictor. For
#' example, if a factor has levels A, B, and C, it would compute A-B, A-C, and
#' B-C. The formula notation would be `comparison = difference ~ pairwise`.
#' - `comparison = "revpairwise"`: Like `"pairwise"`, but reverses the order
#' of levels when comparing, e.g. B-A, C-A, and C-B.
#' - `comparison = "ratio"`: This method computes all possible unique ratios
#' between pairs of levels of the focal predictor (not differences, like the
#' default). It is basically a shortcut for `comparison = ratio ~ pairwise`.
#' - `comparison = "reference"`: This compares each level of the focal predictor
#' to a specified reference level (by default, the first level). For example,
#' if levels are A, B, C, and A is the reference, it computes B-A and C-A.
Expand Down
5 changes: 3 additions & 2 deletions R/format.R
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,9 @@ format.marginaleffects_contrasts <- function(
}

# check type of contrast
is_ratio_comparison <- inherits(comparison, "formula") &&
identical(deparse(comparison[[2]]), "ratio")
is_ratio_comparison <- (inherits(comparison, "formula") &&
identical(deparse(comparison[[2]]), "ratio")) ||
identical(comparison, "ratio")

# Column name for coefficient - fix needed for contrasting slopes and ratios
colnames(x)[colnames(x) == "Slope"] <- "Difference"
Expand Down
11 changes: 9 additions & 2 deletions R/get_marginalcontrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,14 @@ get_marginalcontrasts <- function(
}
# for some comparisons, we need an empty left-hand side. else, we default
# to "difference".
formula_lhs <- switch(comparison, poly = , helmert = "", "difference")
formula_rhs <- comparison
formula_lhs <- switch(
comparison,
poly = ,
helmert = "",
ratio = "ratio",
"difference"
)
formula_rhs <- switch(comparison, ratio = "pairwise", comparison)
}
if (omnibus_test) {
comparison <- NULL
Expand Down Expand Up @@ -553,6 +559,7 @@ get_marginalcontrasts <- function(
.valid_hypothesis_strings <- function() {
c(
"pairwise",
"ratio",
"reference",
"sequential",
"meandev",
Expand Down
12 changes: 8 additions & 4 deletions man/estimate_contrasts.Rd

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

2 changes: 1 addition & 1 deletion man/get_emmeans.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/test-estimate_contrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -1974,3 +1974,23 @@ test_that("estimate_contrast, don't let group labels appear in comparison labels
)
)
})


test_that("estimate_contrast, shortcut 'ratio' works", {
skip_if_not_installed("datawizard")
data(stigma, package = "modelbased")

stigma$symptoms_unreal <- datawizard::recode_into(
symptoms_unreal %in% c("strongly disagree", "disagree") ~ "(strongly) disagree",
symptoms_unreal %in% c("strongly agree", "agree") ~ "(strongly) agree",
data = stigma
)

stigma$symptoms_unreal <- as.factor(stigma$symptoms_unreal)
stigma$symptoms_unreal <- relevel(stigma$symptoms_unreal, ref = "(strongly) disagree")

m <- glm(symptoms_unreal ~ sex, data = stigma, family = binomial())
out <- estimate_contrasts(m, "sex", comparison = "ratio")
expect_named(out, c("Level1", "Level2", "Ratio", "SE", "CI_low", "CI_high", "z", "p"))
expect_equal(out$Ratio, 6.14634, tolerance = 1e-4)
Comment on lines +1994 to +1995

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exp(coef()) returns an odds ratio, while estimate_contrasts(m, "sex", comparison = "ratio") computes the ratio between two predicted probabilities, i.e. a risk ratio.

})
Loading