The clValid package compares two algorithm using two measures 1. Internal measures: Silhouette Coefficient and Dunn Index. 2. Stability Measures : It evaluates the consistency of a clustering result by comparing it with the cluster obtained after each column in removed one at a time. It has four measures APN, AD, ADM and FOM. Value of APN, ADM and FOM ranges between 0 and 1. AD has a value between 0 to infinity, and smaller values are preferred

The format is as follows: clValid(obj, nClust, clMethods = “hierarchical”, validation = “stability”, maxitems = 600, metric = “euclidean”, method = “average”) obj: A numeric matrix or data frame. Rows are the items to be clustered and columns are samples. • nClust: A numeric vector specifying the numbers of clusters to be evaluated. e.g., 2:10 • clMethods: The clustering method to be used. Available options are “hierarchical”, “kmeans”, “diana”, “fanny”, “som”, “model”, “sota”, “pam”, “clara”, and “agnes”, with multiple choices allowed. • validation: The type of validation measures to be used. Allowed values are “internal”, “stability”, and “biological”, with multiple choices allowed. • maxitems: The maximum number of items (rows in matrix) which can be clustered. • metric: The metric used to determine the distance matrix. Possible choices are “euclidean”, “correlation”, and “manhattan”. • method: For hierarchical clustering (hclust and agnes), the agglomeration method to be used. Available choices are “ward”, “single”, “complete” and “average”

`library(clValid)`

`## Warning: package 'clValid' was built under R version 3.6.2`

`## Loading required package: cluster`

`## Warning: package 'cluster' was built under R version 3.6.2`

```
# Iris data set:
# - Remove Species column and scale
df <- scale(iris[, -5])
# Compute clValid
clmethods <- c("hierarchical","kmeans","pam")
intern <- clValid(df, nClust = 2:6,
clMethods = clmethods, validation = "internal")
```

```
## Warning in clValid(df, nClust = 2:6, clMethods = clmethods, validation =
## "internal"): rownames for data not specified, using 1:nrow(data)
```

```
# Summary
summary(intern)
```

```
##
## Clustering Methods:
## hierarchical kmeans pam
##
## Cluster sizes:
## 2 3 4 5 6
##
## Validation Measures:
## 2 3 4 5 6
##
## hierarchical Connectivity 0.9762 5.5964 7.5492 18.0508 24.7306
## Dunn 0.2674 0.1874 0.2060 0.0700 0.0762
## Silhouette 0.5818 0.4803 0.4067 0.3746 0.3248
## kmeans Connectivity 0.9762 23.8151 25.9044 40.3060 40.1385
## Dunn 0.2674 0.0265 0.0700 0.0808 0.0808
## Silhouette 0.5818 0.4599 0.4189 0.3455 0.3441
## pam Connectivity 0.9762 23.0726 31.8067 35.7964 44.5413
## Dunn 0.2674 0.0571 0.0566 0.0642 0.0361
## Silhouette 0.5818 0.4566 0.4091 0.3574 0.3400
##
## Optimal Scores:
##
## Score Method Clusters
## Connectivity 0.9762 hierarchical 2
## Dunn 0.2674 hierarchical 2
## Silhouette 0.5818 hierarchical 2
```

It can be seen that hierarchical clustering with two clusters performs the best in each case (i.e., for connectivity, Dunn and Silhouette measures). Regardless of the clustering algorithm, the optimal number of clusters seems to be two using the three measures.

The stability measures can be computed as follow

```
# Stability measures
clmethods <- c("hierarchical","kmeans","pam")
stab <- clValid(df, nClust = 2:6, clMethods = clmethods,
validation = "stability")
```

```
## Warning in clValid(df, nClust = 2:6, clMethods = clmethods, validation =
## "stability"): rownames for data not specified, using 1:nrow(data)
```

```
# Display only optimal Scores
optimalScores(stab)
```

```
## Score Method Clusters
## APN 0.003266667 hierarchical 2
## AD 1.004288856 pam 6
## ADM 0.016087089 hierarchical 2
## FOM 0.455750052 pam 6
```

For the APN and ADM measures, hierarchical clustering with two clusters again gives the best score. For the other measures, PAM with six clusters has the best score.