检索R中的最佳簇数

时间:2014-05-16 12:56:43

标签: r machine-learning statistics cluster-analysis

我有数据要根据Gap统计数据评估最佳簇数。

我在r中的gap statistic上阅读了该页面,其中给出了以下示例:

gs.pam.RU <- clusGap(ruspini, FUN = pam1, K.max = 8, B = 500)
gs.pam.RU

当我致电gs.pam.RU.Tab时,我会

Clustering Gap statistic ["clusGap"].
B=500 simulated reference sets, k = 1..8
 --> Number of clusters (method 'firstSEmax', SE.factor=1): 4
         logW   E.logW         gap     SE.sim
[1,] 7.187997 7.135307 -0.05268985 0.03729363
[2,] 6.628498 6.782815  0.15431689 0.04060489
[3,] 6.261660 6.569910  0.30825062 0.04296625
[4,] 5.692736 6.384584  0.69184777 0.04346588
[5,] 5.580999 6.238587  0.65758835 0.04245465
[6,] 5.500583 6.119701  0.61911779 0.04336084
[7,] 5.394195 6.016255  0.62205988 0.04243363
[8,] 5.320052 5.921086  0.60103416 0.04233645

我想从中检索群集的数量。但是,与能够轻松获得此数字的pamk功能相反,我无法找到使用clusGap获取此数字的方法。

然后我尝试使用maxSE函数,但我不知道参数f和SE.f代表什么或如何从数据矩阵中获取它们。

检索此最佳群集数量的任何简单方法?

1 个答案:

答案 0 :(得分:8)

答案在输出中:

...
 --> Number of clusters (method 'firstSEmax', SE.factor=1): 4
...

这是产生最大值gap的簇的数量(在表的第4行)。

maxSE(...)的论据分别是gapSE.sim

with(gs.pam.RU,maxSE(Tab[,"gap"],Tab[,"SE.sim"]))
# [1] 4

有时候绘制gap是有用的,看看群集选项有多么差异化:

plot(gs.pam.RU)
gap.range <- range(gs.pam.RU$Tab[,"gap"])
lines(rep(which.max(gs.pam.RU$Tab[,"gap"]),2),gap.range, col="blue", lty=2)