R中不同时间序列数据值的互相关 [英] Cross correlation of different time series data values in R

查看:633
本文介绍了R中不同时间序列数据值的互相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个15天的5位时间序列数据(以日格式),存储为matrix.数据的结构是

I have a time series data (in day format) of 5 places for 15 days stored as a matrix. The structure of data is

meter_daywise<-structure(c(24.4745528484842, 21.5936510486629, 58.9120896540103, 
49.4188338105575, 568.791971631185, 27.1682608244523, 23.3482757939878, 
74.710966227615, 82.6947717673258, 704.212340152625, 23.7581651139442, 
21.154634543401, 64.9680107059625, 420.903181621575, 672.629513512841, 
128.22871420984, 601.521395359887, 74.6606087800009, 335.87599588534, 
576.451039365565, 641.329910104503, 1010.78497435794, 72.6159099850862, 
225.153924410613, 582.652388366075, 529.082673064516, 1151.87208010484, 
76.9939865858514, 198.567927906582, 641.511944831027, 280.685806121688, 
998.647413766557, 73.2033388656998, 337.966543898629, 847.24874747014, 
76.7357959402453, 1065.75153722813, 220.286408574643, 301.120955096701, 
552.703945876515, 206.496034127105, 1053.49582469841, 206.187963352323, 
219.791668265415, 655.496754449233, 172.87981151456, 1018.01514547636, 
544.551001017031, 227.116788647859, 656.566145328213, 373.484460701849, 
1503.65562864399, 117.732932835236, 251.383369528816, 802.871808716031, 
150.471195301885, 1414.88799728991, 14.6490905509617, 203.429955747521, 
622.731792495107, 548.093577186778, 1076.5618643676, 15.5135269483705, 
256.581499048612, 644.572474965446, 63.2304035656636, 1538.07906461011, 
15.0980567507389, 261.513768642083, 622.17970609429, 210.786387991582, 
996.998005580537, 15.8138368515615, 157.390773346978, 573.477606081416
), .Dim = c(5L, 15L), .Dimnames = list(c("apFac_401", "apFac_403", 
"apFac_501", "apFac_503", "apFac_601"), c("D1", "D2", "D3", "D4", 
"D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12", "D13", "D14", 
"D15")))

更早之前,我使用来计算不同系列之间的相关性

Earlier, I was calculating correlation between different series using

library(corrplot)# for plotting correlation matrix
corrplot(cor(t(meter_daywise)),method = "number",type="lower")# have taken transpose of above structure

因此,我得到了一个很好的相关矩阵,该矩阵显示了不同序列之间的相关性.

So, with this I am getting a nice correlation matrix showing correlation between different series.

但是,在观察相关值时,我发现有些问题,并且在搜索时发现了这个链接,其中提到我们需要计算互相关.因此,现在我需要像上面那样计算互相关矩阵.因此,我发现了一些功能,例如

But, while observing correlation values I find something is wrong and on searching I found this link, where it mentions that we need to compute cross-correlation. Therefore, now I need to calculate cross correlation matrix like the above one. Accordingly, I found some functions like

  1. ccf() #in base packages
  2. diss(meter_daywise,METHOD = "CORT",deltamethod = "DTW")#in TSclust package

上述功能面临两个问题:

I am facing two issues with above functions:

  1. ccf不将完整矩阵作为输入
  2. diss()接受输入矩阵并生成一些矩阵,但是在观察值的同时,我发现它不是互相关矩阵,因为这些值不在-11之间.
  1. ccf do not take full matrix as input
  2. diss() takes input matrix and produces some matrix, but while observing the values I find that it is not a cross-correlation matrix because the values are not between -1 and 1.

所以问题是我们如何计算R中不同时间序列值的互相关矩阵?

So the question is how do we compute cross-correlation matrix of different time-series values in R?

推荐答案

您可以将matrix转换为ts对象的list,然后使用do.call.

You can transform matrix to list of ts objects then use do.call.

meter_daywise <- structure(c(24.4745528484842, 21.5936510486629, 58.9120896540103, 
49.4188338105575, 568.791971631185, 27.1682608244523, 23.3482757939878, 
74.710966227615, 82.6947717673258, 704.212340152625, 23.7581651139442, 
21.154634543401, 64.9680107059625, 420.903181621575, 672.629513512841, 
128.22871420984, 601.521395359887, 74.6606087800009, 335.87599588534, 
576.451039365565, 641.329910104503, 1010.78497435794, 72.6159099850862, 
225.153924410613, 582.652388366075, 529.082673064516, 1151.87208010484, 
76.9939865858514, 198.567927906582, 641.511944831027, 280.685806121688, 
998.647413766557, 73.2033388656998, 337.966543898629, 847.24874747014, 
76.7357959402453, 1065.75153722813, 220.286408574643, 301.120955096701, 
552.703945876515, 206.496034127105, 1053.49582469841, 206.187963352323, 
219.791668265415, 655.496754449233, 172.87981151456, 1018.01514547636, 
544.551001017031, 227.116788647859, 656.566145328213, 373.484460701849, 
1503.65562864399, 117.732932835236, 251.383369528816, 802.871808716031, 
150.471195301885, 1414.88799728991, 14.6490905509617, 203.429955747521, 
622.731792495107, 548.093577186778, 1076.5618643676, 15.5135269483705, 
256.581499048612, 644.572474965446, 63.2304035656636, 1538.07906461011, 
15.0980567507389, 261.513768642083, 622.17970609429, 210.786387991582, 
996.998005580537, 15.8138368515615, 157.390773346978, 573.477606081416
), .Dim = c(5L, 15L), .Dimnames = list(c("apFac_401", "apFac_403", 
"apFac_501", "apFac_503", "apFac_601"), c("D1", "D2", "D3", "D4", 
"D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12", "D13", "D14", 
"D15")))

tss <- unlist(apply(meter_daywise, 1 , function(x) list(ts(x))), recursive = FALSE)
tssu <- do.call(ts.union, tss)
plot(acf(tssu))

输出:

这篇关于R中不同时间序列数据值的互相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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