Skip to content
Prev 277487 / 398506 Next

Difference between two time series

It's not clear what it means for the differences to be "of increasing
order" but if you simply mean the differences are increasing, perhaps
something like this will work:

library(caTools)
X = cumsum( 2*(runif(5e4) > 0.5) - 1) # Create a random Walk
Y = runmean(X, 30, endrule = "mean", align = "right")

D = X - Y # Create the difference series:

# Now we need to find the ranges of increasing: to do this, we can just lag D

sign(D - c(0, D[1:(length(D)-1)]))

If you want to find the length of each run or to find runs of a
certain length, try rle().

Michael
On Tue, Nov 15, 2011 at 2:18 PM, Sarwarul Chy <sarwar.shabuj at gmail.com> wrote: