繁体   English   中英

是否有任何函数可以计算 R 中数组中包含的一组矩阵之间的相关性?

[英]Is there any function that calculate correlation between a set of matrices included in an array in R?

我有一个包含 20 个矩阵的列表。 我想计算所有矩阵之间的皮尔逊相关性。 但我找不到任何可能的代码或功能? 你能否提供一些这样做的提示。

something like:
a=matrix(1:8100, ncol = 90)
b=matrix(8100:16199, ncol = 90)
c=matrix(sample(16200:24299),ncol = 90)
z=list(a,b,c)

我发现这个: https : //rdrr.io/cran/lineup/man/corbetw2mat.html并尝试一下:

library(lineup)
corbetw2mat(z[a], z[b], what = "all")

我有以下错误:

Error in corbetw2mat(z[a], z[b], what = "all") : 
  (list) object cannot be coerced to type 'double'

我想要一个这样的列表作为结果:

a & b 
correlations
a & c
correlations
b & c
correlations

谢谢

我将创建一个较小的数据集来说明下面的解决方案。
要获得成对组合,最好的选择是使用combn计算组合矩阵,然后循环遍历它,在这种情况下为lapply循环。

set.seed(1234)    # Make the results reproducible

a <- matrix(1:9, ncol = 3)
b <- matrix(rnorm(9), ncol = 3)
c <- matrix(sample(1:9), ncol = 3)
sample_list <- list(a, b, c)

cmb <- combn(3, 2)
res <- lapply(seq.int(ncol(cmb)), function(i) {
  cor(sample_list[[ cmb[1, i] ]], sample_list[[ cmb[2, i] ]])
})

结果在列表res

请注意, sample是一个基本的 r 函数,因此我将名称更改为sample_list

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM