Compute Within-Cluster Sum of Squares
Arguments
- object
(
any)
Class inheriting from"kmeans".- newdata
(
matrix())
New data to predict on.
Details
WSS is defined as $$ \sum_{i=1}^{n} \left\|x_{i} - \mu_{j(i)}\right\|^2 $$, where \(x_{i}\) is a data point and \(\mu_{j(i)}\) is the centroid of the cluster to which \(x_{i}\) is assigned. When new data is provided, the function predicts the nearest cluster for each new observation and computes the WSS for these points based on their predicted clusters.
Examples
km <- kmeans(mtcars, 5)
compute_wss(km)
#> [1] 208.0365 6355.5813 46659.3172 3616.8297 7256.4492
# or with new data
compute_wss(km, mtcars)
#> [1] 208.0365 6355.5813 46659.3172 3616.8297 7256.4492