| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/find_k.R |
| 3 |
\name{find_k} |
| 4 |
\alias{find_k} |
| 5 |
\alias{plot.find_k} |
| 6 |
\title{Find the (estimated) number of clusters for a dendrogram using average silhouette width} |
| 7 |
\usage{ |
| 8 |
find_k(dend, krange = 2:min(10, (nleaves(dend) - 1)), ...) |
| 9 |
|
| 10 |
\method{plot}{find_k}( |
| 11 |
x, |
| 12 |
xlab = "Number of clusters (k)", |
| 13 |
ylab = "Average silhouette width", |
| 14 |
main = "Estimating the number of clusters using\\n average silhouette width", |
| 15 |
... |
| 16 |
) |
| 17 |
} |
| 18 |
\arguments{ |
| 19 |
\item{dend}{A dendrogram (or hclust) tree object} |
| 20 |
|
| 21 |
\item{krange}{integer vector. Numbers of clusters which are to be compared |
| 22 |
by the average silhouette width criterion. |
| 23 |
Note: average silhouette width and Calinski-Harabasz can't estimate number |
| 24 |
of clusters nc=1. If 1 is included, a Duda-Hart test is applied and 1 is |
| 25 |
estimated if this is not significant.} |
| 26 |
|
| 27 |
\item{...}{passed to \link[fpc]{pamk} (the current defaults criterion="asw" and usepam=TRUE can not be changes).} |
| 28 |
|
| 29 |
\item{x}{An object of class "find_k" (has its own S3 plot method).} |
| 30 |
|
| 31 |
\item{xlab, ylab, main}{parameters passed to plot.} |
| 32 |
} |
| 33 |
\value{ |
| 34 |
A \link[fpc]{pamk} output. This is a list with the following components: |
| 35 |
1) pamobject - The output of the optimal run of the pam-function. |
| 36 |
2) nc - the optimal number of clusters. |
| 37 |
3) crit - vector of criterion values for numbers of clusters. crit[1] is the p-value of the Duda-Hart test if 1 is in krange and diss=FALSE. |
| 38 |
4) k - a copy of nc (just to make it easier to extract - since k is often used in other functions) |
| 39 |
} |
| 40 |
\description{ |
| 41 |
This function estimates the number of clusters based on the maximal average \link[cluster]{silhouette} width |
| 42 |
derived from running \link[cluster]{pam} on the \link[stats]{cophenetic} distance matrix of |
| 43 |
the \link[stats]{dendrogram}. The output is based on the \link[fpc]{pamk} output. |
| 44 |
} |
| 45 |
\examples{ |
| 46 |
|
| 47 |
dend <- iris[, -5] \%>\% |
| 48 |
dist() \%>\% |
| 49 |
hclust() \%>\% |
| 50 |
as.dendrogram() |
| 51 |
dend_k <- find_k(dend) |
| 52 |
plot(dend_k) |
| 53 |
plot(color_branches(dend, k = dend_k$nc)) |
| 54 |
|
| 55 |
library(cluster) |
| 56 |
sil <- silhouette(dend_k$pamobject) |
| 57 |
plot(sil) |
| 58 |
|
| 59 |
dend <- USArrests \%>\% |
| 60 |
dist() \%>\% |
| 61 |
hclust(method = "ave") \%>\% |
| 62 |
as.dendrogram() |
| 63 |
dend_k <- find_k(dend) |
| 64 |
plot(dend_k) |
| 65 |
plot(color_branches(dend, k = dend_k$nc)) |
| 66 |
} |
| 67 |
\seealso{ |
| 68 |
\link[fpc]{pamk}, \link[cluster]{pam}, \link[cluster]{silhouette}. |
| 69 |
} |