predict.kMeans: Predict Method for K-Means Clustering

Description Usage Arguments Value See Also Examples

View source: R/predict.kMeans.R

Description

This function assigns observations in the data matrix newData the most likeliest clusters using the best solution from a kMeans object.

Usage

1
predict.kMeans(X, newData)

Arguments

X

object of class kMeans.

newData

a data matrix or data frame having the same columns as the original X when kMeans was called.

Value

Returns a vector of cluster assignments for newData based on the kMeans object.

See Also

kMeans

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
set.seed(63555)
exampleData <- matrix(nrow=90, ncol=1)
exampleData[1:30, 1] <- rnorm(30, mean=3, sd=1)
exampleData[31:60, 1] <- rnorm(30, mean=6, sd=1)
exampleData[61:90, 1] <- rnorm(30, mean=9, sd=1)
kMeansResult <- kMeans(exampleData, k=3)

fitted(kMeansResult)
# [1] 2 1 2 2 2 1 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1
# [47] 1 1 3 1 1 1 1 1 1 1 1 1 3 1 3 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

newData <- matrix(nro=30, ncol=1)
newData[1:30, 1] <- rnorm(30, mean=6, sd=1)
predict(kMeansResult, newData)
# [1] 1 1 1 1 1 1 2 1 1 1 1 1 1 1 2 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1

rintakumpu/custom-kmeans documentation built on May 3, 2019, 10:45 p.m.