如何找到最佳簇数?

时间:2014-05-17 23:07:44

标签: r cluster-analysis

我知道这个问题已被提出,但我未能为以下代码实施一个合适的情节:

options(digits=1)
set.seed(2014)

mydata <- matrix(seq(1,360),nrow=10,ncol=36)
wss <- c()
for (i in 1:19) wss[i] <- sum(kmeans(x=mydata,centers=seq(1,360,length.out=20)[i])$withinss)
plot(1:9, wss, type="b", xlab="Number of Clusters",
     ylab="Within groups sum of squares")

它产生以下错误

Error in sample.int(m, k) : 
cannot take a sample larger than the population when 'replace = FALSE'

2 个答案:

答案 0 :(得分:3)

kmeans假设每行都是您的数据是观察。因此,如果k中有x行,$clusters的结果将为k。这里您的测试数据有10行。然而,您在centers=20时指定i=2 .10个观察结果无法有20个不同的聚类。

答案 1 :(得分:0)

在黑暗中只是一点火花!

options(digits=1)
set.seed(2014)

mydata <- seq(from=1,to=365)
wss <- c()
for (i in 5:15){
wss[i-4] <- sum(kmeans(mydata,centers=floor(seq(from=1,to=365,length.out=i)[-i]))$withinss)
}
plot(1:15,wss,type="b",xlab="Number of Clusters",ylab="Within groups sum of squares")

这有意义吗? @jlhoward @jbaums