Skip to content

Commit c545ee8

Browse files
authored
Merge pull request #130 from FredHutch/lemireg/pw_test_bin-sampsize
Add SampleSizes to pairwise_test_bin()
2 parents 9bfbaa1 + afa2d0c commit c545ee8

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# VISCfunctions (development version)
22

3+
* Add `SampleSizes` output to `pairwise_test_bin()` (#130)
34
* Minor updates to vignette (#141)
45
* Update title and description in package DESCRIPTION file (#139)
56
* Add `Median_Quartiles` to `pairwise_test_cont()` output (#99)

R/pairwise_comparisons.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ pairwise_test_cont <- function(
487487
#' group_by(antigen, visitno) |>
488488
#' group_modify(~ as.data.frame(
489489
#' pairwise_test_bin(x = .$response, group = .$group,
490-
#' method = 'barnard', alternative = 'less',
490+
#' method = 'barnard', alternative = 'two.sided',
491491
#' num_needed_for_test = 3, digits = 1,
492492
#' trailing_zeros = TRUE, sep_val = ' vs. ', verbose = TRUE)))
493493
#'
@@ -508,6 +508,7 @@ pairwise_test_cont <- function(
508508
#' group_by(Stim, Parent, Population, Visit) |>
509509
#' group_modify(~ as.data.frame(
510510
#' pairwise_test_bin(x = .$response, group = .$Group , alternative = 'greater',
511+
#' sorted_group = 1:4,
511512
#' method = 'barnard', num_needed_for_test = 3, digits = 1,
512513
#' trailing_zeros = TRUE, sep_val = ' vs. ', verbose = TRUE)))
513514
#'
@@ -649,6 +650,8 @@ pairwise_test_bin <- function(x,
649650

650651
stats_by_group <- data.frame(Group1 = i_group,
651652
Group2 = j_group,
653+
Group1_n = sum(!is.na(i_vals)),
654+
Group2_n = sum(!is.na(j_vals)),
652655
Group1_rr = response_info_here_by_group[[1]],
653656
Group2_rr = response_info_here_by_group[[2]],
654657
stringsAsFactors = FALSE
@@ -697,8 +700,9 @@ pairwise_test_bin <- function(x,
697700

698701

699702
data.frame(Comparison = pasted_results$Comparison,
703+
SampleSizes = pasted_results$n_comparison,
700704
ResponseStats = pasted_results$rr_comparison,
701-
ResponseTest = results$ResponseTest ,
705+
ResponseTest = results$ResponseTest,
702706
PerfectSeparation = results$PerfectSeparation,
703707
stringsAsFactors = FALSE)
704708

man/pairwise_test_bin.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_pairwise_comparisons.R

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -616,19 +616,19 @@ test_that("pairwise_comparisons_bin testing two groups", {
616616
group_by(group) |>
617617
mutate(num_pos = sum(x), n = n()) |>
618618
group_by(group,num_pos, n) |>
619-
group_modify( ~ wilson_ci(.$x, .95)) |> ungroup() |>
619+
group_modify( ~ wilson_ci(.$x, .95),
620+
sum(.$x)) |> ungroup() |>
620621
mutate(rr = paste0(num_pos, '/', n, ' = ',
621622
stat_paste(mean * 100, lower * 100, upper * 100, digits = 1, suffix = '%')))
622623

623624
testing_stats <- bind_cols(
624-
testing_stats_pre |> filter(group == 'a') |> select(Group1 = group, Group1_rr = rr),
625-
testing_stats_pre |> filter(group == 'b') |> select(Group2 = group, Group2_rr = rr))
626-
625+
testing_stats_pre |> filter(group == 'a') |> select(Group1 = group, Group1_n = n, Group1_rr = rr),
626+
testing_stats_pre |> filter(group == 'b') |> select(Group2 = group, Group2_n = n, Group2_rr = rr))
627627

628628
# testing multiple methods
629629
test_pasting <- paste_tbl_grp(data = testing_stats)
630630

631-
names(test_pasting) <- c('Comparison', 'ResponseStats')
631+
names(test_pasting) <- c('Comparison', 'SampleSizes', 'ResponseStats')
632632

633633
lapply(c("barnard", "fisher", "chi.sq"),
634634
function(method_in){
@@ -648,18 +648,20 @@ test_that("pairwise_comparisons_bin testing two groups", {
648648
group_by(group) |>
649649
mutate(num_pos = sum(x), n = n()) |>
650650
group_by(group,num_pos, n) |>
651-
group_modify( ~ wilson_ci(.$x, .95)) |> ungroup() |>
651+
group_modify( ~ wilson_ci(.$x, .95),
652+
sum(.$x)) |>
653+
ungroup() |>
652654
mutate(rr = paste0(num_pos, '/', n, ' = ',
653655
stat_paste(mean * 100, lower * 100, upper * 100, digits = 3, suffix = '%')))
654656

655657
testing_stats_3digits <- bind_cols(
656658
testing_stats_pre_3digits |>
657659
filter(group == 'a') |>
658-
select(Group1 = group, Group1_rr = rr), testing_stats_pre_3digits |>
659-
filter(group == 'b') |> select(Group2 = group, Group2_rr = rr))
660+
select(Group1 = group, Group1_n = n, Group1_rr = rr), testing_stats_pre_3digits |>
661+
filter(group == 'b') |> select(Group2 = group, Group2_n = n, Group2_rr = rr))
660662

661663
test_pasting <- paste_tbl_grp(data = testing_stats_3digits)
662-
names(test_pasting) <- c('Comparison', 'ResponseStats')
664+
names(test_pasting) <- c('Comparison', 'SampleSizes', 'ResponseStats')
663665
testing_results <- data.frame(test_pasting,
664666
ResponseTest = two_samp_bin_test(x = x, y = group),
665667
PerfectSeparation = ifelse(diff(testing_stats_pre_3digits$mean) == 1,
@@ -671,7 +673,7 @@ test_that("pairwise_comparisons_bin testing two groups", {
671673
# One-sided test comparison
672674
test_pasting <- paste_tbl_grp(data = testing_stats, alternative = 'less')
673675

674-
names(test_pasting) <- c('Comparison', 'ResponseStats')
676+
names(test_pasting) <- c('Comparison', 'SampleSizes', 'ResponseStats')
675677

676678
testing_results <- data.frame(test_pasting,
677679
# Need reverse testing direction
@@ -686,7 +688,7 @@ test_that("pairwise_comparisons_bin testing two groups", {
686688
# sorted group greater than comparison
687689
test_pasting <- paste_tbl_grp(data = testing_stats, alternative = 'greater',
688690
first_name = 'Group2', second_name = 'Group1')
689-
names(test_pasting) <- c('Comparison', 'ResponseStats')
691+
names(test_pasting) <- c('Comparison', 'SampleSizes', 'ResponseStats')
690692
testing_results <- data.frame(
691693
test_pasting,
692694
# Need reverse testing direction
@@ -701,7 +703,7 @@ test_that("pairwise_comparisons_bin testing two groups", {
701703

702704
# High number needed for testing
703705
test_pasting <- paste_tbl_grp(data = testing_stats)
704-
names(test_pasting) <- c('Comparison', 'ResponseStats')
706+
names(test_pasting) <- c('Comparison', 'SampleSizes', 'ResponseStats')
705707
testing_results <- data.frame(
706708
test_pasting,
707709
ResponseTest = NA_integer_,
@@ -720,7 +722,8 @@ test_that("pairwise_comparisons_bin testing two groups", {
720722
group_by(group) |>
721723
mutate(num_pos = sum(x), n = n()) |>
722724
group_by(group,num_pos, n) |>
723-
group_modify( ~ wilson_ci(.$x, .95)) |> ungroup() |>
725+
group_modify( ~ wilson_ci(.$x, .95),
726+
sum(.$x)) |> ungroup() |>
724727
mutate(rr = paste0(num_pos, '/', n, ' = ', stat_paste(mean * 100,
725728
lower * 100,
726729
upper * 100,
@@ -729,9 +732,10 @@ test_that("pairwise_comparisons_bin testing two groups", {
729732

730733
paired_stats <- bind_cols(paired_stats_pre |>
731734
filter(group == 'a') |>
732-
select(Group1 = group, Group1_rr = rr), paired_stats_pre |>
735+
select(Group1 = group, Group1_n = n, Group1_rr = rr),
736+
paired_stats_pre |>
733737
filter(group == 'b') |>
734-
select(Group2 = group, Group2_rr = rr))
738+
select(Group2 = group, Group2_n = n, Group2_rr = rr))
735739

736740

737741
test_pasting <- paste_tbl_grp(data = paired_stats)
@@ -741,8 +745,8 @@ test_that("pairwise_comparisons_bin testing two groups", {
741745
second_name = "Group1")
742746
expect_false(identical(test_pasting, test_pasting_rev))
743747

744-
names(test_pasting) <- c('Comparison', 'ResponseStats')
745-
names(test_pasting_rev) <- c('Comparison', 'ResponseStats')
748+
names(test_pasting) <- c('Comparison', 'SampleSizes', 'ResponseStats')
749+
names(test_pasting_rev) <- c('Comparison', 'SampleSizes', 'ResponseStats')
746750

747751
testing_results <- data.frame(test_pasting,
748752
ResponseTest = two_samp_bin_test(x = x, y = group, method = 'mcnemar'),
@@ -778,11 +782,12 @@ test_that("pairwise_test_bin testing 3+ groups", {
778782
ci = wilson_ci(response),
779783
r1 = sum(response),
780784
r0 = abs(sum(response - 1)),
785+
n = n(),
781786
.groups = "keep") |>
782787
pivot_wider(id_cols = c(antigen, visitno),
783788
names_from = group,
784789
names_prefix = "grp",
785-
values_from = c(rfraction, ci, r0, r1)) |>
790+
values_from = c(rfraction, ci, r0, r1, n)) |>
786791
mutate(pval = ifelse(((r0_grp1 == 0 & r0_grp2 == 0) | (r1_grp1 == 0 & r1_grp2 == 0)),
787792
1,
788793
as.double(Exact::exact.test(matrix(c(r1_grp1,
@@ -795,6 +800,7 @@ test_that("pairwise_test_bin testing 3+ groups", {
795800
alternative = "two.sided")$p.value)),
796801
PerfectSeparation = FALSE,
797802
Comparison = "1 vs. 2",
803+
SampleSizes = paste0(n_grp1, " vs. ", n_grp2),
798804
ResponseStats = paste0(rfraction_grp1,
799805
" = ",
800806
round_away_0(ci_grp1$mean*100, 1, trailing_zeros = TRUE),
@@ -811,7 +817,7 @@ test_that("pairwise_test_bin testing 3+ groups", {
811817
"%, ",
812818
round_away_0(ci_grp2$upper*100, 1, trailing_zeros = TRUE),
813819
"%)")) |>
814-
select(antigen, visitno, Comparison, ResponseStats, ResponseTest = pval, PerfectSeparation)
820+
select(antigen, visitno, Comparison, SampleSizes, ResponseStats, ResponseTest = pval, PerfectSeparation)
815821

816822
function_obj <- exampleData_BAMA |>
817823
group_by(antigen, visitno) |>

0 commit comments

Comments
 (0)