Example of K-Means:
x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
matrix(rnorm(100, mean=1, sd=0.3), ncol = 2))
results <- matrix(nrow = 14, ncol = 2, dimnames = list(2:15, c("clusters", "sumsquares")))
for (i in 2:15) {
fit <- kmeans(x, i)
results[i-1, 1] <- i
results[i-1, 2] <- fit$tot.withinss
}
results
## clusters sumsquares
## 2 2 18.111334
## 3 3 14.192556
## 4 4 10.123800
## 5 5 8.518024
## 6 6 7.857345
## 7 7 7.430910
## 8 8 4.312971
## 9 9 4.476039
## 10 10 3.661820
## 11 11 4.445108
## 12 12 3.144363
## 13 13 2.802024
## 14 14 2.702368
## 15 15 2.512544
plot(results)

fit <- kmeans(x, 2)
plot(x, col = fit$cluster)

A little more exciting example with higher dimensions:
x <- rbind(matrix(rnorm(100, sd = 10), ncol = 2),
matrix(rnorm(100, mean=5, sd=10), ncol = 2),
matrix(rnorm(100, mean=7, sd=10), ncol = 2),
matrix(rnorm(100, mean=15, sd=20), ncol = 2))
results <- matrix(nrow = 29, ncol = 2, dimnames = list(2:30, c("clusters", "sumsquares")))
for (i in 2:30) {
fit <- kmeans(x, i)
results[i-1, 1] <- i
results[i-1, 2] <- fit$tot.withinss
}
results
## clusters sumsquares
## 2 2 57968.484
## 3 3 41802.119
## 4 4 32961.268
## 5 5 24960.953
## 6 6 19459.715
## 7 7 17524.023
## 8 8 15279.958
## 9 9 13441.918
## 10 10 12840.142
## 11 11 11496.863
## 12 12 10124.641
## 13 13 9872.630
## 14 14 8502.228
## 15 15 8664.287
## 16 16 8039.984
## 17 17 7852.806
## 18 18 7288.301
## 19 19 6897.196
## 20 20 5757.583
## 21 21 5628.360
## 22 22 5400.570
## 23 23 5299.634
## 24 24 4560.394
## 25 25 4577.656
## 26 26 4185.661
## 27 27 4699.668
## 28 28 4135.318
## 29 29 4978.062
## 30 30 4300.097
plot(results)

fit <- kmeans(x, 4)
plot(x, col = fit$cluster)
