R语言简单实现聚类分析计算与分析(基于系统聚类法)


聚类分析计算与分析(基于系统聚类法)

下面以一个具体的例子来实现实证分析。2008年我国其中31个省、市和自治区的农村居民家庭平均每人全年消费性支出。

根据原始数据对我国省份进行归类统计。

                                                               原始数据如下

注:数据来源—www.stats.gov.cn(中华人民共和国国家统计局)

 

代码如下:

#读入数据
china <- read.table("F:\\2008年我国其中31个省、市和自治区的农村居民家庭平均每人全年消费性支出.txt",header=TRUE)
distance <- dist(china)  #计算距离
china.hc <- hclust(distance) #聚类分析,最长距离法
plot(china.hc, hang = -1) #绘画系谱图 
re <- rect.hclust(china.hc, k = 5) #分为5类

re
for (i in 1:5) {
print(paste("第",i,"类"))
print(china[re[[i]],]$地区)
}

china.hc <- hclust(distance,method="single") #聚类分析,最短距离法
plot(china.hc, hang = -1) #绘画系谱图 
re <- rect.hclust(china.hc, k = 5) #分为5类

china.hc <- hclust(distance,method="average") #聚类分析,类平均法
plot(china.hc, hang = -1) #绘画系谱图 
re <- rect.hclust(china.hc, k = 5) #分为5类

 

 

china.hc <- hclust(distance,method="centroid") #聚类分析,重心法
plot(china.hc, hang = -1) #绘画系谱图 
re <- rect.hclust(china.hc, k = 5) #分为5类

china.hc <- hclust(distance,method="median") #聚类分析,中间距离法
plot(china.hc, hang = -1) #绘画系谱图 
re <- rect.hclust(china.hc, k = 5) #分为5类

china.hc <- hclust(distance,method="ward") #聚类分析,离差平方和法
plot(china.hc, hang = -1) #绘画系谱图 
re <- rect.hclust(china.hc, k = 5) #分为5类

 

 以上实现了不同聚类分析计算两个类距离算法得出的不同结果。

 根据离差平方和法可将我国省份作出以下分类:

第一类:北京 上海 浙江

第二类:江苏 福建 广东

第三类:江西 湖北 湖南 四川

第四类:天津   河北   辽宁   黑龙江 安徽   山东   河南   云南

第五类:山西   内蒙古 吉林   广西   海南   重庆   贵州   西藏   陕西   新疆   甘肃   青海   宁夏 

 

posted @ 2016-07-16 14:23  56颜文生  阅读(41829)  评论(2编辑  收藏  举报