-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster validation.Rmd
64 lines (46 loc) · 1.31 KB
/
cluster validation.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
---
title: "R Notebook"
output:
html_document: default
html_notebook: default
pdf_document: default
---
Cluster Validation
```{r}
library(factoextra)
library(clustertend)
library(seriation)
data("faithful")
df3 <- faithful
ggplot(df3, aes(x=eruptions,y=waiting)) + geom_point() + geom_density_2d()
```
```{r}
#Random dataset
set.seed(123)
n <- nrow(df3)
random_df <- data.frame(
x = runif(nrow(df3), min(df3$eruptions), max(df3$eruptions)),
y = runif(nrow(df3), min(df3$waiting), max(df3$waiting)))
ggplot(random_df, aes(x,y)) + geom_point()
km.res1 <- kmeans(df3,2)
fviz_cluster(list(data = df3, cluster = km.res1$cluster),
frame.type = "norm", geom = "point", stand = FALSE)
km.res2 <- kmeans(random_df,2)
fviz_cluster(list(data = random_df, cluster = km.res2$cluster),
frame.type = "norm", geom = "point", stand = FALSE)
fviz_dend(hclust(dist(random_df)), k = 2, cex = 0.5)
```
```{r}
set.seed(123)
hopkins(faithful, n = nrow(faithful)-1)
hopkins(random_df, n = nrow(random_df)-1)
df_scaled <- scale(faithful)
df_dist <- dist(df_scaled)
dissplot(df_dist)
random_df_scaled <- scale(random_df)
random_df_dist <- dist(random_df_scaled)
dissplot(random_df_dist)
set.seed(123)
km.res <- kmeans(scale(faithful), 2)
dissplot(df_dist, labels = km.res$cluster)
```