Seeds Data: Hierarchical Clustering
data <- read.delim("seeds.txt")
data_x <- data[1:7]
distance <- dist(data_x, method = "euclidean")
hc1 <- hclust(distance, method = "complete")
hc2 <- hclust(distance, method = "single")
hc3 <- hclust(distance, method = "average")
store <- c()
k <- c()
for (i in 2:8){
k <- c(k, i)
ct <- cutree(hc1, k=i)
si <- silhouette(ct, dist = distance)
avg_width <- summary(si)$avg.width
store <- c(store, avg_width)
}
store
## [1] 0.4803344 0.4382979 0.3530563 0.3230861 0.3351882 0.3421507 0.3240868
store2 <- c()
for (i in 2:8){
ct <- cutree(hc2, k=i)
si <- silhouette(ct, dist = distance)
avg_width <- summary(si)$avg.width
store2 <- c(store2, avg_width)
}
store2
## [1] 0.19704660 0.04512166 -0.09116026 -0.10513023 -0.12712941 -0.20982970
## [7] -0.30336593
store3 <- c()
for (i in 2:8){
ct <- cutree(hc3, k=i)
si <- silhouette(ct, dist = distance)
avg_width <- summary(si)$avg.width
store3 <- c(store3, avg_width)
}
store3
## [1] 0.5058256 0.4438162 0.3876672 0.3544674 0.3342508 0.3453153 0.3714800
x11()
plot(k, store, main = 'Average Silhouttes for Different k Values and Linkages',xlab = 'k value', ylab = 'Average Silhouettes',type = 'o', col = 'blue',ylim=c(-0.5,0.7), lty = 1)
points(k, store2, col="purple", pch="x")
lines(k, store2, col="purple", lty = 2)
points(k, store3, col="red", pch="+")
lines(k, store3, col="red", lty = 3)
legend(2, -.15,legend=c("complete","single","average"), col=c("blue","purple","red"),
pch=c("o","x","+"),lty=c(1,2,3), ncol=1)

Create 2 clusters
## 2 Clusters cut
ct1 <- cutree(hc1, k=2)
si1 <- silhouette(ct1, dist = distance)
ct2 <- cutree(hc2, k=2)
si2 <- silhouette(ct2, dist = distance)
ct3 <- cutree(hc3, k=2)
si3 <- silhouette(ct3, dist = distance)
## Plots with 2 clusters
x11()
plot(si1, col = 2:3)

x11()
plot(si2, col = 2:3)

x11()
plot(si3, col = 2:3)

Create 3 clusters
ct1 <- cutree(hc1, k=3)
si1 <- silhouette(ct1, dist = distance)
ct2 <- cutree(hc2, k=3)
si2 <- silhouette(ct2, dist = distance)
ct3 <- cutree(hc3, k=3)
si3 <- silhouette(ct3, dist = distance)
x11()
plot(si1, col = 2:4)

x11()
plot(si2, col = 2:4)

x11()
plot(si3, col = 2:4)

Plots of seed data clustering with different linkages
idx <- sample(c(1:length(data[,1])), 70)
data <- data[idx, ]
data_x <- data[1:7]
distance <- dist(data_x, method = "euclidean")
hc1 <- hclust(distance, method = "complete")
hc2 <- hclust(distance, method = "single")
hc3 <- hclust(distance, method = "average")
x11()
plot(hc1, hang = -1, labels = data$Seed.Group)
rect.hclust(hc1, k=3, border="red")

x11()
plot(hc2, hang = -1, labels = data$Seed.Group) ## Not great
rect.hclust(hc2, k=2, border="blue")

x11()
plot(hc3, hang = -1, labels = data$Seed.Group)
rect.hclust(hc3, k=3, border="green")

ct1 <- cutree(hc1, k = 3)
ct2 <- cutree(hc2, k = 3)
ct3 <- cutree(hc3, k = 3)
table(ct1, data$Seed.Group)
##
## ct1 A B C
## 1 0 0 18
## 2 0 16 0
## 3 23 3 10
table(ct2, data$Seed.Group)
##
## ct2 A B C
## 1 23 18 27
## 2 0 1 0
## 3 0 0 1
table(ct3, data$Seed.Group)
##
## ct3 A B C
## 1 3 0 28
## 2 0 15 0
## 3 20 4 0
Seed Data: K-means Clustering
#### Part (b) Kmeans Clustering
## gap statistics - kmeans
gap_kmeans <- clusGap(data_x, kmeans, nstart = 20, K.max = 10, B = 100)
x11()
plot(gap_kmeans, main = "Gap Statistic: kmeans")

## kmeans
data_x <- scale(data_x)
km <- kmeans(data_x, centers = 3, nstart = 10)
x11()
plot(data_x[ ,c("Length.Kernel", "Width.Kernel")], col = km$cluster, main = "Example: k-means")
points(km$centers[, c("Length.Kernel", "Width.Kernel")], col = 1:3, pch = 8, cex = 2)

## how well does the clustering match the labels
table(km$cluster, data$Seed.Group)
##
## A B C
## 1 0 19 0
## 2 22 0 3
## 3 1 0 25
length(km$cluster)
## [1] 70
length(data$Seed.Group)
## [1] 70