Performs a two-sample Kolmogorov-Smirnov test on each column of a matrix, comparing the rows labeled TRUE with the rows labeled FALSE.

tgs_ks(x, labels, alternative = c("two.sided", "less", "greater"))

Arguments

x

a numeric matrix or a data frame of numeric columns. Rows are observations and columns are features.

labels

a logical vector of length nrow(x). Rows labeled TRUE form the first sample and rows labeled FALSE the second.

alternative

one of "two.sided", "less" or "greater", with the same meaning as in ks.test.

Value

A numeric matrix with ncol(x) rows and 2 columns named "D" and "pval". Row names are the column names of x. Columns where one of the samples has no non-missing values are NA.

Details

For each column j, the statistic and the p-value are equal to those of ks.test(x[labels, j], x[!labels, j], alternative = alternative, exact = FALSE): D is the maximal difference between the empirical cumulative distribution functions of the two samples (two-sided), or the maximal difference in the direction of alternative, and the p-value is computed from the asymptotic (Kolmogorov) distribution of the statistic, also when there are ties.

Missing values (NA, NaN) are dropped from each column separately, and rows whose label is NA are ignored. Infinite values are kept, as in ks.test.

See also

Examples

# \donttest{
# Note: all the available CPU cores might be used

set.seed(42)
x <- matrix(rnorm(2000 * 100), ncol = 100)
labels <- rep(c(TRUE, FALSE), each = 1000)
x[labels, 1:10] <- x[labels, 1:10] + 0.5
result <- tgs_ks(x, labels)
head(result)
#>          D         pval
#> [1,] 0.210 1.408132e-19
#> [2,] 0.224 3.234969e-22
#> [3,] 0.209 2.140981e-19
#> [4,] 0.235 2.075474e-24
#> [5,] 0.253 3.178883e-28
#> [6,] 0.228 5.304759e-23
# }