I need to apply the Mann Kendall trend test in R to a big number (about 1 million) of different-sized time series. I've created a .txt file like this:
1 2
1 4
1 5
2 4
2 55
3 2
3 4
3 5
3 4
3 55
...
Every number of the firs column identifies a particular time-series. To obtain the statistics I'm using this code:
library(Kendall)
a=read.table("to_r.txt")
numData=1017135
for (i in 1:numData){
s1=subset(a,a$V1==i)
m=MannKendall(s1$V2)
cat(m[[1]]," ",m[[2]], " ", m[[3]]," ",m[[4]]," ", m[[5]], "\n" , file="monotonic_trend_checking.txt",append=TRUE)
}
This approach works but the problem is that it is taking ages for computation. The time-series are 800 elements long at maximum (only few, the other are shorter) so I think that the main bottle-neck is in the loop. Can you suggest a faster approach?