给定一个相关矩阵,如何让 R 根据变量对于任意阈值是否相关(连通)返回一个分区?
例如,凝视:
corr(x)
并找到由以下定义的分区:
corr(x)>0.5
返回分组为字符列表的行/列名称。
list(c("a","b"),"d")
表示“a”、“b”在一组,但“d”不在一组。
这个问题目前还不是很详细,所以我不确定这是否能解决您的问题,但是我的建议是生成高于特定阈值的所有相关矩阵对的列表:
# example dataset
a <- mvtnorm::rmvnorm(10,rep(0,10)) |>
as.data.frame() |>
setNames(LETTERS[1:10])
# correlation matrix
ca <- cor(a)
# optional: remove upper.tri and diag to avoid duplicates
ca[upper.tri(ca,T)] <- NA
# matrix of name pairs corresponding to each cell of ca
cm <- do.call(expand.grid, dimnames(ca)) |>
apply(1,c,simplify = F) |>
lapply(unname) |>
matrix(ncol(ca))
# use the correlation matrix to filter the matrix of name pairs
# may remove abs() to obtain only positive correlations
# adjust threshold, currently 0.6
as.list(cm[!is.na(ca)&abs(ca)>.6])