pairwise_test_bin: Binary Variable Pairwise Testing

View source: R/pairwise_comparisons.R

pairwise_test_binR Documentation

Binary Variable Pairwise Testing

Description

Takes a binary variable (e.g., response status) and performs pairwise testing. Performs either Barnard, Fisher's, or Chi-sq test for unpaired data and McNemar's test for paired data.

Usage

pairwise_test_bin(
  x,
  group,
  id = NULL,
  method = c("barnard", "fisher", "chi.sq", "mcnemar"),
  barnard_method = c("z-pooled", "z-unpooled", "boschloo", "santner and snell", "csm",
    "csm approximate", "csm modified"),
  alternative = c("two.sided", "less", "greater"),
  sorted_group = NULL,
  num_needed_for_test = 3,
  conf_level = 0.95,
  digits = 1,
  trailing_zeros = TRUE,
  sep_val = " vs. ",
  na_str_out = "---",
  latex_output = FALSE,
  verbose = FALSE,
  ...
)

Arguments

x

numeric vector (0/1) or logical vector or (F/T) (can include NA values)

group

categorical vector of group values.

id

vector which contains the id information (so x values can be linked between groups). Only used and must be present when method = 'mcnemar'.

method

what test to run, "barnard" (default), "fisher" ,"chi.sq" , or "mcnemar")

barnard_method

indicates the Barnard method for finding tables as or more extreme than the observed table: must be either "Z-pooled", "Z-unpooled", "Santner and Snell", "Boschloo", "CSM", "CSM approximate", or "CSM modified". Only used when method = 'barnard'

alternative

character string specifying the alternative hypothesis, must be one of "two.sided" (default), "greater" or "less". You can specify just the initial letter. Only "two.sided" available for ⁠method = 'chi.sq' or 'mcnemar'⁠

sorted_group

a vector listing the group testing order from lowest to highest, if performing one sided tests

num_needed_for_test

required sample size (per group) to perform test.

conf_level

The level of confidence to be used in the confidence interval.

digits

digits to round for descriptive statistics

trailing_zeros

logical indicating if trailing zeros should be included in the descriptive statistics (i.e. 0.100 instead of 0.1). Note if set to TRUE, output is a character vector.

sep_val

value to be pasted between the two measures. Default is ' vs. '.

na_str_out

the character string in the output table that replaces missing values.

latex_output

will this table be used for latex output (default is FALSE)

verbose

a logical variable indicating if warnings and messages should be displayed

...

other parameters to pass to Exact::exact.test when running Barnard test

Details

If all values of x are NA, the function will return NULL. This is to allow for nice return when looping through function with dplyr group_by and group_modify

For one sided tests if sorted_group = NULL than the factor level order of group is respected, otherwise the levels will set to alphabetical order (i.e. if alternative = less then testing a < b ).

If planning on using the table in a latex document then set latex_output = TRUE. This will set the ⁠%⁠ symbol to ⁠\\%⁠ in the binary percentages

Value

Returns a data frame with all possible pairwise comparisons. Variables include Comparison, ResponseStats (group stats; number positive / number = rate (Wilson CI Bounds)), ResponseTest (fisher/chisq p value), PerfectSeparation (a logical flag indicating if one group if 0% and the other 100%)

Examples


set.seed(1)
x_example = c(NA,sample(0:1,50,replace = TRUE, prob = c(.75,.25)),
  sample(0:1,50,replace = TRUE, prob = c(.25,.75)),0,0,1,1)
group_example = c(rep(1,25),NA,rep(2,25),rep(3,25),rep(4,25),'a','a','b','b')

pairwise_test_bin(x_example,group_example, num_needed_for_test = 2)

pairwise_test_bin(
x_example,group_example, alternative = "less",
  sorted_group = c(1:4, 'a','b'),num_needed_for_test = 2)

# Examples with Real World Data
library(dplyr)

# BAMA Assay Data Example
data(exampleData_BAMA)

## Group Comparison
group_testing <- exampleData_BAMA %>%
   group_by(antigen, visitno) %>%
   group_modify(~ as.data.frame(
       pairwise_test_bin(x = .$response, group = .$group,
               method = 'barnard', alternative = 'less',
               num_needed_for_test = 3, digits = 1,
               trailing_zeros = TRUE, sep_val = ' vs. ', verbose = TRUE)))


## Timepoint Comparison
timepoint_testing <- exampleData_BAMA %>%
   group_by(antigen, group) %>%
   group_modify(~ as.data.frame(
       pairwise_test_bin(x = .$response, group = .$visitno, id = .$pubID,
               method = 'mcnemar', num_needed_for_test = 3, digits = 1,
               trailing_zeros = TRUE, sep_val = ' vs. ', verbose = TRUE)))

# ICS Assay Data Example
data(exampleData_ICS)

## Group Comparison
group_testing <- exampleData_ICS %>%
   group_by(Stim, Parent, Population, Visit) %>%
   group_modify(~ as.data.frame(
       pairwise_test_bin(x = .$response, group = .$Group , alternative = 'greater',
               method = 'barnard', num_needed_for_test = 3, digits = 1,
               trailing_zeros = TRUE, sep_val = ' vs. ', verbose = TRUE)))

## Timepoint Comparison
timepoint_testing <- exampleData_ICS %>%
   group_by(Stim, Parent, Population, Group) %>%
   group_modify(~ as.data.frame(
       pairwise_test_bin(x = .$response, group = .$Visit, id = .$pubID,
               method = 'mcnemar', num_needed_for_test = 3, digits = 1,
               trailing_zeros = TRUE, sep_val = ' vs. ', verbose = TRUE)))


FredHutch/VISCfunctions documentation built on Oct. 14, 2024, 11:33 p.m.