R/summary.R

Defines functions summary.kMeans

Documented in summary.kMeans

#' Summarise results of k-means clustering
#'
#' \code{summary} method for class \code{"kMeans"}.
#' @param object an object of class \code{"kMeans"}, usually a result of a call
#' to \code{kMeansLloyd}.
#' @param ... further arguments passed to or from other methods.
#'
#' @return The function \code{summary.kMeans} returns a list of class
#' \code{kMeansSummary}. In addition to the elements of an object of class
#' \code{"kMeans"}, it contains the following elements:
#' \item{totalSS}{total sum of squares.}
#' \item{betweenSS}{between sum of squares}
#' @export
#'
#' @examples
#' # create example data set
#' X <- rbind(matrix(rnorm(50, sd = 0.5), ncol = 2),
#' matrix(rnorm(50, mean = 1, sd = 0.5), ncol = 2))
#'
#' # perform k-means algorithm
#' result <- kMeansLloyd(x = X, centroids = 2, nStart = 2)
#'
#' # summarise result
#' summary(result)
#' @seealso \code{\link{kMeansLloyd}}, \code{\link{print.kMeansSummary}}
summary.kMeans <- function(object, ...){
  out <- object
  total <- sum(t((t(out$data) - colMeans(out$data))^2))
  out$totalSS <- total
  out$betweenSS <- total - out$withinTot
  class(out) <- "kMeansSummary"

  out
}
heiligerl/kMeans_Rpackage documentation built on Aug. 16, 2020, 4:04 p.m.