data <- a0926[, -1]
# 去除第一欄編號
# 建立一個分群模型
##分4群,nstart=10 defaut執行10次 收斂資料區
km <- kmeans(data, centers = 4, nstart = 10)
kmeans.cluster <- kmeans(data, centers=4)
# Elbow Method 應用在 K-Means #資料太多...
fviz_nbclust(data,
FUNcluster = kmeans,# K-Means
method = "wss", # total within sum of square
k.max = 6 # max number of clusters to consider) +
labs(title="Elbow Method for K-Means")
# Determine number of clusters # it's work!!!!!!!!!
wss <- (nrow(data)-1)*sum(apply(data,2,var))
for (i in 2:15) wss[i] <- sum(kmeans(data,
centers=i)$withinss)
plot(1:15, wss, type="b", xlab="Number of Clusters",
ylab="Within groups sum of squares")
# 視覺化 k-means 分群結果(基於ggplot2的語法)
require(factoextra)
fviz_cluster(kmeans.cluster, # 分群結果
data = data, # 資料
geom = c("point"), # 點和標籤(point & label)
frame.type = "norm") # 框架型態
xlab = NULL,
ylab = NULL,
#跑分群之後畫分佈 #失敗
plot(formula = belonging ~ achieve, data = data, col = kmeans$cluster, main = "將歸屬感做分群", xlab = "學業成就", ylab = "歸屬感")
# 視覺化 k-means 分群結果(基於ggplot2的語法)
require(factoextra)
fviz_cluster(kmeans.cluster, # 分群結果
data = data, # 資料
geom = c("point"), # 點和標籤(point & label)
frame.type = "norm") # 框架型態
# Elbow Method 應用在 K-Means #資料太多...
fviz_nbclust(data,
FUNcluster = kmeans,# K-Means
method = "wss", # total within sum of square
k.max = 6 # max number of clusters to consider
) +
labs(title="Elbow Method for K-Means") +
geom_vline(xintercept = 3, # 在 X=3的地方
linetype = 2) # 畫一條垂直虛線
fit <- kmeans(data, 4)
library(cluster)
clusplot(data, fit$cluster, color=TRUE, shade=TRUE,
labels=2, lines=0)
fviz_cluster(kmeans.cluster, # 分群結果
data = data, # 資料
xlab = "data$belonging", ylab = "data$achieve",
geom = c("point"), # 點和標籤
frame.type = "norm") # 框架型態