Legend
Legend

Reputation: 116870

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

Upvotes: 5

Views: 10607

Answers (3)

Legend
Legend

Reputation: 116870

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.

Upvotes: 5

G. Grothendieck
G. Grothendieck

Reputation: 269694

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)

Upvotes: 2

joran
joran

Reputation: 173577

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.

Upvotes: 2

Related Questions