Skip to contents

Compute Within-Cluster Sum of Squares

Usage

compute_wss(object, newdata = NULL)

Arguments

object

of 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]  4665.041 10247.471  8808.032 11474.702  7654.146
# or with new data
compute_wss(km, mtcars)
#> [1]  4665.041 10247.471  8808.032 11474.702  7654.146