From 8f573c8cd89fe4802cafd486a92e04db70edd113 Mon Sep 17 00:00:00 2001 From: ***** <721466+soodoku@users.noreply.github.com> Date: Wed, 25 Nov 2026 05:15:12 -0800 Subject: [PATCH] fix(nnt): preserve finite bound in one-sided confidence intervals --- R/xtab_diff.R | 2 +- tests/testthat/test-xtab.R | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/R/xtab_diff.R b/R/xtab_diff.R index c1b7c537..a19fc845 100644 --- a/R/xtab_diff.R +++ b/R/xtab_diff.R @@ -360,7 +360,7 @@ nnt <- function(x, y = NULL, ci = 0.95, alternative = "two.sided", ...) { if ("CI" %in% colnames(out)) { ci_sign <- unlist(sign(out[c("CI_low", "CI_high")])) - if (all(ci_sign == 1) || all(ci_sign == -1)) { + if (alternative != "two.sided" || all(ci_sign == 1) || all(ci_sign == -1)) { out[c("CI_low", "CI_high")] <- 1 / out[c("CI_high", "CI_low")] } else { out[c("CI_low", "CI_high")] <- 1 / out[c("CI_low", "CI_high")] diff --git a/tests/testthat/test-xtab.R b/tests/testthat/test-xtab.R index b9c87633..917f6f9c 100644 --- a/tests/testthat/test-xtab.R +++ b/tests/testthat/test-xtab.R @@ -232,6 +232,40 @@ test_that("oddsratio & riskratio", { expect_equal(NNT[[1]], arr_to_nnt(arr(x, y)[[1]])) }) +test_that("nnt() one-sided CIs invert the finite ARR bound", { + # ARR > 0 (NNT > 0) + RCT_pos <- rbind( + c(100, 50), + c(30, 71) + ) + nnt_pos_2s <- nnt(RCT_pos, ci = 0.90, alternative = "two.sided") + nnt_pos_gt <- nnt(RCT_pos, ci = 0.95, alternative = "greater") + nnt_pos_lt <- nnt(RCT_pos, ci = 0.95, alternative = "less") + arr_pos_lt <- arr(RCT_pos, ci = 0.95, alternative = "less") + + expect_equal(nnt_pos_gt$CI_low, 1 / arr_pos_lt$CI_high, tolerance = 1e-12) + expect_equal(nnt_pos_gt$CI_low, nnt_pos_2s$CI_low, tolerance = 1e-12) + expect_identical(nnt_pos_gt$CI_high, Inf) + expect_identical(nnt_pos_lt$CI_low, -Inf) + expect_equal(nnt_pos_lt$CI_high, nnt_pos_2s$CI_high, tolerance = 1e-12) + + # ARR < 0 (NNT < 0) + RCT_neg <- rbind( + c(30, 71), + c(100, 50) + ) + nnt_neg_2s <- nnt(RCT_neg, ci = 0.90, alternative = "two.sided") + nnt_neg_lt <- nnt(RCT_neg, ci = 0.95, alternative = "less") + nnt_neg_gt <- nnt(RCT_neg, ci = 0.95, alternative = "greater") + arr_neg_gt <- arr(RCT_neg, ci = 0.95, alternative = "greater") + + expect_identical(nnt_neg_lt$CI_low, -Inf) + expect_equal(nnt_neg_lt$CI_high, 1 / arr_neg_gt$CI_low, tolerance = 1e-12) + expect_equal(nnt_neg_lt$CI_high, nnt_neg_2s$CI_high, tolerance = 1e-12) + expect_equal(nnt_neg_gt$CI_low, nnt_neg_2s$CI_low, tolerance = 1e-12) + expect_identical(nnt_neg_gt$CI_high, Inf) +}) + test_that("fei() for 1D tables", { data(Titanic)