R: 距离,聚类,建树

R有自己的计算样本距离(dist)和聚类(hclust)的方法/函数,各自包含多种算法据数据特征选择。另外在生态学vegan包vegdist函数也可计算距离,包含多种生态常用距离算法。hclust聚类后的对象可直接plot出图。ape包as.phylo结合write.tree可将聚类文件写成nwk文件。

1 输入数据

library(tidyverse)  # data manipulation
library(cluster)    # clustering algorithms
library(factoextra) # clustering visualization
library(dendextend) # for comparing two dendrograms

set.seed(1995)  
# 随机种子
data=matrix(abs(round(rnorm(200, mean=0.5, sd=0.25), 2)), 20, 10)  
# 随机正整数,20行,20列
colnames(data)=paste("Species", 1:10, sep=".")  
# 列名-细菌
rownames(data)=paste("Sample", 1:20, sep=".")

注意:一行为一个样本

2 dist hclust plot

2.1 dist 计算样本距离

R dist 文档:
https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/dist
可选算法:
"euclidean", "maximum", "manhattan", "canberra", "binary" or "minkowski"

dist = dist(data, method = "euclidean")

2.2 hclust 聚类样本

R hclust 文档:
https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/hclust
可选算法:
"ward.D", "ward.D2", "single", "complete", "average" (= UPGMA), "mcquitty" (= WPGMA), "median" (= WPGMC) or "centroid" (= UPGMC).

hclust_dist = hclust(dist, method = "complete")

3.3 plot 聚类结果

plot(hclust_dist)
plot(hclust_dist, hang = -1)
rect.hclust(hclust_dist, k = 4, border = 1:4)

hang 末端对齐
k 切割
border 颜色

3.4 分组散点图:fviz_cluster

library("factoextra")
sub = cutree(hclust_dist, k=4)
fviz_cluster(list(data = data, cluster = sub))

3 vegdist hclust plot

library("vegan")
vegdist = vegdist(data, method = "bray")
hclust_vegdist = hclust(vegdist, method = "complete")
plot(hclust_vegdist)

4 hcut - 切树法一

https://rpkgs.datanovia.com/factoextra/reference/hcut.html

计算距离,聚类,切割一步完成

res <- hcut(data, k = 4, stand = TRUE,
            hc_metric = "euclidean",
            hc_func = "hclust",
            hc_method = "complete")

stand: 是否进行数据标准化 scale()
k: 生成多少个cluster
hc_metric: 距离算法
hc_func:聚类函数
hc_method:距离算法

结果

fviz_dend(res, rect = TRUE)

聚类评估算法-轮廓系数:Silhouette Coefficient
si接近1,则说明样本i聚类合理;
si接近-1,则说明样本i更应该分类到另外的簇;
si 近似为0,则说明样本i在两个簇的边界上。

fviz_silhouette(res)
fviz_cluster(res)

5 cut - 切树法二

https://stackoverflow.com/questions/41992119/cut-a-dendrogram

dend <- as.dendrogram(hclust(dist(data)))
depth.cutoff <- 1

plot(dend, horiz =  F)
abline(h=depth.cutoff, col="red", lty=2)

horiz: 水平树,或者垂直树
h: horizon 线
v: vertical 垂直线

cut(dend, h = depth.cutoff)
summary(cut(dend, h = depth.cutoff))
attr(cut(dend, h = depth.cutoff)$lower[[1]], "members")
attr(cut(dend, h = depth.cutoff)$lower[[2]], "members")

upper是cutoff上的cluster, lower是cutoff下的cluster.

打印cutoff以上的cluster (branch)

plot(cut(dend, h = depth.cutoff)$upper, horiz = F)

6 cutree - 切树法三

6.1 建树

vegdist = vegdist(data, method = "euclidean")  # distance
hclust_vegdist = hclust(vegdist, method = "complete")  # clust
plot(hclust_vegdist)  # 观察,确定cutoff,或者k
depth.cutoff = 1.2
plot(hclust_vegdist)
abline(h=depth.cutoff, col="red", lty=2)

6.1 切树

cutree(hclust_vegdist, h = depth.cutoff)

6.3 结果整理

clusters = data.frame(cutree(hclust_vegdist, h = depth.cutoff))
colnames(clusters) = c("clusters")
clusters$samples = rownames(clusters)
clusters = data.frame(clusters[order(clusters$clusters, decreasing=F),])

6.4 核实对应关系

6.5 hub样本

fviz_cluster展示聚类后的样本分布

