From d62763f3598a0c7369a394ba28ffe715840d5cd8 Mon Sep 17 00:00:00 2001 From: Jeffrey Girard Date: Wed, 25 Nov 2026 17:36:05 -0600 Subject: [PATCH 1/2] Return probabilities of each response category for brms ordinal models (emmeans backend) --- DESCRIPTION | 2 +- NEWS.md | 9 +++++++++ R/get_emmeans.R | 22 ++++++++++++++++++++++ tests/testthat/test-ordinal.R | 19 +++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 89d87b30b..6e244fe82 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", diff --git a/NEWS.md b/NEWS.md index a73a1159f..dde5c9e38 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,12 @@ +# modelbased (devel) + +## Bug fixes + +* `estimate_means()` with `backend = "emmeans"` now returns the probabilities + of each response category for ordinal 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 diff --git a/R/get_emmeans.R b/R/get_emmeans.R index 87439efad..dde1576c6 100644 --- a/R/get_emmeans.R +++ b/R/get_emmeans.R @@ -71,6 +71,12 @@ get_emmeans <- function( dpars <- FALSE fun_args$epred <- TRUE fun_args$type <- "response" + # ordinal 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(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 @@ -175,6 +181,13 @@ get_emmeans <- function( } +# ordinal models from brms +.is_brms_ordinal <- function(model) { + inherits(model, "brmsfit") && + isTRUE(insight::model_info(model, response = 1, verbose = FALSE)$is_ordinal) +} + + # 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"`. @@ -294,6 +307,15 @@ get_emmeans <- function( if (length(colums_to_add)) { means <- cbind(em_grid[colums_to_add], means) } + # brms ordinal models: the response categories + if (.is_brms_ordinal(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 diff --git a/tests/testthat/test-ordinal.R b/tests/testthat/test-ordinal.R index 5a014049c..225772851 100644 --- a/tests/testthat/test-ordinal.R +++ b/tests/testthat/test-ordinal.R @@ -264,6 +264,25 @@ 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, print bracl", { skip_if_not_installed("brglm2") # required for the penguins dataset, which was added in R 4.5.0 From a08aac8148826534990ee0bf824b1894fffaa753 Mon Sep 17 00:00:00 2001 From: Jeffrey Girard Date: Wed, 25 Nov 2026 18:02:56 -0600 Subject: [PATCH 2/2] Also return per-category probabilities for brms categorical models (emmeans backend) --- NEWS.md | 6 +++--- R/get_emmeans.R | 23 +++++++++++++---------- tests/testthat/test-ordinal.R | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/NEWS.md b/NEWS.md index dde5c9e38..364ea63cb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,9 +3,9 @@ ## Bug fixes * `estimate_means()` with `backend = "emmeans"` now returns the probabilities - of each response category for ordinal 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. + 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 diff --git a/R/get_emmeans.R b/R/get_emmeans.R index dde1576c6..80ec377a7 100644 --- a/R/get_emmeans.R +++ b/R/get_emmeans.R @@ -71,10 +71,10 @@ get_emmeans <- function( dpars <- FALSE fun_args$epred <- TRUE fun_args$type <- "response" - # ordinal 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(model)) { + # 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)) { @@ -181,10 +181,13 @@ get_emmeans <- function( } -# ordinal models from brms -.is_brms_ordinal <- function(model) { - inherits(model, "brmsfit") && - isTRUE(insight::model_info(model, response = 1, verbose = FALSE)$is_ordinal) +# 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) } @@ -307,8 +310,8 @@ get_emmeans <- function( if (length(colums_to_add)) { means <- cbind(em_grid[colums_to_add], means) } - # brms ordinal models: the response categories - if (.is_brms_ordinal(model) && "rep.meas" %in% colnames(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)) { diff --git a/tests/testthat/test-ordinal.R b/tests/testthat/test-ordinal.R index 225772851..06f0379b9 100644 --- a/tests/testthat/test-ordinal.R +++ b/tests/testthat/test-ordinal.R @@ -283,6 +283,25 @@ test_that("estimate_means, backend emmeans, brms ordinal", { }) +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")) + 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