创建相关矩阵

Create Correlation Matrix

我想使用能量包中的距离相关矩阵来创建所有成对相关,就像 cor 会做的那样:

cor(iris[,1:4])

             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length    1.0000000  -0.1175698    0.8717538   0.8179411
Sepal.Width    -0.1175698   1.0000000   -0.4284401  -0.3661259
Petal.Length    0.8717538  -0.4284401    1.0000000   0.9628654
Petal.Width     0.8179411  -0.3661259    0.9628654   1.0000000

如何对 energy::dcor() 执行相同的操作?

> energy::dcor(iris[,1], iris[,2], 1.0)
[1] 0.3105326
> energy::dcor(iris[,1], iris[,3], 1.0)
[1] 0.8585197
> energy::dcor(iris[,1], iris[,4], 1.0)
[1] 0.8266021
> ##etc...
m <- sapply(1:4, function(r) {
    sapply(1:4, function(c) {
        energy::dcor(iris[,r], iris[,c])
    })
})
colnames(m) <- rownames(m) <- colnames(iris)[1:4]
m