Reputation: 71
I have a data table like this:
> head(my_data)
V1 V2 V3 V4 V5
1 36045 49933 41622 29491 34393
2 36874 44752 44158 40561 36045
3 45008 51964 58015 32733 29491
4 44830 72017 60434 40347 40561
5 48553 65470 49933 38842 32733
6 52028 64955 44752 41622 40347
I have already learned how to find correlation via multiple columns:
> head(cor(my_data)[,])
V1 V2 V3 V4 V5
V1 1.0000000 0.4621777 0.7985130 0.9490929 0.9045297
V2 0.4621777 1.0000000 0.8041824 0.4201712 0.1583757
V3 0.7985130 0.8041824 1.0000000 0.7466672 0.5889458
V4 0.9490929 0.4201712 0.7466672 1.0000000 0.8672321
V5 0.9045297 0.1583757 0.5889458 0.8672321 1.0000000
I've tried a lot, but could not reach my goal to find maximum absolute value for cross-correlation with ccf funtion among every pair. Thanks a lot in advance for all your answers!
Upvotes: 2
Views: 716
Reputation: 12703
mat
# V1 V2 V3 V4 V5
# [1,] 36045 49933 41622 29491 34393
# [2,] 36874 44752 44158 40561 36045
# [3,] 45008 51964 58015 32733 29491
# [4,] 44830 72017 60434 40347 40561
# [5,] 48553 65470 49933 38842 32733
# [6,] 52028 64955 44752 41622 40347
class(mat)
# [1] "matrix"
combins <- combn(colnames(mat), 2)
a1 <- apply(combins, 2,
FUN = function(x){ccf(mat[, x[1]], mat[, x[2]])})
abs_max_ccf <- unlist(lapply(a1, function(x) abs(max(x$acf))))
names(abs_max_ccf) <- apply(combins, 2, function(x) paste0(x[1], x[2], collapse = ''))
abs_max_ccf
# V1V2 V1V3 V1V4 V1V5 V2V3 V2V4 V2V5 V3V4 V3V5 V4V5
# 0.7460529 0.4450512 0.5167570 0.4672099 0.8028452 0.4944933 0.5220862 0.4076768 0.2884272 0.8494897
Verify Results: Extract two columns from mat
: V1
and V2
and perform absolute maximum of ccf.
abs(max(ccf(mat[, "V1"], mat[, "V2"])$acf))
# [1] 0.7460529
Upvotes: 1