
# There is an issue with kmeans: different itermax give the same results below

load('/home/nevensf/gitrep/s2dverification/Arctic_regions.sav')

var <- bubi
#itermax <- 1
#itermax <- 2
#itermax <- 3
#itermax <- 10
itermax <- 20

kr1 <- kmeans( var , centers = 3, iter.max = itermax, nstart = 20 )
kr2 <- kmeans( var , centers = kr1$centers, iter.max = itermax )
kr3 <- kmeans( var , centers = kr2$centers, iter.max = itermax )
kr4 <- kmeans( var , centers = kr3$centers, iter.max = itermax )
kr5 <- kmeans( var , centers = kr4$centers, iter.max = itermax )
kr6 <- kmeans( var , centers = kr5$centers, iter.max = itermax )
kr7 <- kmeans( var , centers = kr6$centers, iter.max = itermax )
kr8 <- kmeans( var , centers = kr7$centers, iter.max = itermax )
kr9 <- kmeans( var , centers = kr8$centers, iter.max = itermax )

print(kr1$centers)
print(kr1$centers == kr2$centers)
print(kr1$centers == kr9$centers)

print(kr1$cluster)
print(kr2$cluster)
print(kr9$cluster)

# Also, NbClust is giving me strange problem that should not be a problem
# I installed it only on saga at the moment (will catch Oriol on Friday to make it available to everyone)
library('NbClust')

nbclust.results <- NbClust(var, min.nc = 2, max.nc = 30, method = "kmeans")
table(nbclust.results$Best.n[1,])

#the winner is the cluster (top row) that has the highest number in the bottom row
 
