我使用hclust中的欧氏距离和Ward.D2方法在R中进行了聚类分析。我想使用这些聚类中心作为 K 均值分析的起点。但我不知道如何提取沃德方法的中心?有人可以帮忙吗?
这是一个使用 iris 数据集的可重现示例:
data(iris)
iris.z <- scale(iris[, -5]) # Standardize the variables
iris.hcl <- hclust(dist(iris.z), method="ward.D2") # Compute clusters
iris.hcl3 <- cutree(iris.hcl, 3) # Cut at 3 clusters
centers <- aggregate(iris.z, by=list(iris.hcl3), mean) # Compute centroids
iris.km3 <- kmeans(iris.z, centers[, -1]) # Compute kmeans
addmargins(xtabs(~iris.hcl3+iris.km3$cluster)) # Compare results
# iris.km3$cluster
# iris.hcl3 1 2 3 Sum
# 1 49 0 0 49
# 2 1 29 0 30
# 3 0 24 47 71
# Sum 50 53 47 150
请注意,hcl3 中的 1 行从 km3 中的集群 2 移动到集群 1,hcl3 中的 24 行从 km3 中的集群 3 移动到集群 2。
使用 EasyStats 参数包中的
method = "kmeans"
函数中的
cluster_analysis
选项也可以轻松实现这一点。
例如:
# Hierarchical k-means (more robust k-means)
if (require("factoextra", quietly = TRUE)) {
rez <- cluster_analysis(iris[1:4], n = 3, method = "hkmeans")
rez # Show results
predict(rez) # Get clusters
}