Provides a pipe-friendly framework to perform the two-sample Kolmogorov-Smirnov test, comparing the (empirical) distributions of a numeric variable between two groups. Wrapper around the R base function ks.test().

When the grouping factor contains more than two levels, pairwise Kolmogorov-Smirnov tests are automatically performed, with p-value adjustment.

ks_test(
  data,
  formula,
  comparisons = NULL,
  ref.group = NULL,
  p.adjust.method = "holm",
  alternative = "two.sided",
  exact = NULL,
  detailed = FALSE
)

Arguments

data

a data.frame containing the variables in the formula.

formula

a formula of the form x ~ group where x is a numeric variable giving the data values and group is a factor with two or more levels giving the corresponding groups.

comparisons

A list of length-2 vectors specifying the groups of interest to be compared. For example to compare groups "A" vs "B" and "B" vs "C", the argument is as follow: comparisons = list(c("A", "B"), c("B", "C")).

ref.group

a character string specifying the reference group. If specified, for a given grouping variable, each of the group levels will be compared to the reference group (i.e. control group). If ref.group = "all", pairwise two sample tests are performed for comparing each grouping variable level against all (i.e. basemean).

p.adjust.method

method to adjust p values for multiple comparisons. Used when pairwise comparisons are performed. Allowed values include "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none". If you don't want to adjust the p value (not recommended), use p.adjust.method = "none".

alternative

indicates the alternative hypothesis and must be one of "two.sided" (default), "less", or "greater". You can specify just the initial letter of the value, but the argument name must be given in full. See ‘Details’ for the meanings of the possible values.

exact

NULL or a logical indicating whether an exact p-value should be computed. See ‘Details’ for the meaning of NULL.

detailed

logical value. Default is FALSE. If TRUE, a detailed result is shown.

Value

return a data frame with some of the following columns:

  • .y.: the y variable used in the test.

  • group1,group2: the compared groups in the pairwise tests.

  • n1,n2: sample counts.

  • statistic: the value of the test statistic D (the maximum difference between the two empirical cumulative distribution functions).

  • p: p-value.

  • p.adj: the adjusted p-value.

  • method: the statistical test used to compare groups.

  • p.signif, p.adj.signif: the significance level of p-values and adjusted p-values, respectively.

  • alternative: the alternative hypothesis.

The returned object has an attribute called args, which is a list holding the test arguments.

Examples

# Two-samples test
#:::::::::::::::::::::::::::::::::::::::::
ToothGrowth %>% ks_test(len ~ supp)
#> # A tibble: 1 × 7
#>   .y.   group1 group2    n1    n2 statistic      p
#> * <chr> <chr>  <chr>  <int> <int>     <dbl>  <dbl>
#> 1 len   OJ     VC        30    30     0.333 0.0617

# Pairwise comparisons (more than two groups)
#:::::::::::::::::::::::::::::::::::::::::
ToothGrowth %>% ks_test(len ~ dose)
#> # A tibble: 3 × 9
#>   .y.   group1 group2    n1    n2 statistic        p    p.adj p.adj.signif
#> * <chr> <chr>  <chr>  <int> <int>     <dbl>    <dbl>    <dbl> <chr>       
#> 1 len   0.5    1         20    20      0.75 7.03e- 6 1.41e- 5 ****        
#> 2 len   0.5    2         20    20      0.95 3.05e-10 9.14e-10 ****        
#> 3 len   1      2         20    20      0.6  1.06e- 3 1.06e- 3 **          

# Comparison against a reference group
#:::::::::::::::::::::::::::::::::::::::::
ToothGrowth %>% ks_test(len ~ dose, ref.group = "0.5")
#> # A tibble: 2 × 9
#>   .y.   group1 group2    n1    n2 statistic        p    p.adj p.adj.signif
#> * <chr> <chr>  <chr>  <int> <int>     <dbl>    <dbl>    <dbl> <chr>       
#> 1 len   0.5    1         20    20      0.75 7.03e- 6 7.03e- 6 ****        
#> 2 len   0.5    2         20    20      0.95 3.05e-10 6.09e-10 ****