# New R code for ‘moving’ or ‘running’ correlations

March 4, 2011
By

(This article was first published on ikan bilis » R, and kindly contributed to R-bloggers)

Someone at the lab asked how to ‘do something like running means, but with correlations’. I couldn’t find any existing code that would make a good example, so I just wrote some myself.

It would be nice to do this without looping. If anyone has a clever way to do this, please do let me know.

# 2011-03-04
# v0.01

MovingCor <- function(x, y, window.size=21, method="pearson") {
# Computes moving correlations between two vectors with symmetrical windows.
#
# Args:
#   x: One of the two vectors whose correlation is to be calculated.
#   y: The other vector. Note that it must be of the same length as x.
#   window.size: The size of windows to be used for each calculated
#                correlation. Note that if even numbers are chosen, the
#                window will not be skewed as there will be one extra value
#                on the upper-side of the window. Default size is 21.
#   method: The method of correlation. May be: "pearson", "kendall", or
#           "spearman". Default is "pearson".
#
# Returns:
#   A vector of the moving correlations.
n <- length(x)
# Setup a few catches for error handling.
if (TRUE %in% is.na(y) || TRUE %in% is.na(x)) {
stop("Arguments x and y cannot have missing values.")
}
if (n &lt;= 1 || n != length(y)) {
stop("Arguments x and y have different lengths: ",
length(x), " and ", length(y), ".")
}
out <- rep(NA, round(window.size/2))  # Stuffing the returned vector.
for (value in seq(from = 1, to = n - (window.size - 1))) {
value.end &lt;- value + (window.size - 1)
out <- append(out, cor(x[value:value.end],
y[value:value.end],
method = method))
}
out <- append(out, rep(NA, n - length(out)))  # Finish stuffing.
return(out)
}


EDIT:
There are more nimble functions out there for this, and other window-related tasks. See the caTools‘s runmean function. The package zoo also has a number of quick functions including rollmean and the more general rollapply.

Tagged: code, R, stats