相关矩阵的“聚类”

时间:2018-10-18 06:26:08

标签: r

我想以某种方式“整理”或“聚类”给定数据矩阵的相关性,以使高度相关的变量彼此相邻出现。例如,如果我有这样的数据:

n <- 1000
my_corr <- matrix(c(1,   0.8, 0.8, 0,   0, 0,
                    0.8, 1,   0.8, 0,   0,   0,
                    0.8, 0.8, 1,   0,   0, 0,
                    0,   0,   0,   1,   0.9, 0.9,
                    0,   0,   0,   0.9, 1,   0.9,
                    0,   0,   0,   0.9, 0.9, 1), nrow = 6)
my_chol <- chol(my_corr)
# Edit: Rui is right, I should have used replicate
#d <- do.call(cbind, lapply(1:6, function(x) rnorm(n)))
d <- replicate(6, rnorm(n))
d <- d %*% my_chol
d <- cbind(d[, -c(2, 4)], d[, 2], d[, 4])

相关性如下:

library(GGally)
ggcorr(d)

enter image description here

但是,“想要”看起来像这样:

enter image description here

如果我不知道解决方案/如何使用预建函数,该怎么办?

1 个答案:

答案 0 :(得分:2)

corrplot具有此选项:

my_corr <- matrix(c(1,   0.8, 0.8, 0,   0, 0,
                    0.8, 1,   0.8, 0,   0,   0,
                    0.8, 0.8, 1,   0,   0, 0,
                    0,   0,   0,   1,   0.9, 0.9,
                    0,   0,   0,   0.9, 1,   0.9,
                    0,   0,   0,   0.9, 0.9, 1), nrow = 6)

corrplot::corrplot(my_corr, order = "AOE", type = "lower", method = "color")

enter image description here

Here,您几乎可以更改所有内容,使其更像ggcorr