library(cluster)
data(ruspini)
?ruspini
k <- kmeans(ruspini, centers = 3)
g1 <- plot(ruspini, col= k$cluster)
points(k$centers, cex=2, col=11, pch=19)
k$totss # inercia total
## [1] 244373.9
k$betweenss # inercia entre los grupos
## [1] 193310.4
k$withinss # inercia dentro de los grupos
## [1] 4558.235 3176.783 43328.457
k$tot.withinss # # inercia total dentro de los grupos
## [1] 51063.48
library(apcluster)
##
## Attaching package: 'apcluster'
## The following object is masked from 'package:stats':
##
## heatmap

d_dist <- dist(as.matrix(ruspini))
clusters <- identity(hclust(d_dist))
clusters
##
## Call:
## hclust(d = d_dist)
##
## Cluster method : complete
## Distance : euclidean
## Number of objects: 75
agrupamiento <- hclust(d_dist)
g2 <- plot(agrupamiento)
rect.hclust(agrupamiento, k=3, border = "red")

g3 <- plot(agrupamiento)
rect.hclust(agrupamiento, k=4, border = "red")

g4 <- plot(agrupamiento)
rect.hclust(agrupamiento, k=5, border = "red")

apclus <- apcluster(negDistMat(r=2), ruspini)
show(apclus)
##
## APResult object
##
## Number of samples = 75
## Number of iterations = 130
## Input preference = -5714
## Sum of similarities = -13169
## Sum of preferences = -22856
## Net similarity = -36025
## Number of clusters = 4
##
## Exemplars:
## 10 32 50 70
## Clusters:
## Cluster 1, exemplar 10:
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
## Cluster 2, exemplar 32:
## 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
## Cluster 3, exemplar 50:
## 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
## Cluster 4, exemplar 70:
## 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
sim1 <- corSimMat(ruspini, method = "pearson")
apclus1 <- apcluster(s=sim1)
show(apclus1)
##
## APResult object
##
## Number of samples = 75
## Number of iterations = 516
## Input preference = 1
## Sum of similarities = 58
## Sum of preferences = 17
## Net similarity = 75
## Number of clusters = 17
##
## Exemplars:
## 6 8 14 19 21 28 31 35 39 44 48 50 54 64 68 71 72
## Clusters:
## Cluster 1, exemplar 6:
## 6 9 17 23 52 56
## Cluster 2, exemplar 8:
## 8 10 53
## Cluster 3, exemplar 14:
## 1 14 25 26 45 55
## Cluster 4, exemplar 19:
## 5 13 19 51
## Cluster 5, exemplar 21:
## 7 11 15 21 29 30 32 47 58
## Cluster 6, exemplar 28:
## 4 22 28 37 38
## Cluster 7, exemplar 31:
## 2 18 31 57
## Cluster 8, exemplar 35:
## 35 43 59
## Cluster 9, exemplar 39:
## 34 39 46
## Cluster 10, exemplar 44:
## 3 16 24 40 44 49
## Cluster 11, exemplar 48:
## 20 27 33 36 48
## Cluster 12, exemplar 50:
## 12 42 50
## Cluster 13, exemplar 54:
## 41 54
## Cluster 14, exemplar 64:
## 60 61 64 66 69 75
## Cluster 15, exemplar 68:
## 62 63 65 68
## Cluster 16, exemplar 71:
## 71 73 74
## Cluster 17, exemplar 72:
## 67 70 72
sim2 <- corSimMat(ruspini, method = "kendall")
apclus2 <- apcluster(s=sim2)
## Warning in .local(s, x, ...): algorithm did not converge; turn on details
## and call plot() to monitor net similarity. Consider
## increasing 'maxits' and 'convits', and, if oscillations occur
## also increasing damping factor 'lam'.
show(apclus2)
##
## APResult object
##
## Number of samples = 75
## Number of iterations = 1000
## Input preference = 1
## Sum of similarities = 57
## Sum of preferences = 18
## Net similarity = 75
## Number of clusters = 18
##
## Exemplars:
## 1 3 6 11 18 19 24 30 34 36 40 47 48 54 61 62 67 70
## Clusters:
## Cluster 1, exemplar 1:
## 1 21 31
## Cluster 2, exemplar 3:
## 2 3 20 39 58
## Cluster 3, exemplar 6:
## 6 13 29 32 51 53
## Cluster 4, exemplar 11:
## 9 11 12 15 41 55
## Cluster 5, exemplar 18:
## 5 10 16 18
## Cluster 6, exemplar 19:
## 4 14 19 56 59
## Cluster 7, exemplar 24:
## 24 28 52
## Cluster 8, exemplar 30:
## 30 33 37 38 45 49
## Cluster 9, exemplar 34:
## 7 25 26 34 35
## Cluster 10, exemplar 36:
## 8 17 22 36
## Cluster 11, exemplar 40:
## 23 40 42
## Cluster 12, exemplar 47:
## 27 44 46 47 50 57
## Cluster 13, exemplar 48:
## 43 48
## Cluster 14, exemplar 54:
## 54
## Cluster 15, exemplar 61:
## 60 61 75
## Cluster 16, exemplar 62:
## 62 72 74
## Cluster 17, exemplar 67:
## 64 65 67 69
## Cluster 18, exemplar 70:
## 63 66 68 70 71 73
sim3 <- corSimMat(ruspini, method = "spearman")
apclus3 <- apcluster(s=sim3)
show(apclus3)
##
## APResult object
##
## Number of samples = 75
## Number of iterations = 941
## Input preference = 1
## Sum of similarities = 58
## Sum of preferences = 17
## Net similarity = 75
## Number of clusters = 17
##
## Exemplars:
## 10 12 18 28 33 35 36 37 39 43 52 56 57 64 66 70 72
## Clusters:
## Cluster 1, exemplar 10:
## 7 10 19 21 24 38 46 49 53
## Cluster 2, exemplar 12:
## 6 11 12 27 34
## Cluster 3, exemplar 18:
## 8 18 25 30
## Cluster 4, exemplar 28:
## 17 20 26 28 48 50
## Cluster 5, exemplar 33:
## 13 33 41
## Cluster 6, exemplar 35:
## 22 35 59
## Cluster 7, exemplar 36:
## 2 5 36 51 58
## Cluster 8, exemplar 37:
## 31 37 54
## Cluster 9, exemplar 39:
## 1 3 39 40 47
## Cluster 10, exemplar 43:
## 14 15 32 43
## Cluster 11, exemplar 52:
## 44 52 55
## Cluster 12, exemplar 56:
## 4 23 42 56
## Cluster 13, exemplar 57:
## 9 16 29 45 57
## Cluster 14, exemplar 64:
## 61 64 67 68 69 75
## Cluster 15, exemplar 66:
## 65 66
## Cluster 16, exemplar 70:
## 60 63 70 71
## Cluster 17, exemplar 72:
## 62 72 73 74
grupos1 <- kmeans(d_dist, 3)
grupos2 <- kmeans(d_dist, 4)
grupos3 <- kmeans(d_dist, 5)
grupos1$size
## [1] 17 23 35
grupos2$size
## [1] 20 7 40 8
grupos3$size
## [1] 17 5 10 8 35
library(fpc)
g5 <- plotcluster(ruspini, grupos1$cluster)

g6 <- plotcluster(ruspini, grupos2$cluster)

g7 <- plotcluster(ruspini, grupos3$cluster)

variable1 <- grupos1$cluster
variable2 <- grupos2$cluster
variable3 <- grupos3$cluster
vdata <- data.frame(variable1, variable2, variable3)
data <- data.frame(ruspini, vdata)
names(data)
## [1] "x" "y" "variable1" "variable2" "variable3"
g8 <- clusplot(data, grupos1$cluster, color = T, shade = T)

g9 <- clusplot(data, grupos2$cluster, color = T, shade = T)

g10 <- clusplot(data, grupos3$cluster, color = T, shade = T)
