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

☆樱花仙子☆ 提交于 2019-12-25 02:40:05

问题


I am trying to apply functions described here 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.

Thanks.


回答1:


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]].

KeyPoints:

  • Note the tsVec[[1]] in the function definition. ccff essentially receives a list, hence the [[]].
  • 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.

Hope this helps.

Thank you,

GK

http://gk.palem.in/




回答2:


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



来源:https://stackoverflow.com/questions/25477116/how-to-use-mapply-to-calculate-ccf-for-list-of-pairs-of-time-series

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!