How to use mapply to calculate CCF for list of pairs of time series?


I am trying to apply functions described <a href="https://stackoverflow.com/questions/10369109/finding-lag-at-which-cross-correlation-is-maximum-ccf" rel="nofollow">here</a> for a set of time series. For this, mapply seems to be a good approach but I guess there is some problem either in defining the function or in using mapply.

Here is the example code, where I found some discrepancy in the format of dataframe being returned and might be the source of error.

# define the function to apply ccffunction <- function(x, y, plot = FALSE){ ts1 = get(x) ts2 = get(y) d <- ccf(ts1, ts2,lag.max = 24, plot = plot) cor = d$acf[,,1] lag = d$lag[,,1] dd <- data.frame(lag = lag, ccf = cor) return(t(dd)) # if I dont take transpose, not getting a df but info on the contents. # It seems that mapply is adding the results from two series vertically ; # and main part may be to define correct format of object returned } # List of time series simulated for testing results rm(list = ls()) set.seed(123) ts1 = arima.sim(model = list(ar=c(0.2, 0.4)), n = 10) ts2 = arima.sim(model = list(ar=c(0.1, 0.2)), n = 10) ts3 = arima.sim(model = list(ar=c(0.1, 0.8)), n = 10) assign("series1", ts1) assign("series2" , ts2) assign("series3" , ts3) tslist <- list(series1 = ts1, series2 = ts2, series3 = ts3) # convert to mts object if it makes any difference tsmts <- do.call(cbind, tslist) class(tsmts) # create pairs of time series using combn function tspairs <- combn(names(tslist), 2) tspairs tspairs2 <- combn(colnames(tsmts), 2) tspairs2 try1 <- mapply(ccffunction, tspairs[1, ], tspairs[2, ]) try2 <- mapply(function(x, y){ccf(x, y)}, tspairs2[1, ], tspairs2[2,])

I expected try2 to work directly when pairs of time series are created as combn(tslist, 2) and using plyr::mlply to input time series as arguments but that approach does not work or not using correctly.

Is there a way to find CCF matrix for a set of time series using this approach or any alternatives ?

Edits : Tried to make the question more clear and specific.



You can try this:

ccff <- function(tsVec) { return (list(ccf(tsVec[[1]], tsVec[[2]], plot=FALSE))) } corList <- aaply(combn(tslist, 2), 2, ccff)

The results are stored in corList which can then accessed through corList[[1]].


<ul><li>Note the tsVec[[1]] in the function definition. ccff essentially receives a list, hence the [[]].</li> <li>Also note the return (list(...)) in the function definition. That is needed to be able to merge all the return values from the function into a single data structure from the caller.</li> </ul>

Hope this helps.

Thank you,


<a href="http://gk.palem.in/" rel="nofollow">http://gk.palem.in/</a>


ccf cannot get the time-series object - which is what the get in try1 does.

So, in try2 you are simply passing ccf two strings, because it cannot see the time-series objects.

> ccf("a_string","another_string") Error in acf(X, lag.max = lag.max, plot = FALSE, type = type, na.action = na.action) : 'x' must be numeric

and mapply(function(x, y){ccf(x, y)}, tspairs2[1, ], tspairs2[2,]) Error in acf(X, lag.max = lag.max, plot = FALSE, type = type, na.action = na.action) : 'x' must be numeric


  • Is it possible to simulate tri-state checkboxes in Microsoft TreeView Control 6.0 (MSComctlLib.TreeC
  • Can we stream only flash videos throught RTMP?
  • xp_regread() returned error 5, 'Access is denied.'
  • Problems in creating unit test for ASP .NET MVC
  • Projecting Objects Out Of Collision
  • Use SendInput to lock the computer
  • Modifying files nested in tar archive
  • iOS 6 dateFromString returns wrong date
  • Bash if statement with multiple conditions
  • Why use database factory in asp.net mvc?
  • Web.config system.webserver errors
  • Breaking out column by groups in Pandas
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • ImageMagick, replace semi-transparent white with opaque white
  • R - Combining Columns to String Based on Logical Match
  • What is Eclipse's Declaration View used for?
  • output of program is not same as passed argument
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • sending/ receiving email in Java
  • Jquery - Jquery Wysiwyg return html as a string
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Akka Routing: Reply's send to router ends up as dead letters
  • 0x202A in filename: Why?
  • SVN: Merging two branches together
  • AT Commands to Send SMS not working in Windows 8.1
  • Comma separated Values
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • File not found error Google Drive API
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • Is it possible to post an object from jquery to bottle.py?
  • unknown Exception android
  • Append folder name and increment by 1 using batch script
  • Checking variable from a different class in C#
  • reshape alternating columns in less time and using less memory
  • Unable to use reactive element in my shiny app
  • How do I use LINQ to get all the Items that have a particular SubItem?