0

I have an xts object with 12 variables over time in 15 minute intervals.

> 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

I wish to compute the correlations between each of the variables for each interval of time. Since i have 12 variables I expect to have a 12 x 12 matrix for each of the 15 min data points in my xts object.

For the correlation computation I am using the following code:

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

"10" in the code above uses 10 time series values to calculate the correlation matrix therefore I will have 9 NA values at the start of my wideRawXTSscaledCorr with the correlation values returned in the 10th.

> 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

When I change the value of the sliding window to any values <10 I get repetitions of the following error:

> 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

Is this the only test I can use to see if these errors are solely a symptom of my data or could it be due to some coding error I have made? Is there some other way I can go deeper into the code to see which values are causing these errors?

TheGoat
  • 2,587
  • 3
  • 25
  • 58

1 Answers1

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

gives a similar error, check if any of the cor tests uses data that is identical across all elements of one of the vectors (i.e. the second vector)...