R 如何从clusGap函数中获得最佳数量的集群作为输出?
我有一个包含2个变量的数据框,我想使用R 如何从clusGap函数中获得最佳数量的集群作为输出?,r,list,output,cluster-computing,cluster-analysis,R,List,Output,Cluster Computing,Cluster Analysis,我有一个包含2个变量的数据框,我想使用clusGap函数来找到最适合使用的集群数量。此代码具有类似的结果: library(cluster) x <- as.vector(runif(100, 0, 1)) y <- as.vector(runif(100, 0, 1)) df <- data.frame(x, y) gap_stat <- clusGap(df, FUN = kmeans, nstart = n, K.ma
clusGap
函数来找到最适合使用的集群数量。此代码具有类似的结果:
library(cluster)
x <- as.vector(runif(100, 0, 1))
y <- as.vector(runif(100, 0, 1))
df <- data.frame(x, y)
gap_stat <- clusGap(df, FUN = kmeans, nstart = n,
K.max = 10, B = 50)
gap_stat
如第4行所示,集群的最佳数量为1。我希望函数有1作为输出。我需要作为环境中的对象的最佳输出数,例如
n
为1。通常,此类信息直接位于对象内部的某个位置,例如gap\u stat$nc
。寻找它str(gap\u stat)
通常就足够了
然而,在这种情况下,上述策略是不够的。但是,您可以在输出中看到感兴趣的数字,这意味着print.clusGap
(因为gap\u stat
的类是clusGap)将显示如何获取该数字。因此,检查集群:::print.clusGap
将导致
maxSE(f = gap_stat$Tab[, "gap"], SE.f = gap_stat$Tab[, "SE.sim"])
# [1] 1
这在过去可能不够透明,但实际上可以直接指定方法:
nc <- maxSE(f = gap_stat$Tab[,"gap"],
SE.f = gap_stat$Tab[,"SE.sim"],
method = "firstSEmax",
SE.factor = 1)
nc
nc <- maxSE(f = gap_stat$Tab[,"gap"],
SE.f = gap_stat$Tab[,"SE.sim"],
method = "firstSEmax",
SE.factor = 1)