匠人府分享 http://blog.sciencenet.cn/u/meiweipingg

博文

用R作“系统(层次)聚类分析”

已有 33617 次阅读 2016-7-5 15:43 |个人分类:R语言|系统分类:科研笔记| cluster, hierarchical, 进行, RStudio, 系统(层次)聚类分析

查看更多>>



#Hierarchical Cluster Analysis in R or Rstudio

#------------第一步:导入数据 ex.-------------------------------------------------

data(dune)

#-----------第二步:将原始转换成“距离”矩阵  #假设数据不需要进行标准化--------

library(vegan)

distance.ex<-vegdist(dune,method="euc",na.rm=TRUE)

#计算距离的method (Dissimilarity index)包括:

#"manhattan", "euclidean", "canberra","bray", "kulczynski", "jaccard", "gower", "altGower", "morisita", "horn","mountford", "raup" , "binomial", "chao", "cao" or "mahalanobis".

#其中"bray"是指 "Bray–Curtis Dissimilarity index"


#-----------第三步:聚类分析--------------------------------------------------------

hclust.ex <- hclust(distance.ex,method="ward.D2")

#聚类的方法包括:

#"ward.D", "ward.D2", "single", "complete", "average" (= UPGMA), "mcquitty"(= WPGMA), "median" (= WPGMC) or "centroid" (= UPGMC).

# 注意一般软件ward算法相对应的hclust中为ward.D2,小心用错

# agnes(*, method="ward") corresponds to hclust(*, "ward.D2").


#-----------第四步:作树状图--------------------------------------------------------

plot(hclust.ex,hang=-1)    # hang取负数时,树状图y轴 从0 开始。


#---------------注意点:------------------------

#(1)聚类方法"centroid" 相对应使用的距离为平方欧式距离 squared Euclidean distances.   如:hc1ust.centroid <- hclust(dist(cent)^2, method = "cen")

#(2)聚类方法"ward.D2" 相对应使用的距离为欧式距离 "Euclidean" distances.

#(3)聚类方法"average"(=UPGMA) 相对应使用的距离为 "bray"(=Bray-Curtis) distances.



#----------- 补充一 ------------------------------

library(vegan)

data(dune)

distance.bray<-vegdist(dune,method="bray",na.rm=TRUE)                 # bray 距离

hclust.bray<- hclust(distance.bray,method="average")      # UPGMA 聚类

plot(hclust.bray,hang=-1)        # 见下图,y轴的 Height 为 “Bray-Curtis不相似性百分比”。




#------------补充二--------------------------------

#{vegan} package 内还包含以下应用程序:ANOSIM,BIO-ENV,metaMDS(=nMDS),CCA,RDA,SIMPER,vegdist等


#------------参考--------------------------------

# 1. vegdist()应用代码 https://cran.r-project.org/web/packages/vegan/vegan.pdf

# 2. hclust()应用代码 https://stat.ethz.ch/R-manual/R-devel/library/stats/html/hclust.html

# 3. 不同聚类方法的比较 https://cran.r-project.org/web/packages/dendextend/vignettes/Cluster_Analysis.html

# 4. 实例 http://ecology.msu.montana.edu/labdsv/R/labs/lab13/lab13.html


拓展阅读>>

(多个连续变量)聚类分析之R语言篇 http://blog.sciencenet.cn/blog-1114360-735780.html


#由于本文作者水平有限,文中如有错误之处,欢迎大家批评指正!



https://wap.sciencenet.cn/blog-651374-988817.html

上一篇:如何作“雷达图(或蜘蛛网图)”之R(或 Rstudio)应用篇
下一篇:科技论文常用英文缩略词释义 eg., aq., ca., et al.
收藏 IP: 133.45.210.*| 热度|

2 陈南晖 梅志平

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

数据加载中...
扫一扫,分享此博文

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

GMT+8, 2024-4-19 10:23

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部