[R] Difference between two time series

R. Michael Weylandt michael.weylandt at gmail.com
Mon Nov 21 17:20:27 CET 2011


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:
> Hello Michael,
> Thanks again for your reply. Actually, I am working with wind data.
>
> I have some sample data for actual load.
>
> scan("/home/sam/Desktop/tt.dat") ->tt   ## This is the input for the  actual
> output of the generation
> t = ts(tt, start=8, end=24, frequency=1,)
>
> I have another random sequence for Generator Dispatch
>
> scan("/home/sam/Desktop/ss.dat") ->ss## Input for the Generator Dispatch
> s= ts(ss, start=10,end=22, frequency=1)
>
> What I want to do now to take the Max and Min difference  of this two
> sequence (t and s) over a fixed  time interval.
>  Something like, X=max(t-s, start=10, end=12) # I have an error here, I want
> he difference between two over an interval
>                        Y=min(t-sx, start=10, end=12)
>
> Then predict the max and min error between time t and t+1 on the basis of
> information that I have at t-1. Thanks again.
> Sam
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Difference-between-two-time-series-tp819843p4080672.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list