title: Running window of correlation coefficients for time series in R
body: <p>I wish to calculate the correlation coefficients of 12 variables using a running window in R.</p>
  
  <p>My data is stored in a zoo object with a %m.%d.%Y %H:%M:%S index, each of the 12 variables have 1343 observations. I don't know what window size I'm going to use but I can change it if needs. </p>
  
  <p>@Joshua Ulrich has posted <a href="https://stackoverflow.com/questions/20690809/calculate-rolling-correlation-using-rollapply">here</a> how to calculate the rolling correlation using rollapplyr but this example has only two variables. With my limited R experience I am unsure as to how I can incorporate one of the apply family functions to run the correlations across all 12 variables.</p>
  
  <p>Any pointers would be greatly appreciated</p>
  
  <p>My data looks as follows: </p>
  
  <pre><code>&gt; head(wideRawXTS)
                      DO0182U09A3 DO0182U09B3 DO0182U09C3 DO0182U21A1 DO0182U21A2 DO0182U21A3 DO0182U21B1 DO0182U21B2 DO0182U21B3
  2017-01-20 16:30:00     -101.50     -103.37     -103.86     -104.78     -104.95     -105.33     -102.50      -99.43     -104.05
  2017-01-20 16:45:00     -101.32     -102.75     -104.22     -104.51     -103.94     -105.29     -102.82     -101.99     -103.94
  2017-01-20 17:00:00     -101.45     -103.30     -103.93     -104.70     -104.82     -105.13     -103.72     -103.95     -104.25
  2017-01-20 17:15:00     -100.91      -95.92      -99.22     -103.83     -104.72     -105.19     -103.57     -101.36     -104.09
  2017-01-20 17:30:00     -100.91     -103.04     -104.09     -102.15     -104.91     -105.18     -103.88     -104.09     -103.96
  2017-01-20 17:45:00     -100.97     -103.67     -104.12     -105.07     -104.23      -97.48     -103.92     -103.89     -104.01
                      DO0182U21C1 DO0182U21C2 DO0182U21C3
  2017-01-20 16:30:00     -104.51     -104.42     -105.17
  2017-01-20 16:45:00     -104.74     -104.65     -105.25
  2017-01-20 17:00:00     -105.02     -105.04     -105.32
  2017-01-20 17:15:00     -103.90     -102.95     -105.16
  2017-01-20 17:30:00     -104.75     -105.07     -105.23
  2017-01-20 17:45:00     -105.08     -105.14     -104.89
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/kLGiV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kLGiV.png" alt="Cor Output of wideRawDFscaled"></a></p>
  
  <p><a href="https://i.stack.imgur.com/CKCFu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CKCFu.png" alt="enter image description here"></a></p>
  
  <pre><code>#Importing Data
  rawDF &lt;- read.csv("RTWP_DO0182_14Day_Raw.csv",
                    header = F,
                    col.names = c("Period Start Time",
                                  "PLMNname",
                                  "RNCname",
                                  "WBTSname",
                                  "WBTSID",
                                  "WCELname",
                                  "WCELID",
                                  "Overload",
                                  "AverageRTWP",
                                  "DC_RTWP_High_PRX",
                                  "RTWP_Threshold"),
                    colClasses = c("character",
                                   "NULL", #drops PLMN name
                                   "NULL", #drops RNC name
                                   "character", 
                                   "integer",
                                   "character",
                                   "integer",
                                   "NULL", #drops Overload
                                   "numeric",
                                   "NULL", #drops DC_RTWP_High_PRX
                                   "NULL"), #drops RTWP_Threshold
                    strip.white=TRUE,
                    na.strings = c("NA", 
                                   "NULL"),
                    as.is = T,
                    skip=2)
  
  #convert period.start.time to POSIXlt
  rawDF$Period.Start.Time &lt;- as.POSIXlt(strptime(rawDF$Period.Start.Time, 
                                                 format="%m.%d.%Y %H:%M:%S"))
  
  #dcast the long data frame to a wide data frame
  wideRawDF &lt;- dcast(rawDF, 
                     Period.Start.Time ~ WCELname,
                     value.var = "AverageRTWP")
  
  #assign the date times as rownames for converting to XTS
  rownames(wideRawDF) = wideRawDF[[1]]
  
  #drops the duplicate Period Start Time column since date times are rownames
  wideRawDF$Period.Start.Time &lt;- NULL
  
  #Convert wideRawDF to XTS time series
  wideRawDF &lt;- as.xts(wideRawDF)
  
  #NA values replaced by interpolated values
  wideRawDF &lt;- na.approx(wideRawDF, na.rm = FALSE)
  
  #DF is centered by subtracting the column means and scaled by dividing the
  #centered columns by their standard deviations
  wideRawDFscaled &lt;- scale(wideRawDF, center = TRUE, scale = TRUE)
  
  #window &lt;- 20
  #cors &lt;- combn(names(wideRawDFscaled),2,
  #              function(p) rollapply(wideRawDFscaled, window ,
  #                                    function(x) cor(x[,p[1]],x[,p[2]]),
  #                                    by.column = FALSE))
  #colnames(cors) &lt;- combn(names(wideRawDFscaled),2,paste,collapse=".")
  
  #Running window of correlation coefficients
  Cor &lt;- function(x) {
    corr &lt;- cor(wideRawDFscaled)
    out &lt;- as.data.frame.table(corr)[lower.tri(corr), ]
    with(out, setNames(Freq, paste(Var1, Var2)))
  }
  slidingCor &lt;- rollapplyr(wideRawDFscaled, 6, Cor, by.column = FALSE)
  </code></pre>