# Choose and rename the dendrogram ("hclust" object)
hc <- spe.ch.ward
# Plot average silhouette widths (using Ward clustering) for all
# partitions except for the trivial partitions (k = 1 or k = n)
Si <- numeric(nrow(spe))
for (k in 2:(nrow(spe) - 1))
{
sil <- silhouette(cutree(hc, k = k), spe.ch)
Si[k] <- summary(sil)$avg.width
}
k.best <- which.max(Si)
plot(
1:nrow(spe),
Si,
type = "h",
main = "Silhouette-optimal number of clusters",
xlab = "k (number of clusters)",
ylab = "Average silhouette width"
)
axis(
1,
k.best,
paste("optimum", k.best, sep = "\n"),
col = "red",
font = 2,
col.axis = "red"
)
points(k.best,
max(Si),
pch = 16,
col = "red",
cex = 1.5)
Hint Observe how the repeated computation of the average silhouette widths is done by
a for() loop.
As it often happens, this criterion has selected two groups as the optimal number.
Considering the site numbers (reflecting their location along the river) in the
dendrogram (Fig. 4.10), what is the explanation of this partitionin two groups? Is
it interesting from an ecological point of view?
82
4 Cluster Analysis
hc <- spe.ch.ward
# Plot average silhouette widths (using Ward clustering) for all
# partitions except for the trivial partitions (k = 1 or k = n)
Si <- numeric(nrow(spe))
for (k in 2:(nrow(spe) - 1))
{
sil <- silhouette(cutree(hc, k = k), spe.ch)
Si[k] <- summary(sil)$avg.width
}
k.best <- which.max(Si)
plot(
1:nrow(spe),
Si,
type = "h",
main = "Silhouette-optimal number of clusters",
xlab = "k (number of clusters)",
ylab = "Average silhouette width"
)
axis(
1,
k.best,
paste("optimum", k.best, sep = "\n"),
col = "red",
font = 2,
col.axis = "red"
)
points(k.best,
max(Si),
pch = 16,
col = "red",
cex = 1.5)
Hint Observe how the repeated computation of the average silhouette widths is done by
a for() loop.
As it often happens, this criterion has selected two groups as the optimal number.
Considering the site numbers (reflecting their location along the river) in the
dendrogram (Fig. 4.10), what is the explanation of this partitionin two groups? Is
it interesting from an ecological point of view?
82
4 Cluster Analysis
