1
I have the following vector:
a <- c(1,2,3,1,1,2,3,4,2,5,6,7,8,4,6,4,3,2,1)
I would like to find the largest increasing and decreasing subsequence of it. The output would be the indices of the sequence of vectors:
ss_cres <- c(9,10,11,12,13) # ou seja 2,5,6,7 e 8
ss_decres <- c(15,16,17,18,19) # ou seja 6,4,3,2 e 1
This is a common operation used in time series analysis. Is there a package in R that does this? If not, any hint for a general idea for the experiment-based algorithm?
So far I have done this for the largest decreasing series:
long_seq_decres<- function(seq){
seq_inds <- split(1:length(seq), cumsum(c(0, diff(seq)) > 0))
ind_list <- lengths(seq_inds)
long_seq_inds <- seq_inds[which.max(ind_list)]
return(long_seq_inds)
}
It worked well, however I found it a little slow function. Any idea to optimize this function?
Very good! Thanks for the tip!
– Artur_Indio