Skip to content
Prev 278073 / 398506 Next

Difference between two time series

As I said before: please dput() some working data and I'll try to work
something up.

Without it, the only thing I can reasonably suggest is that perhaps
you are looking for the window() function to be applied before
min/max. Something like:

X <- ts(1:48, start = 1, frequency = 4)
Y <- ts(1:12, start = 1, frequency = 1)

for(i in seq_along(time(Y))){
 X.temp <- window(X, start = i, end = i +1)
 Y.temp <- window(Y, start = i, end = i )
 print(max(as.numeric(X.temp)-as.numeric(Y.temp)))
}

It may be possible to do this more elegantly, but since you haven't
even said how your intervals are defined nor what the relationship
between the time stamps of X and Y are, I can't tell you.

Going forward, I'd probably recommend that you don't use the native ts
class of R, in favor of zoo or xts: ts can be a little unwieldy (to
say the least) and zoo and xts are two responses to difficulties
people have had with ts. This would so something similar in xts:

library(xts)
X <- xts(1:192, Sys.Date()+1:192)
Y <- xts(1:24, Sys.Date() + (1:24)*8)

Y.temp <- X; Y.temp[] <- NA; Y.temp[time(Y)] <- Y; Y.temp <-
na.locf(Y.temp); Diff = X - Y.temp
# create a copy of X to hold Y at a higher frequency, then fill NA's
with previous values

And then you can look at the difference on whatever frequency you want.

You're going to have to say more than "predict" the future error:
there are approximately 7.3 umpityjillion different time series models
in the world -- you'll need to decide which ones are appropriate for
your problem domain, taking into account any fundamental relationship
between the two series as well as their own dynamics.

Michael
On Thu, Nov 17, 2011 at 11:56 AM, Sarwarul Chy <sarwar.shabuj at gmail.com> wrote: