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))