Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I plot a cross correlation matrix for timeseries?

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

like image 589
Legend Avatar asked Aug 05 '11 22:08

Legend


3 Answers

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.

like image 178
Legend Avatar answered Oct 11 '22 23:10

Legend


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.

like image 23
joran Avatar answered Oct 11 '22 23:10

joran


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)
like image 39
G. Grothendieck Avatar answered Oct 11 '22 22:10

G. Grothendieck