如何使用mapply计算时间序列对列表的CCF? [英] How to use mapply to calculate CCF for list of pairs of time series?

查看:49
本文介绍了如何使用mapply计算时间序列对列表的CCF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试应用此处 用于一组时间序列.为此, mapply 似乎是一个很好的方法,但我想在定义函数或使用 mapply 时存在一些问题.

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,])

我希望 try2 在将时间序列对创建为 combn(tslist, 2) 并使用 plyr::mlply 作为参数输入时间序列时直接工作,但该方法不起作用或未正确使用.

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.

有没有办法使用这种方法或任何替代方法为一组时间序列找到 CCF 矩阵?

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.

谢谢.

推荐答案

你可以试试这个:

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

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

结果存储在 corList 中,然后可以通过 corList[[1]] 访问.

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

关键点:

  • 注意函数定义中的 tsVec[[1]].ccff 本质上接收一个列表,因此是 [[]].
  • 还要注意函数定义中的return (list(...)).这需要能够将函数的所有返回值合并到调用者的单个数据结构中.
  • 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.

希望这会有所帮助.

谢谢,

GK

http://gk.palem.in/

这篇关于如何使用mapply计算时间序列对列表的CCF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