[英]R Cross-correlation via multiple columns
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. 我已经尝试了很多,但是无法达到我的目标,即在每对中找到与ccf功能互相关的最大绝对值。 Thanks a lot in advance for all your answers! 非常感谢您的所有答案!
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. 验证结果:从mat
: V1
和V2
提取两列,并执行绝对最大值ccf。
abs(max(ccf(mat[, "V1"], mat[, "V2"])$acf))
# [1] 0.7460529
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.