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

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.

# 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 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?

: .

.

0
2

:

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

corList <- aaply(combn(tslist, 2), 2, ccff)

corList, corList[[1]].

:

  • tsVec[[1]] . ccff , [[]].
  • return (list(...)) . , .

, .

,

http://gk.palem.in/

+1

ccf - , get try1.

, try2 ccf , .

> 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

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

0

All Articles