#군집분석 중 계층적 군집분석
x<-c(1,2,2,4,5)
y<-c(1,1,4,3,4)
xy<-data.frame(x,y)
plot(xy,pch=20, xlab="x값",ylab="y값",xlim=c(0,6),ylim=c(0,6),main='응집형 군집화')
#abbreviate(x)
text(xy[,1],xy[,2],labels = abbreviate(row.names(xy)),cex = 0.8,pos = 1, col = "blue") # abbreviate()문자열 관련함수
abline(v=c(3),col="gray",lty=2) #가운데 새로 선그어짐
abline(h=c(3),col="gray",lty=2) #수평선 그어짐
dist(xy,method = "euclidean") ^2 #거리값이 나옴
hclust(dist(xy) ^2)
#최단연결법: 거리행렬에서 거리가 가까운 데이터들을 묶어 군집화
hc_sl <- hclust(dist(xy)^2, method="single")
hc_sl
par(mfrow=c(1,2))
plot(hc_sl)
plot(hc_sl, hang=-1)
#최장연결법: 거리행렬에서 거리가 먼 데이터들을 묶어 군집화
hc_cl <- hclust(dist(xy)^2, method="complete")
hc_cl
plot(hc_cl)
plot(hc_cl, hang=-1)
#평균연결법: 최단연결법과 같으나 거리를 구할 때 평균을 사용함
hc_av <- hclust(dist(xy)^2, method="average")
hc_av
plot(hc_av)
plot(hc_av, hang=-1)
#와드연결법: 군집 내 편차 들의 제곱합을 고려한 방법으로, 군집 간에 손실을 최소화 할 수 있다.
hc_wa <- hclust(dist(xy)^2, method="ward.D2")
hc_wa
plot(hc_wa)
plot(hc_wa, hang=-1)
댓글 영역