In one dimension, the (empirical) autocorrelation coefficient at lag $k$ (for $k$ between $0$ and $n-1$) of a series of numbers $\mathbf t = t_1, t_2, \ldots, t_n$ is the correlation coefficient of the series shifted by $k$ places:
$$\rho_k(\mathbf t) = \operatorname{Cor}((t_1,t_2,\ldots, t_{n-k}),\ (t_{k+1}, t_{k+2}, \ldots, t_n)).$$
When $\mathbf t$ is a series of $d$-vectors (with $d=1$ corresponding to the usual series of numbers)
$$\mathbf t = (t_{1,1},\ldots, t_{1,d}),\ (t_{2,1},\ldots, t_{2,d}),\ \ldots,\ (t_{n,1},\ldots, t_{n,d}),$$
the concept and the definition remain unchanged. All we need is a generalization to the (empirical) correlation of two sequences of vectors. This correlation is a $d\times d$ matrix $P_k(\mathbf t)$ containing all possible correlations at lag $k:$
$$P_k(\mathbf t)_{i,j} = \operatorname{Cor}(t_{1,i}, t_{2,i}, \ldots, t_{n-k,i}),\ (t_{k+1,j}, t_{k+2,j},\ldots, t_{n, j})).$$
You can readily see this applies without modification to the cross-correlation between any two (equal-length) series of vectors, even when the (common) dimension of the vectors in one series differs from that of the other series.
Alternative autocorrelation formulas for ordinary time series can be generalized in the same way.
Here is R
code to illustrate. It uses the built-in cor
function in R
, which implements several forms of correlation (Pearson, Kendall, and Spearman, with Pearson the default). The ...
argument can be used to specify the type of correlation and even how to handle any missing values.
P <- function(lag, t, s, ...) {
if(missing(s)) s <- t
if(!is.matrix(t)) t <- matrix(t, ncol = 1)
if(!is.matrix(s)) s <- matrix(s, ncol = 1)
n <- nrow(t)
if (isTRUE(lag < 0) || isTRUE(lag >= n)) return(matrix(NA, nrow(t), ncol(s)))
cor(t[seq_len(n - lag), ], s[lag + seq_len(n - lag), ], ...)
}
This code converts its input series into matrices (even for series of scalars, where $d=1$ columns are used); arranges to compute the autocorrelation when only a single series is supplied but otherwise will compute the cross-correlation of two series; checks that the lag is legitimate (returning a matrix of missing values otherwise); and then does all the work on the last line by invoking cor
.
As an example, after scanning the data in the question into a vector X
, we might compute (say) the lag-2 Pearson correlation with the expression
P(2, X)
with the result
[,1] [,2] [,3] [,4] [,5]
[1,] 0.000 -0.411 -0.201 0.000 -0.273
[2,] -0.483 -0.290 -0.288 -0.483 -0.281
[3,] -0.397 -0.377 -0.321 -0.397 -0.334
[4,] 0.000 -0.411 -0.201 0.000 -0.273
[5,] -0.397 -0.377 -0.321 -0.397 -0.334
If you only want part of this -- say, the lag-2 cross-correlation between the first three columns of data and the last two columns -- the same code works without modification (demonstrating my claim above):
P(2, X[, 1:3], X[, 4:5])
[,1] [,2]
[1,] 0.000 -0.273
[2,] -0.483 -0.281
[3,] -0.397 -0.334
This is the same as the upper right $3\times 2$ block in the first matrix of results.