fitted.kMeans: Fitting an object of class kMeans

Description Usage Arguments See Also Examples

View source: R/fitted.R

Description

Extract the fitted values from a data set modeled by kMeansLloyd.

Usage

1
2
## S3 method for class 'kMeans'
fitted(object, method = c("centroids", "cluster"), ...)

Arguments

object

an object of class "kMeans". Usually, the result of kMeansLloyd.

method

character: either "centroids" or "cluster". If "centroids" leads to fitted returning the cluster centroids one for each observation, if "cluster" causes fitted to return a vector of class assignments.

...

other arguments.

See Also

kMeansLloyd

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 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)

# fit the result
# centroids
fitted(result, method = "centroids")
# cluster
fitted(result, method = "cluster")

# compute cluster centroids "fitted" to each observation:
fittedX <- fitted(result)
residX <- X - fittedX

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