本文介绍了R通过多列互相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的数据表:
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
我已经尝试了很多,但未能达到我的目标,即找到每对中与 ccf 函数互相关的最大绝对值.提前非常感谢您提供的所有答案!
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!
推荐答案
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
验证结果:从 mat
提取两列: V1
和 V2
并执行绝对最大ccf.
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
这篇关于R通过多列互相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!