不等大小的动物园对象之间的滚动相关性

时间:2013-08-22 17:53:32

标签: r

我有两个大小不等的动物园对象(流入和流出)。流出值滞后于一些未知的时间。我想确定较小的流出对象(6行)和较大的流入对象(许多行)之间的相关性,一次6行,从流入开始时间开始,递增1,并找到最高相关的周期。我认为这被称为“滑动窗口”比较。我尝试了许多不同的方法,但没有成功,使用“rollapply”函数来执行此操作,但因为两个对象之间的大小差异而得到错误。希望有人能理解我想要提出的问题并提供解决方案。下面是我的数据的一部分以及我如何尝试使用rollapply函数的示例。

> inflow
(03/14/13 07:00:00) 11.20451
(03/14/13 07:02:00) 11.03810
(03/14/13 07:04:00) 11.03012
(03/14/13 07:06:00) 11.09517
(03/14/13 07:08:00) 10.90878
(03/14/13 07:10:00) 11.23285
(03/14/13 07:12:00) 11.14890
(03/14/13 07:14:00) 11.17002
(03/14/13 07:16:00) 11.38342
(03/14/13 07:18:00) 11.70833
(03/14/13 07:20:00) 11.93776
(03/14/13 07:22:00) 12.17832
(03/14/13 07:24:00) 12.39648
(03/14/13 07:26:00) 12.24020
(03/14/13 07:28:00) 12.18667
(03/14/13 07:30:00) 12.45410
(03/14/13 07:32:00) 12.50012
(03/14/13 07:34:00) 12.54736
(03/14/13 07:36:00) 13.05010
(03/14/13 07:38:00) 13.06495
(03/14/13 07:40:00) 13.14084
(03/14/13 07:42:00) 12.92427
(03/14/13 07:44:00) 12.98699
(03/14/13 07:46:00) 12.84172
(03/14/13 07:48:00) 12.87263
(03/14/13 07:50:00) 12.51861
(03/14/13 07:52:00) 12.98763
(03/14/13 07:54:00) 12.31124
(03/14/13 07:56:00) 12.33696
(03/14/13 07:58:00) 12.49630
(03/14/13 08:00:00) 12.40648
(03/14/13 08:02:00) 11.87164
(03/14/13 08:04:00) 12.76058
(03/14/13 08:06:00) 12.50016
(03/14/13 08:08:00) 12.68696
(03/14/13 08:10:00) 12.88447
(03/14/13 08:12:00) 12.33336
(03/14/13 08:14:00) 13.06670
(03/14/13 08:16:00) 13.15070
(03/14/13 08:18:00) 12.82410
(03/14/13 08:20:00) 12.91953

outflow2

(03/14/13 07:54:00) (03/14/13 07:56:00) (03/14/13 07:58:00) (03/14/13 08:00:00) (03/14/13 08:02:00) (03/14/13 08:04:00) 
           11.51110            11.11878            11.05775            11.11303            10.95417            10.98035

使用:

> test <- rollapply(inflow, width = 6, by = 1, FUN = cor(inflow, outflow))
Error in cor(inflow, outflow) : incompatible dimensions

1 个答案:

答案 0 :(得分:1)

试试这个:

rollapply(inflow, 6, cor, y = outflow)

这计算

value <- c( cor(inflow[1:6], outflow), cor(inflow[2:7], outflow), ...etc... )
ix <- seq(3, length = length(inflow) - 6 + 1)
zoo(value, time(inflow)[ix])

根据您想要获得的内容,您可能还需要align=参数。