pairwise_test_cont: Pairwise Testing for a Continuous Variable

View source: R/pairwise_comparisons.R

pairwise_test_contR Documentation

Pairwise Testing for a Continuous Variable

Description

Takes a continuous variable and performs pairwise testing (t-test or wilcox test)

Usage

pairwise_test_cont(
  x,
  group,
  paired = FALSE,
  id = NULL,
  method = c("wilcox", "t.test"),
  alternative = c("two.sided", "less", "greater"),
  sorted_group = NULL,
  num_needed_for_test = 3,
  log10_stats = FALSE,
  digits = 0,
  trailing_zeros = TRUE,
  sep_val = " vs. ",
  na_str_out = "---",
  verbose = FALSE
)

Arguments

x

numeric vector (can include NA values).

group

categorical vector of group values.

paired

a logical variable indicating whether to do a paired test.

id

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

method

what test to run ("wilcox" or "t.test").

alternative

character string specifying the alternative hypothesis, must be one of "two.sided" (default), "greater" or "less". You can specify just the initial letter.

sorted_group

a vector listing the group testing order from lowest to highest.

num_needed_for_test

required sample size (per group) to perform test. Note at least 2 distinct values per group are always needed for testing.

log10_stats

specifies whether the summary statistics and p values should be calculated on log10 values. This could affect the median, mean, and p value.If TRUE, geometric mean is displayed as well as mean (sd) results on log10 x values (default is FALSE)

digits

digits to round for magnitude descriptive statistics (default = 0).

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.

verbose

a logical variable indicating if warnings and messages should be displayed.

Details

Runs wilcox_test() in the coin package, with "exact" distribution.

If sorted_group is not specified then testing order based on factor levels if group is a factor, and alphabetical order otherwise

trailing_zeros does not impact p-value column, which will be a numeric column regardless.

If paired = TRUE the descriptive statistics are shown for observations that have non-missing values for both groups.

Value

Returns a data frame with all possible pairwise comparisons:

  • Comparison - Comparisons made

  • SampleSizes - number of samples per group

  • Median_Min_Max - Median [Min, Max] per group

  • Mean_SD - Mean(sd) per group (if log10_stats = FALSE)

  • Mean - Geometric mean per group (if log10_stats = TRUE)

  • log_Mean_SD - Mean(sd) per group on log10 x scale (if log10_stats = TRUE)

  • MagnitudeTest - wilcox/t-test test p value

  • PerfectSeparation - logical flag indicating perfect separation

Returns a data frame with all possible pairwise comparisons. Variables include Comparison, SampleSizes, Median_Min_Max (group stats; median [min, max]), Mean_SD (group stats; mean (sd)), MagnitudeTest (wilcox/t-test p-value), PerfectSeparation (a logical flag indicating if there is perfect separation).

Examples


x_example <- c(NA, sample(1:50, 50), sample(51:99, 49), 1111,2222)
group_example <- c(rep(1:4,25),'a','a')

pairwise_test_cont(x_example,group_example, num_needed_for_test = 2)

pairwise_test_cont(
x_example,group_example, alternative = "less",
  sorted_group = c(1:4, 'a'), num_needed_for_test = 2, , digits = 3)

# using log10 computations
pairwise_test_cont(
x_example,group_example, alternative = "less", log10_stats = TRUE,
  sorted_group = c(1:4, 'a'), num_needed_for_test = 2, digits = 3)



# Examples with Real World Data
library(dplyr)

# BAMA Assay Data Example
data(exampleData_BAMA)

## Group Comparison
group_testing_tibble <- exampleData_BAMA %>%
   group_by(antigen, visitno) %>%
   summarise(pairwise_test_cont(x = magnitude,
                                group = group,
                                paired = FALSE,
                                method = 'wilcox',
                                alternative = "less",
                                sorted_group = c(1,2),
                                digits = 3,
                                num_needed_for_test = 3,
                                verbose = TRUE),
            .groups = "keep")


## Timepoint Comparison
timepoint_testing_dt <- exampleData_BAMA %>%
                       group_by(antigen, group) %>%
                       summarise(pairwise_test_cont(x = magnitude,
                                                   group = visitno,
                                                   paired = TRUE,
                                                   id = pubID,
                                                   method = 'wilcox',
                                                   sorted_group = c(0,1,2),
                                                   alternative = 'less',
                                                   num_needed_for_test = 3,
                                                   digits = 3,
                                                   trailing_zeros = TRUE,
                                                   sep_val = ' vs. ',
                                                   verbose = TRUE),
                                 .groups = "keep")


# ICS Assay Data Example
data(exampleData_ICS)

## Group Comparison
# using dplyr
exampleData_ICS %>%
group_by(Stim, Parent, Population, Visit) %>%
summarise(pairwise_test_cont(x = PercentCellNet,
                            group = Group,
                            paired = FALSE,
                            method = 'wilcox',
                            alternative = 'less',
                            sorted_group = c(1,2,3,4),
                            num_needed_for_test = 3,
                            digits = 4,
                            trailing_zeros = TRUE,
                            sep_val = ' vs. ',
                            verbose = TRUE),
         .groups = "keep")

# Timepoint Comparison
timepoint_testing_dt <- exampleData_ICS %>%
                       group_by(Stim, Parent, Population, Group) %>%
                       summarise(pairwise_test_cont(x = PercentCellNet,
                                                    group = Visit,
                                                    paired = TRUE,
                                                    id = pubID,
                                                    method = 'wilcox',
                                                    sorted_group = c(0,1,2),
                                                    alternative = 'less',
                                                    num_needed_for_test = 3,
                                                    digits = 4,
                                                    trailing_zeros = TRUE,
                                                    sep_val = ' vs. ',
                                                    verbose = TRUE),
                       .groups = "keep")



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