【问题标题】:How to use mapply to calculate CCF for list of pairs of time series?如何使用 mapply 计算时间序列对列表的 CCF?
【发布时间】:2014-08-24 23:06:13
【问题描述】:

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

这是示例代码,我在其中发现返回的数据帧格式存在一些差异,这可能是错误的根源。

# 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 将时间序列作为参数输入时直接工作,但这种方法不起作用或使用不正确。

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

编辑:试图使问题更加清晰和具体。

谢谢。

【问题讨论】:

    标签: r time-series mapply


    【解决方案1】:

    你可以试试这个:

    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(...))。这需要能够将函数的所有返回值合并到调用者的单个数据结构中。

    希望这会有所帮助。

    谢谢,

    GK

    http://gk.palem.in/

    【讨论】:

    • +1 感谢您的回复。这是上面的mapply函数不起作用的原因吗?我只是想知道我在那里错过了什么,也许这对获取配对名称有用。如何使用 aaply 获取对名称?
    • mapplysapply 相似,我认为这里并不适合您的目的。带有 aaply 的名称对非常简单,这已经在前面问题的答案中指出。供您参考:corList &lt;- aaply(combn(names(tslist), 2), 2, function(x) paste(x[1], '-', x[2]))
    • 谢谢。我在想一些带有 aaply 的班轮。喜欢在名称上使用 lapply 或在适用时使用 sapply。
    【解决方案2】:

    ccf 无法获取时间序列对象 - 这是 try1 中的 get 所做的。

    因此,在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

    【讨论】:

    • 我知道,这就是为什么 ccffunction 被定义为从字符串中获取系列。我首先尝试将 combn 函数直接用作 combn(tslist, 2) 而不是名称。我提到这一点是因为有使用 combn 和 mlply 函数的示例,其中可以直接应用函数,并且想知道是否可以以某种方式直接在 ts 列表上应用 ccf。
    猜你喜欢
    • 2015-12-16
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2011-07-02
    • 2021-05-14
    • 1970-01-01
    相关资源
    最近更新 更多