Skip to content

fix(nnt): preserve finite bound in one-sided confidence intervals - #724

Merged
mattansb merged 1 commit into
easystats:mainfrom
finite-sample:fix/nnt-onesided-ci
Nov 29, 2026
Merged

mattansb merged 1 commit into
easystats:mainfrom
finite-sample:fix/nnt-onesided-ci

Conversation

@soodoku

@soodoku soodoku commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Description

In nnt() (R/xtab_diff.R:361-369), confidence intervals are computed by inverting the confidence interval of arr(..., alternative = alternative2) where alternative2 flips "greater" and "less".

Because arr() clamps the unbounded end of a one-sided CI to -1 (for alternative2 = "less") or +1 (for alternative2 = "greater"), the two ARR bounds have opposite signs whenever:

  • alternative = "greater" and ARR > 0 (arr returns [-1, arr_CI_high] with arr_CI_high > 0), or
  • alternative = "less" and ARR < 0 (arr returns [arr_CI_low, 1] with arr_CI_low < 0).

In those cases, all(ci_sign == 1) || all(ci_sign == -1) evaluates to FALSE, sending the one-sided CI into the two-sided zero-crossing else branch that does not swap CI_low and CI_high. Subsequent .limit_ci(out, alternative, -Inf, Inf) then discards the inverted finite bound (1 / arr_CI_high or 1 / 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 invert CI_low/CI_high for one-sided alternatives (alternative != "two.sided" || all(ci_sign == 1) || all(ci_sign == -1)), so .limit_ci(out, alternative, -Inf, Inf) replaces the artificial 1 / -1 or 1 / 1 bound with Inf / -Inf while preserving 1 / 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 both ARR > 0 and ARR < 0 against 1 / arr(...) and the matching two-sided CI bounds.

Minimal Reproducible Example

RCT_pos <- rbind(c(100, 50), c(30, 71))
RCT_neg <- rbind(c(30, 71), c(100, 50))

nnt(RCT_pos, alternative = "greater")
nnt(RCT_neg, alternative = "less")
  • Before (main):
    • nnt(RCT_pos, alternative = "greater"): 2.81 | [-1.00, Inf]
    • nnt(RCT_neg, alternative = "less"): -2.81 | [-Inf, 1.00]
  • After (this PR):
    • 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).
  • Revert check (reverting only R/xtab_diff.R): 4 failures in test-xtab.R; restoring R/xtab_diff.R returns to 69/69 passed.
  • Formatted with air format and checked with lintr (0 new lints).

This PR was assisted by Gemini 4 Neon via Antigravity CLI; I have reviewed and verified the change.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 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

@mattansb
mattansb merged commit 9b4d824 into easystats:main Nov 29, 2026
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.

3 participants