Add by_level argument to sort_ard_hierarchical() (#548) - #591
Open
ddsjoberg wants to merge 1 commit into
Open
Conversation
Adds a `by_level` argument to `sort_ard_hierarchical()` allowing `"descending"` sorting to rank variable groups by the counts observed within specific `by` variable levels, rather than the sums across all levels. The argument accepts a named list (names are `by` variables, values are a single level each), so any combination of `by` variables may be used. This generalizes the single-`by`-variable approach explored in #550 to work with any number of `by` variables, integrated into the current vctrs-based sort engine: a per-row mask built from the pre-reformat `by` group columns is AND-ed into the descending count-sum filter. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
Unit Tests Summary 1 files 276 suites 19s ⏱️ Results for commit 0ed7ca5. ♻️ This comment has been updated with latest results. |
Contributor
Code Coverage SummaryDiff against mainResults for commit: 0ed7ca5 Minimum allowed coverage is ♻️ This comment has been updated with latest results |
Contributor
Unit Test Performance DifferenceAdditional test case details
Results for commit 45db895 ♻️ This comment has been updated with latest results. |
Collaborator
Author
|
Hi @rikoprogrammer, your branch had a merge conflict, and rather than trying to resolve it, I just made a new branch. Can you take a look here? Did I generalize your code correctly? |
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.
What changes are proposed in this pull request?
by_levelargument tosort_ard_hierarchical(), allowing"descending"sorting to rank variable groups by the counts observed within specificbyvariable levels (e.g.by_level = list(TRTA = "Placebo")) rather than the sums across allbyvariable levels. The argument accepts a named list, so any combination ofbyvariables may be used. (Feature Request: sort_ard_hierarchical #548, @rikoprogrammer)Provide more detail here as needed.
by_levelaccepts a named list where names arebyvariables and values are a single level of that variable. When sorting"descending", variable groups are ranked by count sums computed only from rows matching the specifiedbylevel(s):This generalizes the single-
by-variablesort_levelapproach explored in #550 to work with any number ofbyvariables, which was that PR's core limitation.Implementation notes for reviewers
Since #550 was opened, the sort engine was rewritten for performance (#176), so
.append_hierarchy_sums()no longer exists. The feature is integrated into the currentvctrs-based engine:by(as supplied toard_stack_hierarchical()) is captured before the "highest-severity" extraction that moves the innermostbyvariable intovariables, soby_levelis validated and applied against thebyvariables as the user knows them..hierarchy_by_level_mask()builds a per-row logical mask from the pre-reformatbygroup columns (which are not reordered by the sort), keeping it positionally aligned with the reformatted frame. It is AND-ed into thekeepfilter of.hierarchy_sort_group_desc(), so only rows at the requested level(s) contribute to the count sums. Variable groups with no contributing rows sort last..check_by_level()validates the input: must be a fully-named list, names must bebyvariables, a single level per entry, the level must exist, and at least onebyvariable must be present.Correctness was verified with an oracle: sorting with
by_level = list(TRTA = "Placebo")on the full ARD produces the same variable-group ordering as the default sort on an ARD built only from Placebo subjects — confirmed for both single- and two-by-variable cases. New unit tests cover the functional behavior, multiplebyvariables,by_level = NULLequivalence to the default, and all validation error paths (snapshots added).closes #548
Pre-review Checklist (if item does not apply, mark is as complete)
usethis::pr_merge_main()(branched from currentmain)devtools::test_coverage()Reviewer Checklist (if item does not apply, mark is as complete)
pkgdown::build_site(). Check the R console for errors, and review the rendered website.devtools::test_coverage()When the branch is ready to be merged:
NEWS.mdwith the changes from this pull request under the heading "# cards (development version)". If there is an issue associated with the pull request, reference it in parentheses at the end update (seeNEWS.mdfor examples).