-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClustering_Shape7-Data.R
95 lines (67 loc) · 2.52 KB
/
Clustering_Shape7-Data.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
library(tidyverse)
shape7_data <- read.delim("shape7.txt")
shape7_data
#Data cleaning
library(ggplot2)
ggplot(shape7_data, aes(x = X0.85, y = X17.45)) + geom_point()
summary(shape7_data)
scale_numeric <- function(x) x %>% mutate_if(is.numeric, function(y) as.vector(scale(y)))
shape7_scaled <- shape7_data %>% scale_numeric()
summary(shape7_scaled)
#CLUSTERING
# 1. k-means clustering
tic()
km <- kmeans(shape7_scaled, centers = 5, nstart = 10)
km
shape7_clustered <- shape7_scaled %>% add_column(cluster = factor(km$cluster))
shape7_clustered
ggplot(shape7_clustered, aes(x = X0.85, y = X17.45, color = cluster)) + geom_point()
centroids <- as_tibble(km$centers, rownames = "cluster")
centroids
ggplot(shape7_clustered, aes(x=X0.85, y = X17.45, color = cluster)) + geom_point() +
geom_point(data = centroids, aes(x=X0.45, y = X17.45, color = cluster), shape = 3, size = 10)
#factoextra package for visualization
library(factoextra)
fviz_cluster(km, data = shape7_scaled, centroids = TRUE, repel = TRUE, ellipse.type = "norm")
toc()
#extracting single cluster
cluster2 <- shape7_clustered %>% filter(cluster == 2)
cluster2
summary(cluster2)
ggplot(cluster2, aes(x = X0.85, y = X17.45)) + geom_point() +
coord_cartesian(xlim = c(-2, 2), ylim = c(-2, 2))
#cluster with 8 centers
fviz_cluster(kmeans(shape7_scaled, centers = 8), data = shape7_scaled,
centroids = TRUE, geom = "point", ellipse.type = "norm")
# 2. Hierarchical clustering
tic()
d <- dist(shape7_scaled)
hc <- hclust(d, method = "complete")
plot(hc)
fviz_dend(hc, k = 6) #k is number of clusters to visualize in dendogram
toc()
# 3. Density-Based Spatial Clustering of Applications with Noise
library(dbscan)
kNNdistplot(shape7_scaled, k = 4)
tic()
db <- dbscan(shape7_scaled, eps = .32, minPts = 7)
db
str(db)
ggplot(shape7_scaled %>% add_column(cluster = factor(db$cluster)),
aes(x = X0.85, y = X17.45, color = cluster)) + geom_point()
fviz_cluster(db, shape7_scaled, geom = "point")
toc()
#Partitioning Around Medoids (PAM) --> to solve k-medoids problem
library(cluster)
tic()
d <- dist(shape7_scaled)
str(d)
p <- pam(d, k = 5)
p
shape7_clustered <- shape7_scaled %>% add_column(cluster = factor(p$cluster))
medoids <- as_tibble(shape7_scaled[p$medoids, ], rownames = "cluster")
medoids
ggplot(shape7_clustered, aes(x = X0.85, y = X17.45, color = cluster)) + geom_point() +
geom_point(data = medoids, aes(x = X0.85, y = X17.45, color = cluster), shape = 3, size = 7)
fviz_cluster(c(p, list(data = shape7_scaled)), geom = "point", ellipse.type = "norm")
toc()