在时间序列对象

时间:2017-05-24 20:57:00

标签: r correlation xts rollapply

我有一个xts对象,随时间推移有12个变量,间隔为15分钟。

> summary(wideRawXTSscaled)
     Index                      DO0182U09A3        DO0182U09B3       DO0182U09C3       DO0182U21A1       DO0182U21A2      
 Min.   :2017-01-20 16:30:00   Min.   :-1.09338   Min.   :-1.0666   Min.   :-0.9700   Min.   :-1.2687   Min.   :-1.00676  
 1st Qu.:2017-01-24 04:22:30   1st Qu.:-0.60133   1st Qu.:-0.6675   1st Qu.:-0.6009   1st Qu.:-0.4522   1st Qu.:-0.48525  
 Median :2017-01-27 16:15:00   Median :-0.38317   Median :-0.2742   Median :-0.1761   Median :-0.2127   Median :-0.27482  
 Mean   :2017-01-27 16:15:00   Mean   : 0.00000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.00000  
 3rd Qu.:2017-01-31 04:07:30   3rd Qu.: 0.08221   3rd Qu.: 0.2922   3rd Qu.: 0.1125   3rd Qu.: 0.1248   3rd Qu.: 0.05455  
 Max.   :2017-02-03 16:00:00   Max.   : 3.33508   Max.   : 9.2143   Max.   : 5.8473   Max.   :18.4909   Max.   :12.21382  
  DO0182U21A3        DO0182U21B1       DO0182U21B2       DO0182U21B3       DO0182U21C1       DO0182U21C2        DO0182U21C3     
 Min.   :-1.09339   Min.   :-1.0268   Min.   :-0.9797   Min.   :-1.0853   Min.   :-1.3556   Min.   :-1.15469   Min.   :-1.2063  
 1st Qu.:-0.33919   1st Qu.:-0.6020   1st Qu.:-0.5597   1st Qu.:-0.6692   1st Qu.:-0.5600   1st Qu.:-0.37291   1st Qu.:-0.3460  
 Median :-0.21082   Median :-0.3389   Median :-0.3466   Median :-0.3828   Median :-0.2138   Median :-0.16183   Median :-0.1635  
 Mean   : 0.00000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.00000   Mean   : 0.0000  
 3rd Qu.:-0.01826   3rd Qu.: 0.2105   3rd Qu.: 0.2486   3rd Qu.: 0.5624   3rd Qu.: 0.1992   3rd Qu.: 0.08052   3rd Qu.: 0.1363  
 Max.   :12.69083   Max.   : 7.7314   Max.   : 9.2900   Max.   : 7.3540   Max.   :13.7427   Max.   :13.76166   Max.   :15.8086

我希望计算每个时间间隔的每个变量之间的相关性。由于我有12个变量,我希望我的xts对象中的每个15分钟数据点都有一个12 x 12矩阵。

对于相关计算,我使用以下代码:

wideRawXTSscaledCorr <- rollapplyr(wideRawXTSscaled, 10, cor, by.column = FALSE)

&#34; 10&#34;在上面的代码中使用10个时间序列值来计算相关矩阵,因此我将在wideRawXTSscaledCorr的开头有9个NA值,并在10日返回相关值。

> wideRawXTSscaledCorr[1:10,1:5]
                    [,1]      [,2]      [,3]      [,4]       [,5]
2017-01-20 16:30:00   NA        NA        NA        NA         NA
2017-01-20 16:45:00   NA        NA        NA        NA         NA
2017-01-20 17:00:00   NA        NA        NA        NA         NA
2017-01-20 17:15:00   NA        NA        NA        NA         NA
2017-01-20 17:30:00   NA        NA        NA        NA         NA
2017-01-20 17:45:00   NA        NA        NA        NA         NA
2017-01-20 18:00:00   NA        NA        NA        NA         NA
2017-01-20 18:15:00   NA        NA        NA        NA         NA
2017-01-20 18:30:00   NA        NA        NA        NA         NA
2017-01-20 18:45:00    1 0.1590656 0.2427391 0.1987761 -0.1026246

当我将滑动窗口的值更改为任何值<10时,我会重复出现以下错误:

> wideRawXTSscaledCorr <- rollapplyr(wideRawXTSscaled, 7, cor, by.column = FALSE)
Warning messages:
1: In FUN(.subset_xts(data, (i - width + 1):i), ...) :
  the standard deviation is zero
2: In FUN(.subset_xts(data, (i - width + 1):i), ...) :
  the standard deviation is zero
3: In FUN(.subset_xts(data, (i - width + 1):i), ...) :
  the standard deviation is zero
4: In FUN(.subset_xts(data, (i - width + 1):i), ...) :
  the standard deviation is zero
5: In FUN(.subset_xts(data, (i - width + 1):i), ...) :
  the standard deviation is zero

这是我唯一可以用来查看这些错误是否仅仅是我的数据的症状的测试,还是可能是由于我所做的一些编码错误?还有其他方法我可以深入到代码中查看哪些值导致这些错误?

1 个答案:

答案 0 :(得分:2)

cor (c(0,1,0,1,1),c(1,1,1,1,1))

给出类似的错误,检查是否有任何cor测试使用的数据在其中一个向量(即第二个向量)的所有元素中是相同的......