-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyword_clustering.R
335 lines (238 loc) · 12.9 KB
/
keyword_clustering.R
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# get the Hadley Wickham's vehicles data set
library(dplyr)
library(sparklyr)
spark_install(version = "2.0.1")
library(stringdist)
library(ggdendro)
library(ape)
library(ggplot2)
library(stringdist)
write.csv(kwp,"kwp_dl.csv")
loankws <- unique(as.character(kd[kd$Category == "loans",3]))
creditcardkws <- unique(as.character(kd[kd$Category == "creditcard",3]))
# call the stringdistmatrix function and request 20 groups
loans_dist_jw <- stringdistmatrix(loankws, loankws, method = "jw")
loans_dist_ja <- stringdistmatrix(loankws,loankws,method = "ja")
loans_dist_lv <- stringdistmatrix(loankws,loankws,method = "lv")
loans_dist_dl <- stringdistmatrix(loankws,loankws,method = "dl")
creditcards_dist_jw <- stringdistmatrix(creditcardkws,creditcardkws,method = "jw")
creditcards_dist_ja <- stringdistmatrix(creditcardkws,creditcardkws,method = "ja")
creditcards_dist_lv <- stringdistmatrix(creditcardkws,creditcardkws,method = "lv")
creditcards_dist_dl <- stringdistmatrix(creditcardkws,creditcardkws,method = "dl")
rownames(loans_dist_jw) <- loankws
rownames(loans_dist_ja) <- loankws
rownames(loans_dist_lv) <- loankws
rownames(loans_dist_dl) <- loankws
rownames(creditcards_dist_jw) <- creditcardkws
rownames(creditcards_dist_ja) <- creditcardkws
rownames(creditcards_dist_lv) <- creditcardkws
rownames(creditcards_dist_dl) <- creditcardkws
hc_loans_jw <- hclust(as.dist(loans_dist_jw))
hc_loans_ja <- hclust(as.dist(loans_dist_ja))
hc_loans_lv <- hclust(as.dist(loans_dist_lv))
hc_loans_dl <- hclust(as.dist(loans_dist_dl))
hc_creditcards_jw <- hclust(as.dist(creditcards_dist_jw))
hc_creditcards_ja <- hclust(as.dist(creditcards_dist_ja))
hc_creditcards_lv <- hclust(as.dist(creditcards_dist_lv))
hc_creditcards_dl <- hclust(as.dist(creditcards_dist_dl))
#Kmeans
# Cluster Plot against 1st 2 principal components
# vary parameters for most readable graph
library(cluster)
loansfit1 <- kmeans(as.dist(loans_dist_jw), k)
loansfit2 <- kmeans(as.dist(loans_dist_ja), k)
loansfit3<- kmeans(as.dist(loans_dist_lv), k)
loansfit4<- kmeans(as.dist(loans_dist_dl), k)
ccfit1 <- kmeans(as.dist(creditcards_dist_jw), k)
ccfit2 <- kmeans(as.dist(creditcards_dist_ja), k)
ccfit3 <- kmeans(as.dist(creditcards_dist_lv), k)
ccfit4 <- kmeans(as.dist(creditcards_dist_dl), k)
library(class)
# kmeans clustering
cckfit1 <- kmeans(as.dist(creditcards_dist_jw), k)
cckfit2 <- kmeans(as.dist(creditcards_dist_ja), k)
cckfit3 <- kmeans(as.dist(creditcards_dist_lv), k)
cckfit4 <- kmeans(as.dist(creditcards_dist_dl), k)
lkfit1 <- kmeans(as.dist(loans_dist_jw), k)
lkfit2 <- kmeans(as.dist(loans_dist_ja), k)
lkfit3 <- kmeans(as.dist(loans_dist_lv), k)
lkfit4 <- kmeans(as.dist(loans_dist_dl), k)
creditcards_dist_jw_p0 <- stringdistmatrix(creditcardkws,creditcardkws,method = "jw", p = 0.0)
creditcards_dist_jw_p01 <- stringdistmatrix(creditcardkws,creditcardkws,method = "jw", p = 0.1)
loans_dist_jw_p0 <- stringdistmatrix(loankws, loankws, method = "jw", p = 0.0)
loans_dist_jw_p01 <- stringdistmatrix(loankws, loankws, method = "jw", p = 0.1)
cckfit1p0 <- kmeans(as.dist(creditcards_dist_jw_p0), 10)
cckfit1p01 <- kmeans(as.dist(creditcards_dist_jw_p01), 10)
lkfit1p0 <- kmeans(as.dist(loans_dist_jw_p0), 10)
lkfit1p01 <- kmeans(as.dist(loans_dist_jw_p01), 10)
clusplot(as.matrix(creditcards_dist_jw_p0), cckfit1p0$cluster)
clusplot(as.matrix(creditcards_dist_jw_p01), cckfit1p01$cluster)
clusplot(as.matrix(loans_dist_jw_p0), lkfit1p0$cluster)
clusplot(as.matrix(loans_dist_jw_p01), lkfit1p01$cluster)
#plot – need library cluster
library(cluster)
clusplot(as.matrix(creditcards_dist_jw), cckfit1$cluster)
clusplot(as.matrix(creditcards_dist_ja), cckfit2$cluster)
clusplot(as.matrix(creditcards_dist_lv), cckfit3$cluster)
clusplot(as.matrix(creditcards_dist_dl), cckfit4$cluster)
clusplot(as.matrix(loans_dist_jw), lkfit1$cluster)
clusplot(as.matrix(loans_dist_ja), lkfit2$cluster)
clusplot(as.matrix(loans_dist_lv), lkfit3$cluster)
clusplot(as.matrix(loans_dist_dl), lkfit4$cluster)
# DL = Damerau-Levenshtein distance
# LV = Levenshtein distance
# JA = Jaccard
# JW = Jaro-Winkler
clusplot(as.matrix(loans_dist_jw), lkfit1$cluster)
ccfit1jw <- kmeans(as.dist(creditcards_dist_jw), k)
ccfit2jw <- kmeans(as.dist(creditcards_dist_jw), k)
lfit1jw <- kmeans(as.dist(loans_dist_jw), k)
lfit2jw <- kmeans(as.dist(loans_dist_jw), k)
creditcards_dist_jw_p0 <- stringdistmatrix(creditcardkws,creditcardkws,method = "jw", p = 0.0)
creditcards_dist_jw_p01 <- stringdistmatrix(creditcardkws,creditcardkws,method = "jw", p = 0.1)
loans_dist_jw_p0 <- stringdistmatrix(loankws, loankws, method = "jw", p = 0.0)
loans_dist_jw_p01 <- stringdistmatrix(loankws, loankws, method = "jw", p = 0.1)
cckfit1p0 <- kmeans(as.dist(creditcards_dist_jw_p0), 10)
cckfit1p01 <- kmeans(as.dist(creditcards_dist_jw_p01), 10)
lkfit1p0 <- kmeans(as.dist(loans_dist_jw_p0), 10)
lkfit1p01 <- kmeans(as.dist(loans_dist_jw_p01), 10)
clusplot(as.matrix(creditcards_dist_jw_p0), cckfit1p0$cluster)
clusplot(as.matrix(creditcards_dist_jw_p01), cckfit1p01$cluster)
clusplot(as.matrix(loans_dist_jw_p0), lkfit1p0$cluster)
clusplot(as.matrix(loans_dist_jw_p01), lkfit1p01$cluster)
hc_loans_jw_p0 <- hclust(as.dist(loans_dist_jw_p0))
hc_loans_jw_p01 <- hclust(as.dist(loans_dist_jw_p01))
hc_loans_ja <- hclust(as.dist(loans_dist_ja))
hc_loans_lv <- hclust(as.dist(loans_dist_lv))
hc_loans_dl <- hclust(as.dist(loans_dist_dl))
hc_creditcards_jw_p0 <- hclust(as.dist(creditcards_dist_jw_p0))
hc_creditcards_jw_p01 <- hclust(as.dist(creditcards_dist_jw_p01))
hc_creditcards_ja <- hclust(as.dist(creditcards_dist_ja))
hc_creditcards_lv <- hclust(as.dist(creditcards_dist_lv))
hc_creditcards_dl <- hclust(as.dist(creditcards_dist_dl))
#Kmeans
dendr_ljw_p0 <- dendro_data(hc_loans_jw_p0, type="rectangle") # convert for ggplot
dendr_ljw_p01 <- dendro_data(hc_loans_jw_p01, type="rectangle") # convert for ggplot
dendr_lja <- dendro_data(hc_loans_ja, type="rectangle") # convert for ggplot
dendr_llv <- dendro_data(hc_loans_lv, type="rectangle") # convert for ggplot
dendr_ldl <- dendro_data(hc_loans_dl, type="rectangle") # convert for ggplot
dendr_ccjw_p0 <- dendro_data(hc_creditcards_jw_p0, type="rectangle") # convert for ggplot
dendr_ccjw_p01 <- dendro_data(hc_creditcards_jw_p01, type="rectangle") # convert for ggplot
dendr_ccja <- dendro_data(hc_creditcards_ja, type="rectangle") # convert for ggplot
dendr_cclv <- dendro_data(hc_creditcards_lv, type="rectangle") # convert for ggplot
dendr_ccdl <- dendro_data(hc_creditcards_dl, type="rectangle") # convert for ggplot
#install.packages('dendextend')
library(dendextend)
k=10
library(ggplot2)
library(ggdendro)
#hc_loans_jw
#hc_loans_ja
#HClust
clustljw_p0 = cutree(hc_loans_jw_p0, k=k)
clustljw_p01 = cutree(hc_loans_jw_p01, k=k)
clustlja = cutree(hc_loans_ja, k=k)
clustllv = cutree(hc_loans_lv, k=k)
clustldl = cutree(hc_loans_dl, k=k)
clustcjw_p0 = cutree(hc_creditcards_jw_p0, k=k)
clustcjw_p01 = cutree(hc_creditcards_jw_p01, k=k)
clustcja = cutree(hc_creditcards_ja, k=k)
clustclv = cutree(hc_creditcards_lv, k=k)
clustcdl = cutree(hc_creditcards_dl, k=k)
hc_loans_jw_p0 <- hclust(as.dist(loans_dist_jw_p0))
hc_loans_jw_p01 <- hclust(as.dist(loans_dist_jw_p01))
hc_loans_ja <- hclust(as.dist(loans_dist_ja))
hc_loans_lv <- hclust(as.dist(loans_dist_lv))
hc_loans_dl <- hclust(as.dist(loans_dist_dl))
hc_creditcards_jw_p0 <- hclust(as.dist(creditcards_dist_jw_p0))
hc_creditcards_jw_p01 <- hclust(as.dist(creditcards_dist_jw_p01))
hc_creditcards_ja <- hclust(as.dist(creditcards_dist_ja))
hc_creditcards_lv <- hclust(as.dist(creditcards_dist_lv))
hc_creditcards_dl <- hclust(as.dist(creditcards_dist_dl))
clust.ljw_p0.df <- data.frame(label=names(clustljw_p0), cluster=factor(clustljw_p0))
clust.ljw_p01.df <- data.frame(label=names(clustljw_p01), cluster=factor(clustljw_p01))
clust.lja.df <- data.frame(label=names(clustlja), cluster=factor(clustlja))
clust.llv.df <- data.frame(label=names(clustllv), cluster=factor(clustllv))
clust.ldl.df <- data.frame(label=names(clustldl), cluster=factor(clustldl))
clust.cjw_p0.df <- data.frame(label=names(clustcjw_p0), cluster=factor(clustcjw_p0))
clust.cjw_p01.df <- data.frame(label=names(clustcjw_p01), cluster=factor(clustcjw_p01))
clust.cja.df <- data.frame(label=names(clustcja), cluster=factor(clustcja))
clust.clv.df <- data.frame(label=names(clustclv), cluster=factor(clustclv))
clust.cdl.df <- data.frame(label=names(clustcdl), cluster=factor(clustcdl))
dendr_ljw_p0 <- dendro_data(hc_loans_jw_p0, type="rectangle") # convert for ggplot
dendr_ljw_p01 <- dendro_data(hc_loans_jw_p01, type="rectangle") # convert for ggplot
dendr_lja <- dendro_data(hc_loans_ja, type="rectangle") # convert for ggplot
dendr_llv <- dendro_data(hc_loans_lv, type="rectangle") # convert for ggplot
dendr_ldl <- dendro_data(hc_loans_dl, type="rectangle") # convert for ggplot
dendr_cjw_p0 <- dendro_data(hc_creditcards_jw_p0, type="rectangle") # convert for ggplot
dendr_cjw_p01 <- dendro_data(hc_creditcards_jw_p01, type="rectangle") # convert for ggplot
dendr_cja <- dendro_data(hc_creditcards_ja, type="rectangle") # convert for ggplot
dendr_clv <- dendro_data(hc_creditcards_lv, type="rectangle") # convert for ggplot
dendr_cdl <- dendro_data(hc_creditcards_dl, type="rectangle") # convert for ggplot
dendr_ljw_p0[["labels"]] <- merge(dendr_ljw_p0[["labels"]],clust.ljw_p0.df, by="label")
dendr_ljw_p01[["labels"]] <- merge(dendr_ljw_p01[["labels"]],clust.ljw_p01.df, by="label")
dendr_lja[["labels"]] <- merge(dendr_lja[["labels"]],clust.lja.df, by="label")
dendr_llv[["labels"]] <- merge(dendr_llv[["labels"]],clust.llv.df, by="label")
dendr_ldl[["labels"]] <- merge(dendr_ldl[["labels"]],clust.ldl.df, by="label")
dendr_cjw_p0[["labels"]] <- merge(dendr_cjw_p0[["labels"]],clust.cjw_p0.df , by="label")
dendr_cjw_p01[["labels"]] <- merge(dendr_cjw_p01[["labels"]],clust.cjw_p01.df , by="label")
dendr_cja[["labels"]] <- merge(dendr_cja[["labels"]],clust.cja.df, by="label")
dendr_clv[["labels"]] <- merge(dendr_clv[["labels"]],clust.clv.df, by="label")
dendr_cdl[["labels"]] <- merge(dendr_cdl[["labels"]],clust.cdl.df, by="label")
# plot the dendrogram; note use of color=cluster in geom_text(...)
ggplot() +
geom_segment(data=segment(dendr_ljw), aes(x=x, y=y, xend=xend, yend=yend)) +
geom_text(data=label(dendr_ljw), aes(x, y, label=label, hjust=0, color=cluster),
size=3) +
coord_flip() + scale_y_reverse(expand=c(0.2, 0)) +
theme(axis.line.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank(),
axis.title.y=element_blank(),
panel.background=element_rect(fill="white"),
panel.grid=element_blank())
# plot the dendrogram; note use of color=cluster in geom_text(...)
ggplot() +
geom_segment(data=segment(dendr_lja), aes(x=x, y=y, xend=xend, yend=yend)) +
geom_text(data=label(dendr_lja), aes(x, y, label=label, hjust=0, color=cluster),
size=3) +
coord_flip() + scale_y_reverse(expand=c(0.2, 0)) +
theme(axis.line.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank(),
axis.title.y=element_blank(),
panel.background=element_rect(fill="white"),
panel.grid=element_blank())
ggplot() +
geom_segment(data=segment(dendr_cjw_p01), aes(x=x, y=y, xend=xend, yend=yend)) +
geom_text(data=label(dendr_cjw_p01), aes(x, y, label=label, hjust=0, color=cluster),
size=3) +
coord_flip() + scale_y_reverse(expand=c(0.2, 0)) +
theme(axis.line.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y=element_blank(),
axis.title.y=element_blank(),
panel.background=element_rect(fill="white"),
panel.grid=element_blank())
#KMeans
library("factoextra")
# K-means clustering
cc.res1 <- eclust(creditcards_dist_jw, "kmeans", k = 10, nstart = 25, graph = FALSE)
cc.res2 <- eclust(creditcards_dist_ja, "kmeans", k = 10, nstart = 25, graph = FALSE)
cc.res3 <- eclust(creditcards_dist_lv, "kmeans", k = 10, nstart = 25, graph = FALSE)
cc.res4 <- eclust(creditcards_dist_dl, "kmeans", k = 10, nstart = 25, graph = FALSE)
l.res1 <- eclust(loans_dist_jw, "kmeans", k = 10, nstart = 25, graph = FALSE)
l.res2 <- eclust(loans_dist_ja, "kmeans", k = 10, nstart = 25, graph = FALSE)
l.res3 <- eclust(loans_dist_lv, "kmeans", k = 10, nstart = 25, graph = FALSE)
l.res4 <- eclust(loans_dist_dl, "kmeans", k = 10, nstart = 25, graph = FALSE)
# k-means group number of each observation
fviz_cluster(cc.res1, frame.type = "norm", frame.level = 0.68)
fviz_cluster(cc.res2, frame.type = "norm", frame.level = 0.68)
fviz_cluster(cc.res3, frame.type = "norm", frame.level = 0.68)
fviz_cluster(cc.res4, frame.type = "norm", frame.level = 0.68)
fviz_cluster(l.res1, frame.type = "norm", frame.level = 0.68)
fviz_cluster(l.res2, frame.type = "norm", frame.level = 0.68)
fviz_cluster(l.res3, frame.type = "norm", frame.level = 0.68)
fviz_cluster(l.res4, frame.type = "norm", frame.level = 0.68)
fviz_dend(l.res1, rect = TRUE, show_labels = TRUE, cex = 0.5)
clusplot(as.matrix(creditcards_dist_jw), cckfit1$cluster)