sub = cutree(hclust_dist, k=5)
fviz_cluster(list(data = data, cluster = sub))

获取其中一个cluster的hub sample

# 1 距离矩阵转成表格
df_dist = data.frame(as.matrix(vegdist))
# 2 整理 cluster1数据
target = clusters[clusters$clusters=="1",]$samples
target_dist = df_dist[c(target), c(target)] 
dist_sum = apply(target_dist, 1, FUN=sum)
target_dist_sum = data.frame(samples = rownames(target_dist),
                             dist_sum = dist_sum)
target_dist_sum_sort = target_dist_sum[order(target_dist_sum$dist_sum, decreasing=F),]

7 比较树,比较距离或聚类算法

library(dendextend) # for comparing two dendrograms

hc1 <- hclust(vegdist(data, method = "bray"), method = "complete")
hc2 <- hclust(vegdist(data, method = "bray"), method = "average")

tanglegram(as.dendrogram(hc1), as.dendrogram(hc2))
dend1 = as.dendrogram(hc1)
dend2 = as.dendrogram(hc2)

dend_list <- dendlist(dend1, dend2)

tanglegram(dend1, dend2,
  highlight_distinct_edges = FALSE, # Turn-off dashed lines
  common_subtrees_color_lines = FALSE, # Turn-off line colors
  common_subtrees_color_branches = TRUE, # Color common branches 
  main = paste("entanglement =", round(entanglement(dend_list), 2))
  )

8 选择合适的聚类数

Elbow Method

fviz_nbclust(data, FUN = hcut, method = "wss")


Average Silhouette Method

fviz_nbclust(data, FUN = hcut, method = "silhouette")


Gap Statistic Method

library(cluster)
gap_stat <- clusGap(data, FUN = hcut, nstart = 25, K.max = 10, B = 50)
fviz_gap_stat(gap_stat)

9 kmeans聚类

kmeans: K-Means Clustering

set.seed(123)
# Data preparation
data("iris")
head(iris)
# Remove species column (5) and scale the data
iris.scaled <- scale(iris[, -5])

# K-means clustering
km.res <- kmeans(iris.scaled, 3, nstart = 10)

# Visualize kmeans clustering
# use repel = TRUE to avoid overplotting
fviz_cluster(km.res, iris[, -5], ellipse.type = "norm")
# Change the color palette and theme
fviz_cluster(km.res, iris[, -5],
   palette = "Set2", ggtheme = theme_minimal())

tree转nwk,plot树形

library(ape)
tree = as.phylo(hclust_vegdist)
plot(tree, type="fan")

可选树形:
“phylogram”, “cladogram”, “fan”, “unrooted”, “radial”

write.tree为nwk

write.tree(phy=tree, file="tree.nwk") 
# 保存为nwk

更多:
R语言:UPGMA聚类分析和树状图
R:层次聚类分析-dist、hclust、heatmap等
R语言鸢尾花iris数据集的层次聚类分析
cmdscale: Classical (Metric) Multidimensional Scaling
cmdscale 经典度量多为测量,也称PCOA,主坐标分析
Cluster with distance threshold in R
Hierarchical Clustering in R: Step-by-Step Example
Hierarchical Clustering in R
Hierarchical Cluster Analysis
推荐:树比较,最佳cluster数计算

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,898评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,401评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,058评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,539评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,382评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,319评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,706评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,370评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,664评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,715评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,476评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,326评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,730评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,003评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,275评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,683评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,877评论 2 335

推荐阅读更多精彩内容

  • 一、层次聚类 1)距离和相似系数 r语言中使用dist(x, method = "euclidean",diag ...
    姚的日志阅读 2,718评论 0 4
  • 本笔记内容:最近工作中遇到的分析需求:按照要求的分组画boxplot和PcoA的散点图。对画各种图的实现方法,一些...
    GPZ_Lab阅读 14,303评论 0 18
  • 以下内容为阅读医学统计学笔记与R语言[https://bookdown.org/wxhyihuan/Noteboo...
    ks_c阅读 1,115评论 0 5
  • 工欲善其事,必先利其器。总结一下,方便多了。R语言还是很牛逼的,可以干很多事情。有一把顺手的刀还是很重要的。 0....
    Liam_ml阅读 4,585评论 1 60
  • 俗话说,“独学而无友,则孤陋而寡闻”。 为了方便大家交流学习,共同进步,我特地创建了☞生信交流群[https://...
    生信交流平台阅读 1,788评论 0 3