I have a matrix in which each row is a sample from a distribution. I want to do a rolling comparison of the distributions using ks.test
and save the test statis
Here's a dplyr
solution that gets the same result as your loop. I have my doubts if this is actually faster than the loop, but perhaps it can serve as a first step towards a solution.
require(dplyr)
mt %>%
as.data.frame %>%
mutate_each(funs(lag)) %>%
cbind(mt) %>%
slice(-1) %>%
rowwise %>%
do({
x = unlist(.)
n <- length(x)
data.frame(ks = ks.test(head(x, n/2), tail(x, n/2))$statistic)
}) %>%
unlist %>%
c(NA, .) %>%
matrix