compute_wss: Compute Within-Cluster Sum of Squares

View source: R/gmeans.R

compute_wssR Documentation

Compute Within-Cluster Sum of Squares

Description

Compute Within-Cluster Sum of Squares

Usage

compute_wss(object, newdata = NULL)

Arguments

object

(any)
Class inheriting from "kmeans".

newdata

(matrix())
New data to predict on, a numeric matrix or a data frame. Columns are matched to the centers by name and unused columns are ignored.

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.

Value

A numeric() vector with one within-cluster sum of squares per cluster, in the order of the rows of object$centers. Clusters with no assigned points contribute 0.

Examples

km <- kmeans(mtcars, 5)
compute_wss(km)
# or with new data
compute_wss(km, mtcars)

gmeans documentation built on Sept. 12, 2026, 1:06 a.m.