牛海山博客分享 http://blog.sciencenet.cn/u/leymus

博文

用R进行聚类分析时R2的简便计算方法

已有 6599 次阅读 2015-2-20 15:00 |个人分类:R使用|系统分类:科研笔记| R语言, 计算方法, 聚类分析, 决定系数

R在进行聚类分析时,没提供R^2。网上有人提供了自己编制的代码。觉得还应该有更简洁的计算方法,我于是借鉴了kmeans函数的帮助,写了下面三行代码计算R2。使用了scale() 和 by() 两个函数,大大减少了代码量。这两个函数都在base包里,不需要额外加载包。


使用时需要注意一点: 只保留分类变量列,把名字列去掉。


tss.cal<-function(x) sum(scale(x,scale=F)^2)#总平方和
ess.cal<-function(x,clst) sum(by(x,INDICES=clst,FUN=tss.cal))#总组内平方和,或者“误差平方和”。
rsq.cal<-function(x,clst) (tss.cal(x)-ess.cal(x,clst))/tss.cal(x) #R2; clst是聚类向量,即聚类结果

下面提供一个案例:
#--------------------------------------------------------------
#下面读入案例数据集
poverty<-read.table(text="Birth Death InfantDeath Country
                   24.7 5.7 30.8 Albania
                   12.5 11.9 14.4 Bulgaria
                   13.4 11.7 11.3 Czechoslovakia
                   12 12.4 7.6 Former_E._Germany
                   11.6 13.4 14.8 Hungary
                   14.3 10.2 16 Poland
                   13.6 10.7 26.9 Romania
                   14 9 20.2 Yugoslavia
                   17.7 10 23 USSR
                   15.2 9.5 13.1 Byelorussia_SSR
                   13.4 11.6 13 Ukrainian_SSR
                   20.7 8.4 25.7 Argentina
                   46.6 18 111 Bolivia
                   28.6 7.9 63 Brazil
                   23.4 5.8 17.1 Chile
                   27.4 6.1 40 Columbia
                   32.9 7.4 63 Ecuador
                   28.3 7.3 56 Guyana
                   34.8 6.6 42 Paraguay
                   32.9 8.3 109.9 Peru
                   18 9.6 21.9 Uruguay
                   27.5 4.4 23.3 Venezuela
                   29 23.2 43 Mexico
                   12 10.6 7.9 Belgium
                   13.2 10.1 5.8 Finland
                   12.4 11.9 7.5 Denmark
                   13.6 9.4 7.4 France
                   11.4 11.2 7.4 Germany
                   10.1 9.2 11 Greece
                   15.1 9.1 7.5 Ireland
                   9.7 9.1 8.8 Italy
                   13.2 8.6 7.1 Netherlands
                   14.3 10.7 7.8 Norway
                   11.9 9.5 13.1 Portugal
                   10.7 8.2 8.1 Spain
                   14.5 11.1 5.6 Sweden
                   12.5 9.5 7.1 Switzerland
                   13.6 11.5 8.4 U.K.
                   14.9 7.4 8 Austria
                   9.9 6.7 4.5 Japan
                   14.5 7.3 7.2 Canada
                   16.7 8.1 9.1 U.S.A.
                   40.4 18.7 181.6 Afghanistan
                   28.4 3.8 16 Bahrain
                   42.5 11.5 108.1 Iran
                   42.6 7.8 69 Iraq
                   22.3 6.3 9.7 Israel
                   38.9 6.4 44 Jordan
                   26.8 2.2 15.6 Kuwait
                   31.7 8.7 48 Lebanon
                   45.6 7.8 40 Oman
                   42.1 7.6 71 Saudi_Arabia
                   29.2 8.4 76 Turkey
                   22.8 3.8 26 United_Arab_Emirates
                   42.2 15.5 119 Bangladesh
                   41.4 16.6 130 Cambodia
                   21.2 6.7 32 China
                   11.7 4.9 6.1 Hong_Kong
                   30.5 10.2 91 India
                   28.6 9.4 75 Indonesia
                   23.5 18.1 25 Korea
                   31.6 5.6 24 Malaysia
                   36.1 8.8 68 Mongolia
                   39.6 14.8 128 Nepal
                   30.3 8.1 107.7 Pakistan
                   33.2 7.7 45 Philippines
                   17.8 5.2 7.5 Singapore
                   21.3 6.2 19.4 Sri_Lanka
                   22.3 7.7 28 Thailand
                   31.8 9.5 64 Vietnam
                   35.5 8.3 74 Algeria
                   47.2 20.2 137 Angola
                   48.5 11.6 67 Botswana
                   46.1 14.6 73 Congo
                   38.8 9.5 49.4 Egypt
                   48.6 20.7 137 Ethiopia
                   39.4 16.8 103 Gabon
                   47.4 21.4 143 Gambia
                   44.4 13.1 90 Ghana
                   47 11.3 72 Kenya
                   44 9.4 82 Libya
                   48.3 25 130 Malawi
                   35.5 9.8 82 Morocco
                   45 18.5 141 Mozambique
                   44 12.1 135 Namibia
                   48.5 15.6 105 Nigeria
                   48.2 23.4 154 Sierra_Leone
                   50.1 20.2 132 Somalia
                   32.1 9.9 72 South_Africa
                   44.6 15.8 108 Sudan
                   46.8 12.5 118 Swaziland
                   31.1 7.3 52 Tunisia
                   52.2 15.6 103 Uganda
                   50.5 14 106 Tanzania
                   45.6 14.2 83 Zaire
                   51.1 13.7 80 Zambia
                   41.7 10.3 66 Zimbabwe",
                   header=T)

#下面三行是计算平方和和R2的函数
tss.cal<-function(x) sum(scale(x,scale=F)^2)
ess.cal<-function(x,clst) sum(by(x,INDICES=clst,FUN=tss.cal))
rsq.cal<-function(x,clst) (tss.cal(x)-ess.cal(x,clst))/tss.cal(x)

poverty<-poverty[,1:3]#把名称列去掉!
rsq<-vector("numeric",14)#建立存储R2结果的向量,长度是14,看聚类成2到15个类时R2的值
res<-hclust(dist(poverty),method="ward.D")#进行分层聚类。这里只是以分层聚类hclust为例,当然可以换成别的聚类方法。
for(ii in 2:15){
 clst<-cutree(res,ii)#clst是聚类向量,即聚类的结果。
 rsq[ii-1]<-rsq.cal(poverty,clst)#调用刚才建立的函数,计算R2
}

#下面看一下不同聚类数下R2变化
cluster.rslt<-data.frame(nclust=2:15,rsq=rsq)
plot(rsq~nclust,cluster.rslt,type="b")#看来三个世界的说法有聚类分析的支持



https://wap.sciencenet.cn/blog-39344-869157.html

上一篇:超级计算机上用PBS提交R计算任务详解
下一篇:立体农业——农业节水的根本方案是把农业变成工业
收藏 IP: 150.203.54.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-3-29 04:36

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部