2016-10-05 68 views
-5

我在R中运行分层聚类,我怎样才能将哪个观察属于哪个聚类?谢谢!我如何知道哪些观察属于哪个簇?

### Hierarchical Clustering 
d <- dist(EMEA_2, method = "euclidean") # distance matrix 
fit <- hclust(d, method="complete") 

### Decide bet number of clusters 
library(knitr) 
library(NbClust) 

nc<-NbClust(data = EMEA_2, distance = "euclidean", min.nc=2, max.nc=15, method = "complete", index = "db", alphaBeale = 0.1) 

groups <- cutree(fit, k=2) # cut tree into 2 clusters 

### Get group means and number of frequencies within each cluster 
a2<-aggregate(EMEA_2, list(groups),mean) 
a4<-data.frame(Cluster = a2[,1], Freq = as.vector(table(groups)), a2[,-1]) 
+0

你的代码在哪里? – mtoto

+0

@mtoto我刚刚编辑谢谢! –

+1

看'str(nc)'''''''''''''''''''''''''''''''''''''''''stats :: kmeans'然后你可以用'nc $ clusters $'或者其他名称来访问它。 –

回答

1

如果您有兴趣从NbClust优化结果,你会发现它在nc$Best.partition,每个数字都是在数据矩阵各行的簇号。

例如

> nc$Best.partition 
[1] 1 2 3 4 5 1 3 5 1 1 4 1 4 1 5 1 5 1 4 2 

为20×10的数据矩阵。

相关问题