【问题标题】:Error in NbClust: not enough objects to clusterNbClust 中的错误:没有足够的对象进行聚类
【发布时间】:2017-06-28 10:15:24
【问题描述】:

我正在尝试使用 R 中的 NbClust 方法来确定聚类分析中的最佳聚类数量,遵循Manning 书中的方法。 但是,我收到一条错误消息:

hclust 中的错误(md, method = "average"): must have n >= 2 objects to 集群。

即使 hclust 方法似乎有效。因此,我认为问题是(错误消息也说明了这一点),NbClust 试图创建内部只有一个对象的组。

这是我的代码:

mydata = read.table("PLR_2016_WM_55_5_Familienstand_aufbereitet.csv", skip = 0, sep = ";", header = TRUE)

mydata <- mydata[-1] # Without first line (int)
data.transformed <- t(mydata) # Transformation of matrix
data.scale <- scale(data.transformed) # Scaling of table
data.dist <- dist(data.scale) # Calculates distances between points

fit.average <- hclust(data.dist, method = "average")
plot(fit.average, hang = -1, cex = .8, main = "Average Linkage Clustering")

library(NbClust)
nc <- NbClust(data.scale, distance="euclidean", 
          min.nc=2, max.nc=15, method="average") 

我发现了类似的问题here,但我无法调整代码。

【问题讨论】:

    标签: r grouping cluster-analysis hclust


    【解决方案1】:

    您的数据集中存在一些问题。
    最后 4 行不包含数据,必须删除。

    mydata <- read.table("PLR_2016_WM_55_5_Familienstand_aufbereitet.csv", skip = 0, sep = ";", header = TRUE)
    mydata <- mydata[1:(nrow(mydata)-4),]
    mydata[,1] <- as.numeric(mydata[,1])
    

    现在重新调整数据集:

    data.transformed <- t(mydata) # Transformation of matrix
    data.scale <- scale(data.transformed) # Scaling of table
    

    由于某种原因,data.scale 不是满秩矩阵:

    dim(data.scale)
    # [1]  72 447
    qr(data.scale)$rank
    # [1] 71
    

    因此,我们从data.scale 中删除一行并将其转置:

    data.scale <- t(data.scale[-72,])
    

    现在数据集已准备好用于NbClust

    library(NbClust)
    nc <- NbClust(data=data.scale, distance="euclidean", 
              min.nc=2, max.nc=15, method="average") 
    

    输出是

    [1] "Frey index : No clustering structure in this data set"
    *** : The Hubert index is a graphical method of determining the number of clusters.
                    In the plot of Hubert index, we seek a significant knee that corresponds to a 
                    significant increase of the value of the measure i.e the significant peak in Hubert
                    index second differences plot. 
    
    *** : The D index is a graphical method of determining the number of clusters. 
                    In the plot of D index, we seek a significant knee (the significant peak in Dindex
                    second differences plot) that corresponds to a significant increase of the value of
                    the measure. 
    
    ******************************************************************* 
    * Among all indices:                                                
    * 8 proposed 2 as the best number of clusters 
    * 4 proposed 3 as the best number of clusters 
    * 8 proposed 4 as the best number of clusters 
    * 1 proposed 5 as the best number of clusters 
    * 1 proposed 8 as the best number of clusters 
    * 1 proposed 11 as the best number of clusters 
    
                       ***** Conclusion *****                            
    
    * According to the majority rule, the best number of clusters is  2 
    
    ******************************************************************* 
    

    【讨论】:

    • 谢谢。您的回答很有帮助。
    猜你喜欢
    • 2018-05-16
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 2021-05-17
    相关资源
    最近更新 更多