I was wondering if there is a package or a new way that can perform KS-test between each columns of a matrix more quickly than use 2 loops? Thanks!
all <- c()
for (i in (1:(ncol(a)-1))){
for (j in ((i+1):ncol(a))){
res <- ks.test(i,j)
all <- rbind(all,res)
}
}
Suppose we have a 5 column matrix, each containing 10 draws from a different normal distribution:
We can get all unique combinations of 2 columns using
combnand applyks.testto these pairs of columns, retreiving all the p values in a single vector like this:To make it clearer which comparison these p values refer to, we can store the results in a 5 x 5 matrix that will be the same as the result of your loop. Note though, that we have only had to run
ks.test10 times instead of 25 because the diagonal is already known to be p = 1, and the matrix is symmetrical:Alternatively, you could give
pthe names of the columns compared:Created on 2022-09-10 with reprex v2.0.2