# Variability in k-means clusters results: setting set.seed() before?

Then I fit linear models to the plot(n_clust, error) aiming to identify the best combination of I'm trying to perform a k-means cluster on my data (matrix with 2000 cases and 10 variables). I don't know how many clusters should I choose. To solve this problem, I adopted a strategy in which different values of K are setted. Error results and the number of used clusters are stored in two vectors.

Then I fitted linear models that gives me the highest sum R-squared. I used this strategy to choose de best K value. The point is that I have performed this process several times, noting that error vector varied between each run. This gave me different values of best K. An alternative to make k-means results "stable" is the use of set.seed() function prior kmean(). However I'm afraid that the result, despite fixed, have no consistency. Somebody could give me some "clues"? set.seed() will not just hind a variability? Is there another strategy for choosing the best K?

Thanks!

n_clust=NULL
error=NULL
for(i in 1:200){
cl <- kmeans(scores, i, iter.max=100)
erro <- c(error,cl\$tot.withinss)
n_clust <- c(n_clust,i)
}

r2=NULL
for(i in 3:197){
a <- lm(error[1:i] ~ n_clust[1:i])
b <- lm(error[i+1:200] ~ n_clust[i+1:200])
rsqd <- as.numeric(summary.lm(a)[8]) + as.numeric(summary.lm(b)[8])
r2 <- c(r2, rsqd)}

id_n <- 3 + which(r2==max(r2))

-

## migrated from stackoverflow.comApr 12 '12 at 16:23

This question came from our site for professional and enthusiast programmers.

You say that set.seed doesn't work for you, but your example doesn't use set.seed so it's hard to know if you use it correctly!

This is an example close to yours that seem to work:

scores <- matrix(runif(1000), 100, 10)

set.seed(42)
k1 = kmeans(scores, 5, iter.max=500)

set.seed(42)
k2 = kmeans(scores, 5, iter.max=500)

identical(k1, k2) # TRUE


...note that you need to call set.seed with the same seed before calling kmeans, and you have to give the same parameters to kmeans if you want to expect the same answer.

When you specify an integer for the centers parameter, kmeans uses random numbers to come up with the centers. If you instead specify the centers your self, it should be reproducible. Here I choose the 5 first rows as centers (probably a bad idea though):

k1 = kmeans(scores, scores[1:5,], iter.max=500)
k2 = kmeans(scores, scores[1:5,], iter.max=500)
identical(k1, k2) # TRUE

-