Repository navigation
Filter the response categories of ordinal models named in by (emmeans backend) - #692
Merged
Merged
Conversation
…ith unknown categories
…and error, and fix the NEWS wording
…l response terms, and give a clear error for unreadable values
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
Only a minor test-coverage nit remains; no blocking issues were identified.
No open issues 🎉
4 files not reviewed
man/estimate_contrasts.Rd: Generated fileman/estimate_means.Rd: Generated fileman/estimate_slopes.Rd: Generated fileman/get_emmeans.Rd: Generated file
What changed in this PR
Adds response-category filtering for frequentist ordinal models using the emmeans backend.
Changes:
- Supports filtering means, contrasts, and slopes by ordinal response categories.
- Validates response categories and preserves category-specific adjustments.
- Adds tests, documentation, NEWS updates, and a version bump.
| File | Summary |
|---|---|
tests/testthat/test-ordinal.R |
Adds ordinal response-filter regression tests. |
R/get_emtrends.R |
Applies filters to ordinal slopes. |
R/get_emmeans.R |
Extracts, validates, and applies response filters. |
R/get_emcontrasts.R |
Propagates filters through ordinal contrasts. |
R/estimate_means.R |
Documents the filtering behavior. |
NEWS.md |
Records the bug fix. |
man/get_emmeans.Rd |
Updates generated documentation. |
man/estimate_slopes.Rd |
Updates generated documentation. |
man/estimate_means.Rd |
Updates generated documentation. |
man/estimate_contrasts.Rd |
Updates generated documentation. |
DESCRIPTION |
Bumps the development version. |
Review effort: Lite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For ordinal models with
backend = "emmeans", abyterm can name the response with values, such as"Sat='Low'". This filter had no effect on means and contrasts, and it made slopes fail. emmeans ignoresatfor the response categories withmode = "prob", so the filter never reached the output.Example, with
m <- MASS::polr(Sat ~ Infl + Type + Cont, weights = Freq, data = MASS::housing).m2ispolr(Sat ~ Infl * x)on the same data with a numericx:estimate_means(m, c("Infl", "Sat='Low'"), backend = "emmeans")Low)estimate_contrasts(m, "Infl", by = "Sat='Low'", backend = "emmeans")estimate_slopes(m2, "x", by = c("Infl", "Sat='Low'"), backend = "emmeans")estimate_means(m, c("Infl", "Sat='Low'"), predict = "cum.prob", backend = "emmeans")Satcan only be used inbywithpredict = "prob"estimate_means(m, c("Infl", "Sat='Lowest'"), backend = "emmeans")Sathas no categoryLowestWhat changed:
.process_emmeans_arguments()now calls the new.extract_ordinal_response_filter(). For frequentist ordinal models (.is_emmeans_ordinal()), it removesbyterms on the response before emmeans runs. The emmeans call is then the same as without the filter. It keeps the chosen categories as character, so unquoted numbers such asrating=1also work. Several terms on the response are combined. The values are checked against the categories of the model (y.levelsorlev).response_filterattribute..clean_pseudo_factor_emmeans_ordinal()keeps the rows of these categories before it renames the column toResponse, so a predictor namedResponsedoes not change the result. p-values are adjusted within each response category, as before, so filtering does not change them..check_ordinal_response_filter()stops with an error for otherpredictmodes. For slopes it checks the mode the user asked for, not the internal"cum.prob"mode.bydocs and NEWS describe the filter.Tests in
test-ordinal.Rcover polr, clm, clmm, and clmm with letter categories. They check one and two categories for means and contrasts, withp_adjust = "holm"for polr and clm, and slopes and contrasts of slopes for polr. The filtered output must equal the matching rows of the unfiltered output at a tolerance of 1e-10. The tests also cover the errors, several terms on the response, and the response named without values.Not fixed here, the same as on main:
bythat names the response is not filtered."Sat [Low]"is not recognized as a term on the response.estimate_contrasts(m, "Sat='Low'", by = "Infl")ignores the value.plot()of emmeans contrasts for ordinal models stops with "Please provide the estimated means data".devtools::test()gives 0 failures locally.devtools::check()gives 0 errors and 0 warnings. The version is bumped to 0.17.0.20.