R/wss.R

Defines functions wss

Documented in wss

# Function to calculate the Weighted Sum of Squares
# x is a data matrix or data frame, w is weight vector

wss = function(x, w = rep(1,nrow(x))){

  x = as.data.frame(x)

  if (length(w) != nrow(x)){
    stop("The length of weights should be the same as the number of observations. ")
  }
  
  weighted_mean <- c(t(x)%*%w)/sum(w)
  sum(t(sweep(x, 2, weighted_mean)^2)%*%w)

}

Try the WCluster package in your browser

Any scripts or data that you put into this service are public.

WCluster documentation built on April 3, 2025, 7:50 p.m.