[R] Comparing two time series?

Spencer Graves spencer.graves at pdf.com
Thu May 4 17:58:07 CEST 2006


	  The standard "ccf" function requires two series sampled
concurrently at equally spaced points in time.  I think the simplest
thing would be to try to transform your data into this form and use
"ccf".  The "zoo" package provides "aggregate.zoo" and "na.approx" that
can be used to do this, after you've first selected an appropriate
series of regular points in time.  I found vignette("zoo") especially
useful.  [You may know that vignettes are essential Adobe Acrobat files
with companion R script files that you can process one line at a time
while reading the text, modifying R commands and trying different things
to test your understanding.  The acrobat file is opened by "printing"
vignette("zoo").  Under Rgui, edit(vignette("zoo")) will open the script
in a local window.  Under XEmacs, Stangle(vignette("zoo")$file) will
create a file "zoo.R" in the working directory, getwd(), which you can
then open in XEmacs.]

	  If you would like more help from this listserve, please submit
another post.  Be advised, however, that there is substantive anecdotal
evidence suggesting that questions more consistent with the posting
guide! "www.R-project.org/posting-guide.html" are more likely to
generate quicker, more informative replies.  This is particularly true
if you can express your question in terms of a relatively simple,
self-contained, example, like the following:

> x <- rep(1:3, 4)
> y <- rep(1:4, 3)
> x[3] <- NA
> y[4] <- NA
> ccf(x, y)
Error in na.fail.default(ts.union(as.ts(x), as.ts(y))) :
	missing values in object

	  Some poor semi-literate such as I can then copy these few lines into
R, and in a minute or so test something like the following:

library(zoo)
  print(ccf(x,y, na.action=na.approx))

Autocorrelations of series 'X', by lag

     -7     -6     -5     -4     -3     -2     -1      0      1      2
     3
-0.198  0.251  0.018 -0.159  0.066  0.269  0.000  0.106  0.207  0.040
-0.026
      4      5      6      7
-0.044  0.040  0.172 -0.282

	  Without such examples, I don't know how close your series are to
being sampled regularly, how much they overlap, what other things you've
tried, etc.

	  hope this helps,
	  spencer graves

Robert Lundqvist wrote:

> I have got pairs of time series, where one usually is shorter (n typically
> about 5400) than the other (n typically about 52000). I would like to
> calculate the ccf for these series, but I haven't found a smart way to let
> the shorter "slide" along the longer one in steps.
> 
> Manually splitting the longer series into shorter ones of the same length
> as the shorter is possible, but tedious. Any suggestions, either for doing
> it (i e the ccf calculations for series of different lengths) in one round
> or for doing a split of a long series into shorter pieces? (I know I have
> seen a description of the later in the documentation, but now I can't
> find where it was...)
> 
> Robert
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list