Search code examples
rcorrelationcross-correlation

cor() heatmap that also takes into account lag?


I have this data frame:

> head(calcium)
     Cell 1    Cell 2     Cell 3    Cell 4    Cell 5    Cell 6    Cell 7    Cell 8    Cell 9   Cell 10
1 0.4672073 0.5844194 0.53550783 0.5364516 0.3470978 0.2802241 0.3965193 0.4651296 0.4871049 0.5685573
2 0.6724369 0.5549080 0.46405108 0.5231966 0.3634458 0.4480713 0.4991682 0.5315563 0.5486042 0.5294624
3 0.4596055 0.4670149 0.49120023 0.4395472 0.2961227 0.2931165 0.3343016 0.3347550 0.5988327 0.3738076
4 0.5033532 0.6265054 0.38094926 0.5931815 0.4030199 0.3461845 0.4966594 0.5039769 0.6795089 0.4875191
5 0.2544972 0.3734030 0.06739901 0.2006546 0.1038821 0.2315288 0.1704387 0.1001154 0.3799489 0.3224333
    Cell 11   Cell 12   Cell 13   Cell 14   Cell 15    Cell 16   Cell 17   Cell 18   Cell 19   Cell 20
1 0.6870339 0.6695488 0.6130446 0.5356461 0.6609977 0.58372848 0.5422807 0.5344803 0.6320900 0.6294263
2 0.5734583 0.6547612 0.5392496 0.5330794 0.5704163 0.29659694 0.5567181 0.5780661 0.4010656 0.5863210
3 0.3404278 0.4653025 0.4841558 0.4371394 0.4779519 0.37612878 0.5571457 0.4525741 0.3330830 0.6095697
4 0.5570465 0.5552679 0.5799134 0.5745080 0.5390752 0.44927465 0.5118969 0.5618096 0.4442722 0.5840596
5 0.2730119 0.2671422 0.1901299 0.2949408 0.2715825 0.03302615 0.2712914 0.2962793 0.2137080 0.3243795

> tail(calcium)
        Cell 1    Cell 2    Cell 3    Cell 4    Cell 5    Cell 6    Cell 7    Cell 8    Cell 9   Cell 10
1495 0.4740832 0.5869490 0.5582463 0.6920769 0.5148404 0.4017368 0.6079173 0.7788185 0.8114776 0.7092727
1496 0.6341606 0.5733480 0.4622866 0.6170735 0.3884858 0.3778201 0.5342382 0.6239193 0.5823868 0.6487626
1497 0.3662752 0.4143892 0.2904592 0.4388380 0.2732026 0.4264538 0.4004278 0.4336887 0.3919094 0.4598705
1498 0.4719526 0.5387774 0.4595078 0.4988954 0.4979742 0.3102995 0.5260781 0.5331988 0.6804864 0.6189913
1499 0.4194409 0.5404455 0.4455472 0.5634257 0.4054463 0.4645974 0.4627777 0.5951874 0.4545299 0.6653680
       Cell 11   Cell 12   Cell 13   Cell 14   Cell 15   Cell 16   Cell 17   Cell 18   Cell 19   Cell 20
1495 0.6556259 0.7681060 0.7380664 0.7464327 0.5121680 0.6313292 0.6928669 0.7497219 0.5277792 0.7770823
1496 0.4813021 0.6268656 0.5539104 0.5806629 0.3948412 0.3627633 0.5811911 0.6131842 0.3701380 0.6591560
1497 0.3969300 0.5247286 0.4840403 0.4693218 0.4129616 0.3213437 0.5072689 0.5465302 0.2863405 0.4979315
1498 0.4925699 0.5820069 0.6174026 0.5797521 0.4415087 0.3831081 0.6320489 0.6061551 0.5626544 0.7134859
1499 0.4026728 0.5014044 0.5536220 0.5139246 0.4354147 0.3987318 0.5499019 0.5411839 0.4232987 0.5824049

and I would like to get a cross-correlation matrix, like the one I get with:

library(gplots)
Colors=c("blue","yellow","red")
Colors=colorRampPalette(Colors)(100)
heatmap.2(as.matrix(cor(calcium)), dendrogram = "none",
          col = Colors, trace = "none", density.info =  "none")

but this time, I'd like it to consider a lag of 1. How can I do that?


Solution

  • Make up example data:

    nc <- 20
    calcium <- matrix(rnorm(nc^2), nc, nc)
    

    Loop over the entire matrix (CCF(i,j,1) is not identical to CCF(j,i,1), so we need to compute the whole matrix, not just one half), computing correlation matrix:

    cmat <- matrix(NA,nc,nc)
    for (i in 1:nc) {
       for (j in 1:nc) {
          cmat[i,j] <- cor(calcium[-1,i], calcium[-nrow(calcium),j])
          ## or: ccf(calcium[,i],calcium[,j],lag.max=1)$acf[3,1,1]
       }
    }
    

    Then use cmat in place of cor(calcium) in your plotting code.


    There are (at least) two ways to compute CCF(i,j,1).

    • cor(x[-1,i], x[-nrow(x),j]) sets up the lagged version of the columns manually (by excluding the first element for one column and the last element for the other)
    • ccf(x[,i],x[,j],lag.max=1)$acf[3,1,1] uses the built-in ccf() function. The results are returned in the $acf element of a list, which is a (lag)x1x1 array, where the first dimension holds the range of lags. When lag.max=1 the cross-correlations are computed for lags -1, 0, 1, so the third element is the +1 lag result.