我想以高度相关的变量彼此相邻的方式“整理”或“聚类”给定数据矩阵的相关性。例如,如果我有这样的数据:
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)
但是,“想要”看起来像这样:
如果我不知道解决方案/是否可以使用预构建的函数,我该怎么做?
最佳答案
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")
Here ,您几乎可以更改所有内容并使其更像 ggcorr
。
关于r - 相关矩阵"Clustering",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52868124/