#-- 第12章 クラスター分析 --#

# データの読み込み
data<- read.csv("12_cluster.csv", header=TRUE, fileEncoding="CP932")
head(data)

# データの整理
ndat<- data[-1]
rownames(ndat)<- data[,1]
colnames(ndat)

# データ標準化とユークリッド距離の算出
ndat.s<- scale(ndat)
ndat.d<- dist(ndat.s)

# 階層的クラスター分析
res<- hclust(ndat.d, method="ward.D")
plot(res, cex=0.7, hang=-1)

# 3つのクラスターを作成
cluster<- cutree(res, k=3)
for(i in 1:3){
	print(cluster[cluster==i])
}
aggregate(ndat.s, list(group=cluster), mean)


