How do I plot a cross correlation matrix for timeseries?

10,176

Solution 1

There seems to be another trivial way of doing it!

timeseries = read.table("./test", header=F)
acf(timeseries)

gives me a matrix of correlation plots. Of course, there are other options that can be passed to acf if a covariance is needed.

Solution 2

A trivial way of doing this is to simply create a matrix of plots on your plotting device and place each ccf plot in one by one:

M <- matrix(sample(0:1,40,replace = TRUE),nrow = 10)

par(mfrow= c(4,4))
for (i in 1:4){
    for (j in 1:4){
        ccf(M[,i],M[,j])
    }
}

But if you wait around a bit, someone who knows the time series packages more intimately may swing by with a function that does this a bit more nicely.

Solution 3

Try this where M is as in joran's post:

pnl <- function(x, y = x) { par(new = TRUE); ccf(x, y) }
pairs(as.data.frame(M), upper.panel = pnl, diag.panel = pnl, cex.labels = 1)
Share:
10,176
Legend
Author by

Legend

Just a simple guy :)

Updated on June 11, 2022

Comments

  • Legend
    Legend almost 2 years

    I have a timeseries representation of my data as follows (without the row and column) annotations:

          L1 L2 L3 L4
    t=1    0  1  1  0
    t=2    0  1  1  1
    t=3    1  0  1  1
    t=4    0  1  1  0
    

    I am reading this into R as:

    timeseries = read.table("./test", header=F)
    

    I am plotting timeseries for L1 using

    ts.plot(timeseries$V1)
    

    and plotting the cross-correlation function as:

    ccf(timeseries$V1, timeseries$V2)
    

    Now, can someone please tell me how do I plot a cross correlation matrix that shows the output of this function for L1-L4? Basically, something like this (in my case, a 4x4 matrix of plots):

    enter image description here

  • Legend
    Legend almost 13 years
    +1 Great! Thank you. I'll probably wait for half-a-day. If nothing else comes by, I'll accept this. Thank you.