library(cluster)
library(factoextra)
data("mtcars")
df <- scale(mtcars) #數(shù)據(jù)標(biāo)準(zhǔn)化
km.dist <- get_dist(df, method = 'euclidean') #計算數(shù)據(jù)的euclidean距離矩陣
fviz_dist(km.dist) #可視化距離矩陣
均值聚類
fviz_nbclust(df, FUNcluster = kmeans, method = 'wss') +
geom_vline(xintercept = 5, linetype = 3, color = 'red') #選擇最優(yōu)聚類數(shù)目
set.seed(123)
km.res <- kmeans(df, centers = 5, nstart = 25)#計算聚類結(jié)果季稳,k = 5
table(km.res$cluster)
1 2 3 4 5
4 12 7 2 7
#計算每個聚類的原數(shù)據(jù)變量的均值
aggregate(mtcars, by = list(cluster = km.res$cluster), mean)
cluster mpg cyl disp hp drat wt qsec vs am gear carb
1 21.92500 5.500000 146.3250 121.50000 3.962500 2.601250 16.42000 0 1 4.500000 4.000000
2 15.05000 8.000000 357.6167 194.16667 3.120833 4.104083 17.14250 0 0 3.000000 3.083333
3 20.74286 5.142857 175.1143 102.14286 3.570000 3.194286 19.96714 1 0 3.571429 2.142857
4 15.40000 8.000000 326.0000 299.50000 3.880000 3.370000 14.55000 0 1 5.000000 6.000000
5 28.37143 4.000000 89.8000 80.57143 4.148571 2.028286 18.70000 1 1 4.142857 1.428571
#可視化聚類
km.plot <- fviz_cluster(km.res, data = df,
repel = T,
ggtheme = theme_minimal())
km.plot
層次聚類
hc.dist <- get_dist(df, method = 'euclidean')
hc.res <- hclust(dist(df), method = 'ward.D2')
fviz_dend(hc.res, cex = 0.45) #原始圖形
hc.coph <- cophenetic(hc.res)
cor(hc.dist, hc.coph) #計算一致性系數(shù),大于0.75表示一致性較好
[1] 0.7575063
#用cutree(, k = 5)分成五個聚類,自行決定分類數(shù)
grp <- cutree(hc.res, k = 5)
#計算每個聚類里的頻數(shù)
table(grp)
## grp
## 1 2 3 4 5
## 3 8 7 12 2
#計算每個聚類的原數(shù)據(jù)變量的均值
aggregate(mtcars, by = list(cluster = grp), mean)
cluster mpg cyl disp hp drat wt qsec vs am gear carb
1 20.56667 6.000000 155.0000 131.6667 3.806667 2.755000 16.32667 0.000 1 4.333333 4.666667
2 28.07500 4.000000 93.6125 81.8750 4.183750 2.042250 18.45000 0.875 1 4.250000 1.500000
3 20.74286 5.142857 175.1143 102.1429 3.570000 3.194286 19.96714 1.000 0 3.571429 2.142857
4 15.05000 8.000000 357.6167 194.1667 3.120833 4.104083 17.14250 0.000 0 3.000000 3.083333
5 15.40000 8.000000 326.0000 299.5000 3.880000 3.370000 14.55000 0.000 1 5.000000 6.000000
hc.plot <- fviz_dend(hc.res, k = 5,
cex = 0.45,
rect = T,
rect_fill = T,
rect_border = 'jco') #分割圖形
hc.plot
fviz_dend(hc.res, k = 5,
k_colors = 'jco',
type = 'phylogenic',
repel = T)
fviz_dend(hc.res, k = 5,
cex = 0.5,
k_colors = 'jco',
rect = T,
type = 'circular')
fviz_cluster(list(data = df, cluster = grp),
repel = T,
ellipse.type = 'convex',
palette = 'Set1',
ggtheme = theme_minimal())