Learn R Programming

cluster (version 2.1.6)

medoids: Compute pam-consistent Medoids from Clustering

Description

Given a data matrix or dissimilarity x for say \(n\) observational units and a clustering, compute the pam()-consistent medoids.

Usage

medoids(x, clustering, diss = inherits(x, "dist"), USE.NAMES = FALSE, ...)

Value

a numeric vector of length

Arguments

x

Either a data matrix or data frame, or dissimilarity matrix or object, see also pam.

clustering

an integer vector of length \(n\), the number of observations, giving for each observation the number ('id') of the cluster to which it belongs. In other words, clustering has values from 1:k where k is the number of clusters, see also partition.object and cutree(), for examples where such clustering vectors are computed.

diss

see also pam.

USE.NAMES

a logical, typical false, passed to the vapply() call computing the medoids.

...

optional further argument passed to pam(xj, k=1, ...), notably metric, or variant="f_5" to use a faster algorithm, or trace.lev = k.

Author

Martin Maechler, after being asked how pam() could be used instead of kmeans(), starting from a previous clustering.

See Also

pam, kmeans. Further, cutree() and agnes (or hclust).

Examples

Run this code
## From example(agnes):
data(votes.repub)
agn1 <- agnes(votes.repub, metric = "manhattan", stand = TRUE)
agn2 <- agnes(daisy(votes.repub), diss = TRUE, method = "complete")
agnS <- agnes(votes.repub, method = "flexible", par.meth = 0.625)

for(k in 2:11) {
  print(table(cl.k <- cutree(agnS, k=k)))
  stopifnot(length(cl.k) == nrow(votes.repub), 1 <= cl.k, cl.k <= k, table(cl.k) >= 2)
  m.k <- medoids(votes.repub, cl.k)
  cat("k =", k,"; sort(medoids) = "); dput(sort(m.k), control={})
}

Run the code above in your browser using DataLab