Repository navigation
fix(nnt): preserve finite bound in one-sided confidence intervals - #724
Merged
Merged
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues for the one-sided NNT confidence-interval correction.
No open issues 🎉
What changed in this PR
This PR fixes one-sided confidence intervals in nnt() so that inverting ARR bounds preserves the correct finite bound rather than reporting an artificial −1 or +1.
Changes:
- Always swap and invert ARR bounds for one-sided alternatives, leaving two-sided behavior unchanged.
- Add regression tests for both one-sided alternatives with positive and negative ARR, checking finite bounds and infinite endpoints.
| File | Description |
|---|---|
tests/testthat/test-xtab.R |
Adds regression coverage for one-sided NNT confidence intervals. |
R/xtab_diff.R |
Corrects bound inversion for one-sided NNT confidence intervals. |
Review effort: Balanced
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.
Description
In
nnt()(R/xtab_diff.R:361-369), confidence intervals are computed by inverting the confidence interval ofarr(..., alternative = alternative2)wherealternative2flips"greater"and"less".Because
arr()clamps the unbounded end of a one-sided CI to-1(foralternative2 = "less") or+1(foralternative2 = "greater"), the two ARR bounds have opposite signs whenever:alternative = "greater"andARR > 0(arrreturns[-1, arr_CI_high]witharr_CI_high > 0), oralternative = "less"andARR < 0(arrreturns[arr_CI_low, 1]witharr_CI_low < 0).In those cases,
all(ci_sign == 1) || all(ci_sign == -1)evaluates toFALSE, sending the one-sided CI into the two-sided zero-crossingelsebranch that does not swapCI_lowandCI_high. Subsequent.limit_ci(out, alternative, -Inf, Inf)then discards the inverted finite bound (1 / arr_CI_highor1 / arr_CI_low) and leaves-1(1 / -1) or+1(1 / 1) as the reported finite bound.Proposed Changes
R/xtab_diff.R: always swap and invertCI_low/CI_highfor one-sided alternatives (alternative != "two.sided" || all(ci_sign == 1) || all(ci_sign == -1)), so.limit_ci(out, alternative, -Inf, Inf)replaces the artificial1 / -1or1 / 1bound withInf/-Infwhile preserving1 / arr_CI_high/1 / arr_CI_low. Two-sided CIs (both crossing 0 and not crossing 0) are completely unchanged.tests/testthat/test-xtab.R: add a regression test verifying one-sided"greater"and"less"CIs for bothARR > 0andARR < 0against1 / arr(...)and the matching two-sided CI bounds.Minimal Reproducible Example
main):nnt(RCT_pos, alternative = "greater"):2.81 | [-1.00, Inf]nnt(RCT_neg, alternative = "less"):-2.81 | [-Inf, 1.00]nnt(RCT_pos, alternative = "greater"):2.81 | [2.21, Inf]nnt(RCT_neg, alternative = "less"):-2.81 | [-Inf, -2.21]Verification
testthat::test_file("tests/testthat/test-xtab.R"): 69/69 passed (including existing two-sided crossing-0 and non-crossing-0 NNT tests).R/xtab_diff.R): 4 failures intest-xtab.R; restoringR/xtab_diff.Rreturns to 69/69 passed.air formatand checked withlintr(0 new lints).This PR was assisted by Gemini 4 Neon via Antigravity CLI; I have reviewed and verified the change.