-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-heirarchical-clust.Rmd
216 lines (189 loc) · 4.85 KB
/
08-heirarchical-clust.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
---
title: "Heirarchical Clustering"
author: "Kundan K. Rao"
date: "01/12/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Hierarchical Clustering
Method in distance matrix:-
Euclidean
Linkage method in clustering algorithm:-
1. Complete
2. WARD.D2
```{r}
# hierarchical clustering
dist.mat <- dist(data.scaled, method = "euclidean")
hc.complete<-hclust(dist.mat,method = "complete")
hc.avg<-hclust(dist.mat,method = "average")
hc.ward<-hclust(dist.mat,method = "ward.D2")
```
```{r}
#str(as.dendrogram(hc.ward))
```
```{r}
# ISL book
# dendrograms plot
par(mfrow=c(1,2))
plot(hc.complete,main="Complete Linkage", xlab="",sub="",cex=.9)
plot(hc.ward,main="WARD D2", xlab="",sub="",cex=.9)
```
```{r}
# visualising dendograms
# clustering book
par(mfrow=c(1,2))
fviz_dend(hc.complete, cex = 0.5)
```
```{r}
fviz_dend(hc.eucl, cex = 0.5)
```
```{r}
fviz_dend(hc.avg, cex = .5)
```
Verify cluster tree:-
```{r}
# cophenetic distance
hc.complete.coph <- cophenetic(hc.complete)
hc.avg.coph <- cophenetic(hc.avg)
hc.ward.coph <- cophenetic(hc.ward)
# correlation b/w the cophenetic distance and original distance
cor(dist.mat, hc.complete.coph)
cor(dist.mat, hc.ward.coph)
cor(dist.mat, hc.avg.coph)
```
```{r}
# ISL book
# each wine's cluster
grp.complete <- cutree(hc.complete,3)
grp.avg <- cutree(hc.avg, 9)
grp.ward <- cutree(hc.ward,3)
table(grp.complete)
table(grp.avg)
table(grp.ward)
```
```{r}
grp.avg
grp.complete
grp.ward
```
Visualising cutree
```{r}
# Cut in 4 groups and color by groups
fviz_dend(hc.ward, k = 3, # Cut in four groups
cex = 0.5, # label size
k_colors = c("#2E66DF", "#00AFBB", "#E7B800"),
color_labels_by_k = TRUE, # color labels by groups
rect = TRUE, # Add rectangle around groups
rect_border = c("#2E66DF", "#00AFBB", "#E7B800"),
rect_fill = TRUE,
main = "Dendrogram - ward.D2",
xlab = "Objects", ylab = "Distance", sub = "",
ggtheme = theme_minimal()
)
```
```{r}
# Cut in 4 groups and color by groups
fviz_dend(hc.complete, k = 3, # Cut in four groups
cex = 0.5, # label size
k_colors = c("#2E00DF", "#55AFBB", "#E7B800"),
color_labels_by_k = TRUE, # color labels by groups
rect = TRUE # Add rectangle around groups
)
```
```{r}
# Cut in 4 groups and color by groups
fviz_dend(hc.avg, k = 9, # Cut in four groups
cex = 0.5, # label size
#k_colors = c("#2E9F00", "#00AFBB", "#E7B866"),
color_labels_by_k = TRUE, # color labels by groups
rect = TRUE # Add rectangle around groups
)
```
```{r}
# circular visualisation
fviz_dend(hc.ward, cex = 0.5, k = 3,
k_colors = "jco", type = "circular")
```
```{r}
library(igraph)
fviz_dend(hc.ward, k = 3, k_colors = "jco",
type = "phylogenic", repel = TRUE,
phylo_layout = "layout_as_tree"
)
```
As cluster:-
```{r}
fviz_cluster(list(data = data.scaled, cluster = grp.complete),
#palette = c("#2E9FDF", "#00AFBB", "#E7B800", "#FC4E07"),
ellipse.type = "convex", # Concentration ellipse
repel = TRUE, # Avoid label overplotting (slow)
show.clust.cent = FALSE, ggtheme = theme_minimal())
```
```{r}
fviz_cluster(list(data = data.scaled, cluster = grp.ward),
#palette = c("#2E9FDF", "#00AFBB", "#E7B800", "#FC4E07"),
ellipse.type = "convex", # Concentration ellipse
repel = TRUE, # Avoid label overplotting (slow)
show.clust.cent = FALSE, ggtheme = theme_minimal())
```
```{r}
fviz_cluster(list(data = data.scaled, cluster = grp.avg),
#palette = c("#2E9FDF", "#00AFBB", "#E7B800", "#FC4E07"),
ellipse.type = "convex", # Concentration ellipse
repel = TRUE, # Avoid label overplotting (slow)
show.clust.cent = FALSE, ggtheme = theme_minimal())
```
Using cluster package for hirarchical clustering
```{r}
library("cluster")
# Agglomerative Nesting (Hierarchical Clustering)
hc.agnes <- agnes(x = data, # data matrix
stand = TRUE, # Standardize the data
metric = "euclidean", # metric for distance matrix
method = "ward" # Linkage method
)
# DIvisive ANAlysis Clustering
hc.diana <- diana(x = data, # data matrix
stand = TRUE, # standardize the data
metric = "euclidean" # metric for distance matrix
)
fviz_dend(hc.agnes, cex = 0.6, k=3)
```
```{r}
fviz_dend(hc.diana, cex = 0.6, k=3)
```
Comparing dendograms:-
```{r}
library(dendextend)
# creating two dendograms
dend1 <- as.dendrogram(hc.avg)
dend2 <- as.dendrogram(hc.ward)
# creating a list to hold the dendograms
dend.list <- dendlist(dend1, dend2)
```
```{r}
tanglegram(dend1,dend2,
highlight_distinct_edges = FALSE, # Turn-off dashed lines
common_subtrees_color_lines = FALSE, # Turn-off line colors
common_subtrees_color_branches = TRUE, # Color common branches
main = paste("entanglement =", round(entanglement(dend.list), 2))
)
```
```{r}
# Cophenetic correlation matrix
cor.dendlist(dend.list, method = "cophenetic")
```
```{r}
# Baker correlation matrix
cor.dendlist(dend.list, method = "baker")
```
```{r}
# Cophenetic correlation coefficient
cor_cophenetic(dend1, dend2)
```
```{r}
# Baker correlation coefficient
cor_bakers_gamma(dend1, dend2)
```