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

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

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.

Value

a numeric vector of length

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

## 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.method = 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={})
}
#> 
#>  1  2 
#>  8 42 
#> k = 2 ; sort(medoids) = c(1, 31)
#> 
#>  1  2  3 
#>  8 22 20 
#> k = 3 ; sort(medoids) = c(1, 13, 31)
#> 
#>  1  2  3  4 
#>  6 22 20  2 
#> k = 4 ; sort(medoids) = c(10, 13, 31, 40)
#> 
#>  1  2  3  4  5 
#>  6 17 20  5  2 
#> k = 5 ; sort(medoids) = c(10, 11, 13, 31, 40)
#> 
#>  1  2  3  4  5  6 
#>  6 17  8 12  5  2 
#> k = 6 ; sort(medoids) = c(10, 11, 13, 26, 31, 40)
#> 
#>  1  2  3  4  5  6  7 
#>  6 17  8  9  5  2  3 
#> k = 7 ; sort(medoids) = c(10, 11, 13, 26, 31, 33, 40)
#> 
#>  1  2  3  4  5  6  7  8 
#>  6 10  8  9  5  7  2  3 
#> k = 8 ; sort(medoids) = c(10, 11, 13, 15, 26, 31, 33, 40)
#> 
#>  1  2  3  4  5  6  7  8  9 
#>  6 10  8  9  3  7  2  2  3 
#> k = 9 ; sort(medoids) = c(10, 11, 13, 15, 26, 31, 33, 39, 40)
#> 
#>  1  2  3  4  5  6  7  8  9 10 
#>  3 10  8  3  9  3  7  2  2  3 
#> k = 10 ; sort(medoids) = c(9, 10, 11, 13, 15, 26, 31, 33, 39, 40)
#> 
#>  1  2  3  4  5  6  7  8  9 10 11 
#>  3 10  8  3  3  6  3  7  2  2  3 
#> k = 11 ; sort(medoids) = c(9, 10, 11, 13, 15, 26, 31, 33, 37, 39, 40)