군집화

From Biocourse

Jump to: navigation, search

  hc <- hclust(dist(USArrests), "ave") ## USArrests자료를 계층적 군집화 방법중 average방법으로 clustering

plot(hc)  ## clustering 결과를 기본값

plot(hc, hang = -1)  ## label을 동일하게 위치시킴

## Do the same with centroid clustering and squared Euclidean distance,
## cut the tree into ten clusters and reconstruct the upper part of the
## tree from the cluster centers.

hc <- hclust(dist(USArrests)^2, "cen") 
## USArrests자료를 거리의 제곱으로 계층적 군집화 방법중 centroid방법으로 clustering

memb <- cutree(hc, k = 10)  ## 가지의 subset이 10개의 위치
cent <- NULL
for(k in 1:10){
       cent <- rbind(cent, colMeans(USArrests[memb == k, , drop = FALSE]))
}
hc1 <- hclust(dist(cent)^2, method = "cen", members = table(memb)) ## 위에서subset의10개를 이용한 clustering

opar <- par(mfrow = c(1, 2)) ## plot을 그리기 위해서 partition을 1*2 행으로 나눔

plot(hc,  labels = FALSE, hang = -1, main = "Original Tree")  ## hc결과를 그림

plot(hc1, labels = FALSE, hang = -1, main = "Re-start from 10 clusters")  ## hc1결과를 그림