Skip to content

Return slopes and contrasts of each response category for brms ordinal and categorical models (emmeans backend) - #724

Merged
strengejacke merged 1 commit into
mainfrom
hotfix-brms-ordinal-emmeans-slopes
Nov 26, 2026
Merged

strengejacke merged 1 commit into
mainfrom
hotfix-brms-ordinal-emmeans-slopes

Conversation

@jmgirard

Copy link
Copy Markdown
Contributor

With backend = "emmeans", estimate_slopes() and estimate_contrasts() gave a single slope or difference of about 0 for brms ordinal and categorical models, because emmeans averaged over the response categories. For example, estimate_slopes(insight::download_model("brms_ordinal_1"), "mpg", backend = "emmeans") returned one slope of -2.5e-17.

This follows #723, which fixed estimate_means(): the rep.meas pseudo-factor is added to specs, slopes and contrasts are computed within each response category, and the column is named Response, as for the frequentist ordinal models.

The slope test uses insight::download_model("brms_ordinal_1"). The contrast test is in test-brms.R and fits a small model, because none of the brms models from insight::download_model() has a factor predictor.

Copilot AI left a comment

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

2 open issues
What changed in this PR

Fixes backend = "emmeans" behavior for brms ordinal/categorical models so estimate_slopes() / estimate_contrasts() return per-response-category results instead of (incorrectly) averaging across categories.

Changes:

  • Adjust emmeans specs to include the rep.meas pseudo-factor for brms ordinal/categorical models when computing slopes/contrasts.
  • Normalize output column naming by renaming rep.meas to Response via a shared helper.
  • Add regression tests for slopes (downloaded brms model) and contrasts (small fitted brms model).
File Description
tests/​testthat/​test-ordinal.R Adds test asserting one slope per brms response category with emmeans backend.
tests/​testthat/​test-brms.R Adds contrast tests for brms ordinal models across response categories.
R/​get_emtrends.R Adds rep.meas to specs for brms ordinal/categorical slopes; cleans response-category column name.
R/​get_emmeans.R Introduces .clean_brms_response_categories() helper and reuses it for emmeans output.
R/​get_emcontrasts.R Ensures contrasts/slopes are computed within brms response categories; cleans response-category column name.
NEWS.md Documents the corrected behavior for slopes/contrasts with emmeans backend for brms ordinal/categorical.
DESCRIPTION Bumps patch version.

Review effort: Lite

Comment thread tests/testthat/test-brms.R Resolved
Comment thread tests/testthat/test-ordinal.R Resolved
@strengejacke
strengejacke merged commit bc4a61c into main Nov 26, 2026
12 of 21 checks passed
@strengejacke
strengejacke deleted the hotfix-brms-ordinal-emmeans-slopes branch November 26, 2026 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants