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.19.0
Version: 0.19.0.1
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# modelbased (devel)

## Bug fixes

* `estimate_means()` with `backend = "emmeans"` now returns the probabilities
of each response category for ordinal and categorical models from *brms*.
Before, the results were wrong: emmeans averaged over the response
categories, so each probability was one divided by the number of categories.

# modelbased 0.19.0

## Breaking Changes
Expand Down
25 changes: 25 additions & 0 deletions R/get_emmeans.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ get_emmeans <- function(
dpars <- FALSE
fun_args$epred <- TRUE
fun_args$type <- "response"
# ordinal and categorical models: brms adds the response categories as
# pseudo-factor "rep.meas" to the reference grid. We add it to `specs`,
# else emmeans averages over it
if (.is_brms_ordinal_or_categorical(model)) {
fun_args$specs <- .add_emmeans_pseudo_factor(fun_args$specs, "rep.meas")
}
} else if (predict %in% .brms_aux_elements(model)) {
dpars <- TRUE
fun_args$dpar <- predict
Expand Down Expand Up @@ -175,6 +181,16 @@ get_emmeans <- function(
}


# ordinal and categorical models from brms
.is_brms_ordinal_or_categorical <- function(model) {
if (!inherits(model, "brmsfit")) {
return(FALSE)
}
m_info <- insight::model_info(model, response = 1, verbose = FALSE)
isTRUE(m_info$is_ordinal) || isTRUE(m_info$is_categorical)
}


# name of the pseudo-factor that emmeans adds to the reference grid for
# ordinal models: the response categories for `mode = "prob"`, and the
# thresholds for `mode = "cum.prob"`, `"exc.prob"` and `"linear.predictor"`.
Expand Down Expand Up @@ -294,6 +310,15 @@ get_emmeans <- function(
if (length(colums_to_add)) {
means <- cbind(em_grid[colums_to_add], means)
}
# brms ordinal and categorical models: the response categories
if (.is_brms_ordinal_or_categorical(model) && "rep.meas" %in% colnames(means)) {
new_name <- "Response"
# a predictor can have the same name, so we add a suffix ("Response.1")
if (new_name %in% colnames(means)) {
new_name <- make.unique(c(colnames(means), new_name))[ncol(means) + 1]
}
colnames(means)[colnames(means) == "rep.meas"] <- new_name
}
} else {
means <- as.data.frame(stats::confint(x, level = ci))
# the df column is not shown, but `pool_predictions()` needs the df
Expand Down
38 changes: 38 additions & 0 deletions tests/testthat/test-ordinal.R
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,44 @@ test_that("estimate_means, backend emmeans, clmm shows response level names", {
})


test_that("estimate_means, backend emmeans, brms ordinal", {
skip_if_not_installed("emmeans")
m <- insight::download_model("brms_ordinal_1")
skip_if(is.null(m))

# one probability per response category
out <- suppressWarnings(estimate_means(m, "mpg", length = 3, backend = "emmeans"))
expected <- as.data.frame(suppressWarnings(emmeans::emmeans(
m,
specs = c("mpg", "rep.meas"),
at = list(mpg = c(10.4, 22.15, 33.9)),
epred = TRUE
)))
expect_identical(as.character(out$Response), as.character(expected$rep.meas))
expect_equal(out$mpg, expected$mpg, tolerance = 1e-6)
expect_equal(out$Probability, expected$emmean, tolerance = 1e-6)
})


test_that("estimate_means, backend emmeans, brms categorical", {
skip_if_not_installed("emmeans")
m <- insight::download_model("brms_categorical_2_num")
skip_if(is.null(m))

# one probability per response category
out <- suppressWarnings(estimate_means(m, "Sepal.Width", length = 3, backend = "emmeans"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[air] reported by reviewdog 🐶

Suggested change
out <- suppressWarnings(estimate_means(m, "Sepal.Width", length = 3, backend = "emmeans"))
out <- suppressWarnings(estimate_means(
m,
"Sepal.Width",
length = 3,
backend = "emmeans"
))

expected <- as.data.frame(suppressWarnings(emmeans::emmeans(
m,
specs = c("Sepal.Width", "rep.meas"),
at = list(Sepal.Width = c(2, 3.2, 4.4)),
epred = TRUE
)))
expect_identical(as.character(out$Response), as.character(expected$rep.meas))
expect_equal(out$Sepal.Width, expected$Sepal.Width, tolerance = 1e-6)
expect_equal(out$Probability, expected$emmean, tolerance = 1e-6)
})


test_that("estimate_means, print bracl", {
skip_if_not_installed("brglm2")
# required for the penguins dataset, which was added in R 4.5.0
Expand Down
Loading