I am using the kmeans() function in R and I was curious what is the difference between the totss and tot.withinss attributes of the returned object. From the documentation they seem to be returning the same thing, but applied on my dataset the value of totss is 66213.63 and for tot.withinss is 6893.50.
Please let me know if you are familiar with mroe details.
Thank you!
Marius.
Given the between sum of squares
betweenssand the vector of within sum of squares for each clusterwithinssthe formulas are these:For example, if there were only one cluster then
betweensswould be0, there would be only one component inwithinssandtotss = tot.withinss = withinss.For further clarification, we can compute these various quantities ourselves given the cluster assignments and that may help clarify their meanings. Consider the data
xand the cluster assignmentscl$clusterfrom the example inhelp(kmeans). Define the sum of squares function as follows — this subtracts the mean of each column of x from that column and then sums of the squares of each element of the remaining matrix:Then we have the following. Note that
cl$centers[cl$cluster, ]are the fitted values, i.e. it iis a matrix with one row per point such that the ith row is the center of the cluster that the ith point belongs to.EDIT:
Since this question was answered, R has added additional similar
kmeansexamples (example(kmeans)) and a newfitted.kmeansmethod and we now show how the fitted method fits into the above in the comments trailing the code lines.