Skip to content

Simplify contrasts of avg slopes#611

Merged
strengejacke merged 19 commits intomainfrom
simplify_context_fx
Apr 26, 2026
Merged

Simplify contrasts of avg slopes#611
strengejacke merged 19 commits intomainfrom
simplify_context_fx

Conversation

@strengejacke
Copy link
Copy Markdown
Member

No description provided.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the modelbased package to version 0.14.0.9, introducing a more intuitive syntax for calculating "context effects" (contrasts of average slopes) in estimate_contrast(). Instead of using specific string options for the comparison argument, users can now specify multiple numeric predictors directly in the contrast argument. The feedback highlights critical bugs in R/get_marginalcontrasts.R where the new second_focal variable is not properly initialized for single-predictor cases, which would cause the function to crash. There is also a request to clarify the documentation regarding the formula syntax for comparisons to ensure consistency between the description and the examples provided.

Comment thread R/get_marginalcontrasts.R Outdated
Comment thread R/get_marginalcontrasts.R Outdated
Comment thread R/estimate_contrasts.R Outdated
@strengejacke strengejacke requested a review from Copilot April 26, 2026 11:29
@strengejacke
Copy link
Copy Markdown
Member Author

@gemini-code-assist review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the handling of context effects (contrasting average slopes) in estimate_contrast(). The previous requirement to explicitly set comparison = "slope" has been replaced by an automatic detection mechanism that triggers when two numeric predictors are passed to the contrast argument. Documentation, examples, and internal logic across several files have been updated to support this new syntax. Review feedback highlighted a bug involving an invalid regex backreference in variable name cleaning and a logic error where existing grouping variables in the by argument could be unintentionally overwritten.

Comment thread R/get_marginalcontrasts.R
Comment thread R/get_marginalcontrasts.R
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates how estimate_contrasts() handles “context effects” / contrasts of average slopes, shifting from special comparison= modes to a simpler interface driven by the contrast argument (two numeric predictors, optionally plus a third grouping variable).

Changes:

  • Simplifies context-effects usage so contrasting two average slopes is triggered by providing two numeric predictors in contrast (and optionally a third variable for pairwise comparisons across levels).
  • Refactors internal slope/context detection (contrast_slopes) and removes now-unneeded special-casing/mappings for prior slope/context comparison modes.
  • Updates documentation + tests to reflect the new interface and bumps the dev version.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/testthat/test-estimate_contrasts_context.R Updates tests to use the new “two numeric predictors in contrast” API and checks the informational message when comparison is overridden.
tests/testthat/test-estimate_contrasts.R Cleans up snapshot calls (removes # nolint markers around closing parentheses).
man/get_emmeans.Rd Documents contrasting average slopes via contrast (but currently contains a malformed inline example).
man/estimate_contrasts.Rd Same documentation update as above (also currently contains a malformed inline example).
R/get_marginalmeans.R Removes a now-unnecessary early-return special case for context comparisons.
R/get_marginalcontrasts.R Refactors slope/context detection into .get_marginaleffects_hypothesis_argument() and introduces contrast_slopes plumbing.
R/get_inequalitycontrasts.R Switches from compute_slopes to my_args$contrast_slopes and removes mapping of "slope"/"slope_pairwise" to context modes.
R/format.R Renames attribute usage from compute_slopes to contrast_slopes for inequality formatting logic.
R/estimate_contrasts.R Expands docs: new note + new section on context effects via contrasting average slopes; updates examples accordingly.
NEWS.md Updates release notes for the new API, but currently references a non-existent estimate_contrast() function name.
DESCRIPTION Bumps package version to 0.14.0.9.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread R/estimate_contrasts.R Outdated
Comment thread man/estimate_contrasts.Rd Outdated
levels at which contrasts are evaluated (e.g., \code{contrast="x=c('a','b')"}).
\strong{Note:} It is also possible to contrast average slopes, i.e. \code{contrast} can
be the name of two numeric predictors. However, while it is possible to filter
data for one numeric contrast (e.g., \code{contrast = c("num_pred=c(0, 1, 3)")},
Copy link

Copilot AI Apr 26, 2026

Choose a reason for hiding this comment

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

The contrast argument note contains a malformed example: \code{contrast = c("num_pred=c(0, 1, 3)")}, includes a literal } and trailing comma inside the \code{} block, which will display incorrectly in the rendered documentation. Please fix the example text so the code is valid and does not include stray punctuation (this likely should be regenerated from corrected roxygen comments).

Suggested change
data for one numeric contrast (e.g., \code{contrast = c("num_pred=c(0, 1, 3)")},
data for one numeric contrast (e.g., \code{contrast = c("num_pred=c(0, 1, 3)")}),

Copilot uses AI. Check for mistakes.
Comment thread man/get_emmeans.Rd Outdated
levels at which contrasts are evaluated (e.g., \code{contrast="x=c('a','b')"}).
\strong{Note:} It is also possible to contrast average slopes, i.e. \code{contrast} can
be the name of two numeric predictors. However, while it is possible to filter
data for one numeric contrast (e.g., \code{contrast = c("num_pred=c(0, 1, 3)")},
Copy link

Copilot AI Apr 26, 2026

Choose a reason for hiding this comment

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

The contrast argument note contains a malformed example: \code{contrast = c("num_pred=c(0, 1, 3)")}, includes a literal } and trailing comma inside the \code{} block, so it will render incorrectly. Please correct the example (ideally by fixing the roxygen source and re-running roxygen2).

Suggested change
data for one numeric contrast (e.g., \code{contrast = c("num_pred=c(0, 1, 3)")},
data for one numeric contrast (e.g., \code{contrast = c("num_pred=c(0, 1, 3)")}),

Copilot uses AI. Check for mistakes.
Comment thread NEWS.md Outdated
* Added option `comparison = "slope"` to `estimate_contrast()`, to allow
calculating contrasts of average slopes. This can also be useful when
estimating the context effects of within- and between-effects in a model.
* `estimate_contrast()` can now also calculate contrasts of average slopes, by
Copy link

Copilot AI Apr 26, 2026

Choose a reason for hiding this comment

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

NEWS.md refers to estimate_contrast(), but the exported API appears to be estimate_contrasts() (plural). Please update this entry to the correct function name to avoid confusing users.

Suggested change
* `estimate_contrast()` can now also calculate contrasts of average slopes, by
* `estimate_contrasts()` can now also calculate contrasts of average slopes, by

Copilot uses AI. Check for mistakes.
@strengejacke strengejacke merged commit 0d860ea into main Apr 26, 2026
15 of 20 checks passed
@strengejacke strengejacke deleted the simplify_context_fx branch April 26, 2026 11:45
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 26, 2026

Codecov Report

❌ Patch coverage is 94.73684% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.10%. Comparing base (99d9aa0) to head (c6f85cf).
⚠️ Report is 18 commits behind head on main.

Files with missing lines Patch % Lines
R/get_marginalcontrasts.R 93.54% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #611      +/-   ##
==========================================
+ Coverage   89.75%   90.10%   +0.35%     
==========================================
  Files          41       42       +1     
  Lines        3669     3842     +173     
==========================================
+ Hits         3293     3462     +169     
- Misses        376      380       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants