-1

假设我们有以下数据集

set.seed(144) 
dat <- matrix(rnorm(100), ncol=5)

以下函数创建所有可能的列组合并删除第一个

(combinations <- do.call(expand.grid, rep(list(c(F, T)), ncol(dat)))[-1,])
#     Var1  Var2  Var3  Var4  Var5
# 2   TRUE FALSE FALSE FALSE FALSE
# 3  FALSE  TRUE FALSE FALSE FALSE
# 4   TRUE  TRUE FALSE FALSE FALSE
# ...
# 31 FALSE  TRUE  TRUE  TRUE  TRUE
# 32  TRUE  TRUE  TRUE  TRUE  TRUE

最后一步是对每个列子集运行 k-means 聚类,这是 apply 的一个简单应用(我们希望每个 kmeans 模型中有 3 个聚类):

models <- apply(combinations, 1, function(x) kmeans(dat[,x], 3))

我的问题是如何为每个列子集运行层次聚类,而不是 kmeans。任何想法?

4

1 回答 1

1

您可以使用 hclust

models <- apply(combinations, 1, function(x) hclust(dist(dat[,x])))
clusters <- apply(combinations, 1, function(x) cutree(hclust(dist(dat[,x])), k = 3))
于 2015-05-29T09:48:34.560 回答