Skip to content

[Bug]: Stratified Wilson and Newcombe CI's when zero successes are observed #1416

@srigby-argx

Description

@srigby-argx

What happened?

In case there are no successes in one of the groups, I get the error: Error in [.default(tbl, "TRUE", ) : subscript out of bounds. Apparently this is because tbl has no row called "TRUE".

I don't know of any reason why we can't calculate a CI here; in theory it should be possible. The unstratified version apparently works fine.

Note that if I swap successes <-> failures then I get different errors.

sessionInfo()

> sessionInfo()
R version 4.2.2 (2022-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.6 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tern_0.9.0        rtables_0.6.12    magrittr_2.0.3    formatters_0.5.11

loaded via a namespace (and not attached):
 [1] rstudioapi_0.17.1 splines_4.2.2     tidyselect_1.2.0  munsell_0.5.0    
 [5] lattice_0.20-45   colorspace_2.1-0  R6_2.5.1          rlang_1.1.1      
 [9] fastmap_1.1.1     dplyr_1.1.4       rbibutils_2.2.16  tools_4.2.2      
[13] grid_4.2.2        broom_1.0.5       checkmate_2.3.0   gtable_0.3.3     
[17] cli_3.6.3         htmltools_0.5.8.1 survival_3.7-0    digest_0.6.32    
[21] tibble_3.2.1      lifecycle_1.0.4   Matrix_1.6-0      tidyr_1.3.1      
[25] purrr_1.0.4       ggplot2_3.5.1     Rdpack_2.6        vctrs_0.6.4      
[29] glue_1.6.2        stringi_1.7.12    compiler_4.2.2    pillar_1.10.1    
[33] generics_0.1.3    scales_1.3.0      backports_1.4.1   pkgconfig_2.0.3  
>

Relevant log output

set.seed(5)

library(tern)
#> Loading required package: rtables
#> Loading required package: formatters
#> Loading required package: magrittr
#> 
#> Attaching package: 'rtables'
#> The following object is masked from 'package:utils':
#> 
#>     str
#> Registered S3 method overwritten by 'tern':
#>   method   from 
#>   tidy.glm broom

grp <- factor(c(rep("T", 50), rep("C", 50)))
rsp <- as.logical(rbinom(n = 100, size = 1, prob = 0.2))
strata <- factor(sample(1:4, size = 100, replace = TRUE))

prop_wilson(rsp[grp == "C"], conf_level = 0.95)
#> [1] 0.2243695 0.4784617

prop_diff_strat_nc(rsp, grp, strata,
                   weights_method = "cmh")
#> $diff
#> [1] -0.128047
#> 
#> $diff_ci
#>       lower       upper 
#> -0.29644748  0.04999571

prop_strat_wilson(rsp[grp == "C"], strata[grp == "C"])
#> $conf_int
#>     lower     upper 
#> 0.2127322 0.4590773 
#> 
#> $weights
#>         1         2         3         4 
#> 0.2993060 0.2090818 0.3423156 0.1492966

# Change all controls to failures

rsp[grp == "C"] <- FALSE

prop_wilson(rsp[grp == "C"], conf_level = 0.95)
#> [1] 0.0000000 0.0713476

prop_diff_strat_nc(rsp, grp, strata,
                   weights_method = "cmh")
#> Error in `[.default`(tbl, "TRUE", ): subscript out of bounds

prop_strat_wilson(rsp[grp == "C"], strata[grp == "C"])
#> Error in `[.default`(tbl, "TRUE", ): subscript out of bounds

# Swap TRUEs and FALSEs
rsp <- !rsp

prop_wilson(rsp[grp == "C"], conf_level = 0.95)
#> [1] 0.9286524 1.0000000

prop_diff_strat_nc(rsp, grp, strata,
                   weights_method = "cmh")
#> Error in stats::prop.test(x, n, correct = correct, conf.level = strata_conf_level): 'conf.level' must be a single number between 0 and 1

prop_strat_wilson(rsp[grp == "C"], strata[grp == "C"])
#> Error in if (diff_v[length(diff_v)] < tol) break: missing value where TRUE/FALSE needed

Created on 2025-05-08 with reprex v2.1.1

Code of Conduct

  • I agree to follow this project's Code of Conduct.

Contribution Guidelines

  • I agree to follow this project's Contribution Guidelines.

Security Policy

  • I agree to follow this project's Security Policy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions