How to use mapply to calculate CCF for list of pai

2019-09-13 22:48发布

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.

2条回答
唯我独甜
2楼-- · 2019-09-13 23:32

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/

查看更多
虎瘦雄心在
3楼-- · 2019-09-13 23:43

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

查看更多
登录 后发表回答