-
Notifications
You must be signed in to change notification settings - Fork 0
/
10. Clustering.R
232 lines (190 loc) · 7.32 KB
/
10. 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
#Open dataset
OO2 <- read.csv("Fixed optho copy 2.csv", header = TRUE)
#Remove ID and PBD group for clustering
OO3 <- OO2[3:12]
#Double check that all binaries were transformed into integers
str(OO3)
#Transform all binary variables into integers
OO3 <- transform(OO3, Nystagmus..TRUE = as.integer(Nystagmus..TRUE))
OO3 <- transform(OO3, Pupils.2..Reactive = as.integer(Pupils.2..Reactive))
#load all packages
library(tidyverse)
library(magrittr)
library(cluster)
library(cluster.datasets)
library(cowplot)
library(NbClust)
library(clValid)
library(ggfortify)
library(clustree)
library(dendextend)
library(factoextra)
library(FactoMineR)
library(corrplot)
library(GGally)
library(ggiraphExtra)
library(knitr)
library(kableExtra)
library(cluster)
library(dplyr)
library(ggplot2)
library(readr)
library(Rtsne)
library(mclust)
library("kohonen")
#gower
gower_dist <- daisy(OO3, metric = "gower")
summary(gower_dist)
gower_mat <- as.matrix(gower_dist)
#Number of clusters: The silhouette figure helps us identify the best option(s)
sil_width <- c(NA)
for(i in 2:16){
pam_fit <- pam(gower_dist, diss = TRUE, k = i)
sil_width[i] <- pam_fit$silinfo$avg.width
}
plot(1:8, sil_width,
xlab = "Number of clusters",
ylab = "Silhouette Width")
lines(1:8, sil_width)
#For silhouette width. Let’s pick k = 2, since it has the highest silhouette width
#Summary of each cluster
k <- 2
pam_fit <- pam(gower_dist, diss = TRUE, k)
pam_results <- OO3 %>%
mutate(cluster = pam_fit$clustering) %>%
group_by(cluster) %>%
do(the_summary = summary(.))
pam_results$the_summary
#visualization in a lower dimensional space
tsne_obj <- Rtsne(gower_dist, is_distance = TRUE, perplexity = 1)
tsne_data <- tsne_obj$Y %>%
data.frame() %>%
setNames(c("X", "Y")) %>%
mutate(cluster = factor(pam_fit$clustering))
ggplot(aes(x = X, y = Y), data = tsne_data) +
geom_point(aes(color = cluster))
#This is to see which observations went to which cluster (in the same order as the dataset)
tsne_data$cluster
#-------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
#Example of my chi-squared and p-value package on only the Cortical.visual.impairement variable
#p-value for clustered data
library(htestClust)
OO3 <- transform(OO3, Cortical.visual.impairement = as.integer(Cortical.visual.impairement))
levenetestClust(OO3$Cortical.visual.impairement ~ tsne_data$cluster, id=OO2$ID)
#Density graph
OO3 <- transform(OO3, Cortical.visual.impairement = as.factor(Cortical.visual.impairement))
cdplot(OO3$Cortical.visual.impairement ~ tsne_data$cluster, data=OO3)
# Bar graph
ggplot(OO3,
aes(x = OO3$Cortical.visual.impairement,
fill = tsne_data$cluster)) +
geom_bar(position = "dodge")
#-------------------------------------------------------------------------------
#p-value for clustered data
OO3 <- transform(OO3, Cataract = as.integer(Cataract))
levenetestClust(OO3$Cataract ~ tsne_data$cluster, id=OO2$ID)
#Density graph
OO3 <- transform(OO3, Cataract = as.factor(Cataract))
cdplot(OO3$Cataract ~ tsne_data$cluster, data=OO3)
# Bar graph
ggplot(OO3,
aes(x = OO3$Cataract,
fill = tsne_data$cluster)) +
geom_bar(position = "dodge")
#-------------------------------------------------------------------------------
#p-value for clustered data
OO3 <- transform(OO3, Nyctalopia = as.integer(Nyctalopia))
levenetestClust(OO3$Nyctalopia ~ tsne_data$cluster, id=OO2$ID)
#Density graph
OO3 <- transform(OO3, Nyctalopia = as.factor(Nyctalopia))
cdplot(OO3$Nyctalopia ~ tsne_data$cluster, data=OO3)
# Bar graph
ggplot(OO3,
aes(x = OO3$Nyctalopia,
fill = tsne_data$cluster)) +
geom_bar(position = "dodge")
#-------------------------------------------------------------------------------
#p-value for clustered data
OO3 <- transform(OO3, Maculopathy = as.integer(Maculopathy))
levenetestClust(OO3$Maculopathy ~ tsne_data$cluster, id=OO2$ID)
#Density graph
OO3 <- transform(OO3, Maculopathy = as.factor(Maculopathy))
cdplot(OO3$Maculopathy ~ tsne_data$cluster, data=OO3)
# Bar graph
ggplot(OO3,
aes(x = OO3$Maculopathy,
fill = tsne_data$cluster)) +
geom_bar(position = "dodge")
#-------------------------------------------------------------------------------
#p-value for clustered data
OO3 <- transform(OO3, Bilateral.hyperopia = as.integer(Bilateral.hyperopia))
levenetestClust(OO3$Bilateral.hyperopia ~ tsne_data$cluster, id=OO2$ID)
#Density graph
OO3 <- transform(OO3, Bilateral.hyperopia = as.factor(Bilateral.hyperopia))
cdplot(OO3$Bilateral.hyperopia ~ tsne_data$cluster, data=OO3)
# Bar graph
ggplot(OO3,
aes(x = OO3$Bilateral.hyperopia,
fill = tsne_data$cluster)) +
geom_bar(position = "dodge")
#-------------------------------------------------------------------------------
#p-value for clustered data
OO3 <- transform(OO3, Retinitis.pigmentosa = as.integer(Retinitis.pigmentosa))
levenetestClust(OO3$Retinitis.pigmentosa ~ tsne_data$cluster, id=OO2$ID)
#Density graph
OO3 <- transform(OO3, Retinitis.pigmentosa = as.factor(Retinitis.pigmentosa))
cdplot(OO3$Retinitis.pigmentosa ~ tsne_data$cluster, data=OO3)
# Bar graph
ggplot(OO3,
aes(x = OO3$Retinitis.pigmentosa,
fill = tsne_data$cluster)) +
geom_bar(position = "dodge")
#-------------------------------------------------------------------------------
#p-value for clustered data
OO3 <- transform(OO3, Blindness = as.integer(Blindness))
levenetestClust(OO3$Blindness ~ tsne_data$cluster, id=OO2$ID)
#Density graph
OO3 <- transform(OO3, Blindness = as.factor(Blindness))
cdplot(OO3$Blindness ~ tsne_data$cluster, data=OO3)
# Bar graph
ggplot(OO3,
aes(x = OO3$Blindness,
fill = tsne_data$cluster)) +
geom_bar(position = "dodge")
#-------------------------------------------------------------------------------
#p-value for clustered data
OO3 <- transform(OO3, Optic.Nerve.Atrophy = as.integer(Optic.Nerve.Atrophy))
levenetestClust(OO3$Optic.Nerve.Atrophy ~ tsne_data$cluster, id=OO2$ID)
#Density graph
OO3 <- transform(OO3, Optic.Nerve.Atrophy = as.factor(Optic.Nerve.Atrophy))
cdplot(OO3$Optic.Nerve.Atrophy ~ tsne_data$cluster, data=OO3)
# Bar graph
ggplot(OO3,
aes(x = OO3$Optic.Nerve.Atrophy,
fill = tsne_data$cluster)) +
geom_bar(position = "dodge")
#-------------------------------------------------------------------------------
#p-value for clustered data
OO3 <- transform(OO3, Nystagmus..TRUE = as.integer(Nystagmus..TRUE))
levenetestClust(OO3$Nystagmus..TRUE ~ tsne_data$cluster, id=OO2$ID)
#Density graph
OO3 <- transform(OO3, Nystagmus..TRUE = as.factor(Nystagmus..TRUE))
cdplot(OO3$Nystagmus..TRUE ~ tsne_data$cluster, data=OO3)
# Bar graph
ggplot(OO3,
aes(x = OO3$Nystagmus..TRUE,
fill = tsne_data$cluster)) +
geom_bar(position = "dodge")
#-------------------------------------------------------------------------------
#p-value for clustered data
OO3 <- transform(OO3, Pupils.2..Reactive = as.integer(Pupils.2..Reactive))
levenetestClust(OO3$Pupils.2..Reactive ~ tsne_data$cluster, id=OO2$ID)
#Density graph
OO3 <- transform(OO3, Pupils.2..Reactive = as.factor(Pupils.2..Reactive))
cdplot(OO3$Pupils.2..Reactive ~ tsne_data$cluster, data=OO3)
# Bar graph
ggplot(OO3,
aes(x = OO3$Pupils.2..Reactive,
fill = tsne_data$cluster)) +
geom_bar(position = "dodge")