-
Notifications
You must be signed in to change notification settings - Fork 0
/
BC_TumorAnalysis_Clustering.Rmd
217 lines (186 loc) · 6.47 KB
/
BC_TumorAnalysis_Clustering.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
216
---
title: "Tumor Analysis- Cluster Analysis"
description: "Develop accurate models for tumor diagnosis prediction using comprehensive analysis of Wisconsin Breast Cancer Diagnostic (WBCD) dataset including feature selection, classification models, visualization, outlier identification, imbalanced data management, performance evaluation, and generalization approaches. Increased model accuracy and precision with PCA for dimensionality reduction and K-means clustering for feature extraction"
author: "[email protected]"
date: "2023-04-07"
output: html_document
---
```{r}
library(readr)
library(MVA)
library(HSAUR2)
library(SciViews)
library(scatterplot3d)
library(car)
library(lattice)
library(GGally)
library(ggplot2)
library(ggridges)
library(ggvis)
library(ggthemes)
library(cowplot)
library(gapminder)
library(gganimate)
library(dplyr)
library(tidyverse)
library(grid)
library(gridExtra)
library(RColorBrewer)
library(Hotelling)
library(stats)
library(biotools)
library(factoextra)
library(FactoMineR)
library(ggfortify)
library(psych)
library(corrplot)
library(MASS)
library(ggplot2)
library(memisc)
library(ROCR)
library(dplyr)
library(klaR)
library(reshape2)
library(corrplot)
library(MASS)
library(ggplot2)
library(memisc)
library(ROCR)
library(dplyr)
library(klaR)
library(plyr)
# Load the WBCD dataset from the UCI repository
wbdc <- read.csv(url(
"https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data"),
header = FALSE,
col.names = c("ID", "diagnosis", "radius_mean",
"texture_mean", "perimeter_mean",
"area_mean", "smoothness_mean",
"compactness_mean", "concavity_mean",
"concave_pts_mean", "symmetry_mean",
"fractal_dim_mean", "radius_se",
"texture_se", "perimeter_se", "area_se",
"smoothness_se", "compactness_se",
"concavity_se", "concave_pts_se",
"symmetry_se", "fractal_dim_se",
"radius_worst", "texture_worst",
"perimeter_worst", "area_worst",
"smoothness_worst", "compactness_worst",
"concavity_worst", "concave_pts_worst",
"symmetry_worst", "fractal_dim_worst"))
dim(wbdc)
diagnosis <- factor(wbdc$diagnosis)
str(wbdc)
# Data Preprocessing
rownames(wbdc)=wbdc$id
wbdc=wbdc[,-1]
wbdc=wbdc[,-1]
#fix(wbdc)
#inspection of missing values in the dataset
sum(is.na(wbdc)) # number of records with N/A values
set.seed(123)
#any(is.na(wbdc)) # Check if there are any missing values in the data
#any(!is.finite(wbdc)) # Check if there are any infinite values in the data
#wbdc_mat <- as.matrix(wbdc)
k_means_fit <- kmeans(wbdc, 2,nstart = 100) # k = 2
print(k_means_fit)
table(k_means_fit$cluster)
#cluster mean
aggregate(wbdc, by=list(cluster=k_means_fit$cluster), mean)
#visualisation
library(factoextra)
wbdc_num <- wbdc[,0:ncol(wbdc)]
# Convert to matrix
wbdc_mat <- as.matrix(wbdc_num)
# Cluster the data
k_means_fit <- kmeans(wbdc_mat, centers = 2, nstart = 25)
# Visualize the clusters
library(factoextra)
library(ggplot2)
fviz_cluster(k_means_fit, data = wbdc_mat,
geom = "point",
palette = c("#2E9FDF", "#E7B800"),
ellipse.type = "euclid",
star.plot = TRUE,
repel = FALSE,
ggtheme = theme_minimal()
)
# how many clusters for K-means
wssplot <- function(wbdc_mat, nc=10, seed=1234){
wss <- (nrow(wbdc_mat)-1)*sum(apply(wbdc_mat,2,var))
for (i in 2:nc){
set.seed(seed)
wss[i] <- sum(kmeans(wbdc_mat, centers=i)$withinss)}
plot(1:nc, wss, type="b", xlab="Number of Clusters",
ylab="Within groups sum of squares")}
wssplot(wbdc_mat, nc=10)
library(factoextra)
fviz_nbclust(wbdc_mat, kmeans, method = "wss") +
geom_vline(xintercept = 2, linetype = 2)+
labs(subtitle = "Elbow method")
fviz_cluster(kmeans(wbdc_mat, centers = 2), geom = "point", data = wbdc_mat)
# Elbow method
fviz_nbclust(wbdc, kmeans, method ="wss") +
geom_vline(xintercept = 2, linetype = 2)+
labs(subtitle = "Elbow method")
# Silhouette method
fviz_nbclust(wbdc, kmeans, method = "silhouette")+
labs(subtitle = "Silhouette method")
#############################################
# hierarchical clustering
#As part of the preparation for hierarchical clustering, the distance between all pairs
# of observations are computed. Furthermore, there are different ways to link clusters
#together, with single, complete, and average being the most common linkage methods.
# Perform PCA
pca <- prcomp(wbdc, scale = TRUE)
# Extract scores
scores <- pca$x
# Compute Euclidean distance matrix
d <- dist(scores, method = "euclidean")
d_matrix <- as.matrix(d)
#d_matrix
#agglomerative clustering
d <- dist(scores, method = "euclidean") # Euclidean distance matrix.
d_matrix=as.matrix(d)
#d_matrix
#complete linkage
H.fit <- hclust(d, method="complete")
fviz_dend(H.fit, cex = 0.5)
# Average linkage
H.fit.avg=hclust(d, method="average")
fviz_dend(H.fit.avg)
# Ward linkage
H.fit.ward=hclust(d, method="ward.D2")
fviz_dend(H.fit.ward)
# Cut tree into 2/4 groups
grp <- cutree(H.fit.ward, k = 2)
head(grp, n = 2)
# Number of members in each cluster
table(grp,diagnosis)
table(k_means_fit$cluster, grp)
fviz_dend(H.fit.ward, k = 2,
cex = 0.5,
k_colors = c("#2E9FDF", "yellow","red","pink"),
color_labels_by_k = TRUE,
rect = TRUE
)
fviz_cluster(list(data = wbdc, cluster = grp),
palette = c("#2E9FDF", "#E7B800", "#FC4E07"),
ellipse.type = "convex",
repel = FALSE,
show.clust.cent = FALSE, ggtheme = theme_minimal())
# # ##########################################################
# ## validation
#
# km2 <- data.frame(wbdc, cluster = k_means_fit$cluster, diagnosis = wbdc$diagnosis)
# km2$diagnosis <- as.factor(mapvalues(km2$diagnosis, from=c("B", "M"), to=c("2", "1")))
# mean(km2$cluster == km2$diagnosis)
# # Output : 0.8541301
# hc2 <- data.frame(wbdc, grp, diagnosis = wbdc$diagnosis)
# hc2$diagnosis <- as.factor(mapvalues(hc2$diagnosis,
# from = c("B", "M"),
# to = c("2","1")))
#
# mean(hc2$grp == hc2$diagnosis)
# #Output: 0.8804921
```