R: why different between [k-means] build-in function and Kmeans from amap package I am doing k-means algorithm on iris data using two functions, the regular "kmeans" and "Kmeans" from amap package.
irisCluster <- kmeans(iris[, 3:4], 3, nstart = 20,algorithm = "Lloyd");
irisCluster$withinss
#[1] 13.05769 16.29167  2.02200

irisCluster2<-Kmeans(iris[,3:4],3,nstart = 20,method="euclidean");
irisCluster2$withinss
#[1] 0.12010750 0.00113741 0.00596000

According to Rhelp, Kmeans when method="euclidean" should return same result as with function kmeans. But clearly from above, they look very different. Why is the case?
 A: The amap R package is broken in many ways.


*

*K-means does not use Euclidean distance, but squared deviations. There is no reason to compute the sqrt.

*With other distances, such as absolute Pearson correlation, available in amap, k-means may fail to convergeand will not find even "locally" optimal solutions. You can't just put other distances into k-means, but you also need to change the mean. The means is optimal for squared errors, it is not optimal for other distances. But if you -systematically- never make a good choice for the current cluster center, then the final cluster center will not be good either. For example, PAM ("k-medoids") does something similar for other distances, but fixes the problem of using the mean. Apparently, the amap authors were not aware of this limitation of k-means...

*Their WSS computation is obviously defect. Instead of the sum of squared deviations, you get the last squared distance only. The comment still contains a different code that didn't have this bug.

*The default kmeans algorithm (Hartigan Wong) is much faster than Lloyds algorithm, and may find better results.

