I am trying to apply the described functions here for a set of time series. For this, mapply seems like a good approach, but I think there is some kind of problem either in the function definition or when using mapply.
Here is an example code where I found some discrepancy in the format of the returned data frame and may be the source of the error.
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.
}
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)
tsmts <- do.call(cbind, tslist)
class(tsmts)
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 time series pairs are created as combn (tslist, 2) and use plyr :: mlply to enter time series as arguments, but this approach does not work or does not work correctly.
Is there a way to find the CCF matrix for a set of time series using this approach or any alternatives?
: .
.