Fix backwards comparison in brunner_munzel permutation test causing inflated p-values#110
Merged
arcaldwell49 merged 5 commits intomasterfrom Jan 26, 2026
Merged
Conversation
Co-authored-by: arcaldwell49 <24322628+arcaldwell49@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix inflated p-values in brunner_munzel for small samples
Fix backwards comparison in brunner_munzel permutation test causing inflated p-values
Jan 26, 2026
- Fix permutation loop, causing error with variance calc - Deprecate simple_htest interface with brunner_munzel - Updated output for brunner_munzel, parameter changed to N-permutations and labeling for sample estimate more verbose/accurate. - added file to junk folder comparing my implementation to nparcomp
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical bug in the Brunner-Munzel permutation test where inverted comparison operators caused inflated p-values (approximately doubled) for completely separated samples. The issue specifically affected brunner_munzel(test_method = "perm").
Changes:
- Fixed backwards comparison operators in permutation p-value calculation (lines 777-778 in R/brunner_munzel.R)
- Changed two-sided p-value formula from doubling method to absolute value method for consistency with reference implementations
- Improved unique allocation sampling to avoid duplicate permutations in small samples
- Enhanced output labels to use actual group names instead of generic "X" and "Y"
- Added comprehensive regression tests validating the fix against known reference values
- Deprecated brunner_munzel option in simple_htest() to encourage direct use of brunner_munzel()
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| R/brunner_munzel.R | Core fix for comparison operators (lines 777-778), improved permutation sampling (lines 189-229), consistent zero-variance handling, relative effect convention fix in perm_loop (line 1064), enhanced estimate labeling with actual group names, and improved parameter reporting for permutation tests |
| tests/testthat/test-htest.R | Comprehensive regression tests for completely separated samples, one-sided and two-sided alternatives, validation against brunnermunzel R package reference values |
| R/simple_htest.R | Deprecation warning for brunner_munzel test option with guidance to use brunner_munzel() directly, updated documentation removing brunner_munzel from main options |
| junk/validate_brunner_munzel_vs_nparcomp.R | Development validation script comparing TOSTER implementation against nparcomp package across multiple scenarios |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
arcaldwell49
approved these changes
Jan 26, 2026
Collaborator
arcaldwell49
left a comment
There was a problem hiding this comment.
Changes successfully have resolved issue. Unit tests added to avoid issue in the future.
Collaborator
|
Issue with R-CMD-Check appears to be a network error. Merging despite failing check. |
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.
Issue
Permutation test p-values in
brunner_munzel(test_method = "perm")were systematically inflated ~2x for completely separated samples due to inverted comparison operators counting non-extreme permutations instead of extreme ones.Changes
Code fix (
R/brunner_munzel.R:751-752):Regression tests (
tests/testthat/test-htest.R):Impact
Affects two-sided permutation tests on small samples with extreme effects. Causes conservative bias (reduced power). Unpaired tests only—paired tests use different code path and are unaffected.
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
cloud.r-project.org/usr/lib/R/bin/exec/R /usr/lib/R/bin/exec/R --slave --no-save -e ~n~install.packages(c('testthat',~+~'ggplot2',~+~'ggdist',~+~'distributional',~+~'cowplot',~+~'tidyr',~+~'R6',~+~'lifecycle'),~+~repos='REDACTED',~+~Ncpus=4)~n~(dns block)cran.posit.co/usr/lib/R/bin/exec/R /usr/lib/R/bin/exec/R --slave --no-save dpkg-split -Qao�� all.deb .0-1build2_amd64.deb eb ll.deb 1.14-1_all.deb eb rm -rf stall-haIRnB/707-r-cran-riskregression_2023.12.21+ds-1_amd64.deb p.ci plit stall-haIRnB/507/usr/bin/dpkg b plit dpkg-deb(dns block)cran.r-project.org/usr/lib/R/bin/exec/R /usr/lib/R/bin/exec/R --slave --no-save -e ~n~install.packages(c('testthat',~+~'ggplot2',~+~'ggdist',~+~'distributional',~+~'cowplot',~+~'tidyr',~+~'R6',~+~'lifecycle'),~+~repos='REDACTED',~+~Ncpus=4)~n~(dns block)esm.ubuntu.com/usr/lib/apt/methods/https /usr/lib/apt/methods/https(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.