【问题标题】:How to calculate the cophenetic similarity between two individual in two dendograms or between two clustering methods?如何计算两个树状图中两个个体之间或两种聚类方法之间的共同相似性?
【发布时间】:2018-08-31 19:34:12
【问题描述】:

如何计算个人在两棵树内(而不是两棵整棵树之间)的共同距离?

我想计算两个树状图中每个个体的位置相似性/相异性,并使用 R 包 dendextend 和 heatmaply 以组合热图和树状图的行颜色显示结果。

【问题讨论】:

  • cophenetic 是树状图(树)中两个项目(叶子)之间的距离。您可以使用 cophenetic 函数查看树状图的距离矩阵。那是你要找的吗? (如果没有,我怀疑您的问题可能需要进一步澄清,一个简单的例子也会有所帮助:))
  • 谢谢,基本上这就是我需要的。我只能找到比较两棵整棵树的例子,所以是所有共同距离的平均值,而不是单个共同距离。在我的情况下,我想比较树中所有叶子对与所有其他叶子的相关距离的相关性,因此 Tree1:AB、AC、AD、AE...在一棵树中,并将这些与 Tree2:AB 相关联,交流,广告,AE。这应该给我一个衡量两棵树上的一片叶子的位置相似性。谢谢你帮我澄清。

标签: cluster-analysis dendrogram dendextend heatmaply


【解决方案1】:

已聚类的两个观测值之间的共同距离被定义为两个观测值首次组合成一个聚类时的组间差异。查看here 的工作示例。要深入讨论,我推荐SO post。而here 你可以看到R 的实现。

【讨论】:

  • 抱歉,我没有资格对您的答案投票。谢谢,使用这些链接我编写了代码来解决我将作为答案的问题。
  • @niels-zondervan 没问题。乐于助人。
【解决方案2】:

感谢大家的帮助,根据 vilisSO 提供的链接和 Grant 的回答,我编写了以下代码,根据完整数据和数据的子样本计算两棵树中的共同距离之间的相关性。对于树状图中的每个叶子,计算两棵树中的共生距离向量 o 之间的相关性: enter image description here

## Compare cophenetic similarity between leaves in two trees build on full data and subsample of the data

# 1 ) Generate random data to build trees
set.seed(2015-04-26)
dat <- (matrix(rnorm(100), 10, 50)) # Dataframe with 50 columns
datSubSample <- dat[, sample(ncol(dat), 30)] #Dataframe with 30 columns sampled from the dataframe with 50
dat_dist1 <- dist(datSubSample)
dat_dist2 <- dist(dat)
hc1 <- hclust(dat_dist1)
hc2 <- hclust(ddat_dist2)

# 2) Build two dendrograms, one based on all data, second based a sample of the data (30 out of 50 columns)
dendrogram1 <- as.dendrogram(hc1)
dendrogram2 <- as.dendrogram(hc2)

# 3) For each leave in a tree get cophenetic distance matrix, 
# each column represent distance of that leave to all others in the same tree
cophDistanceMatrix1 <- as.data.frame(as.matrix(cophenetic(dendrogram1)))
cophDistanceMatrix2 <- as.data.frame(as.matrix(cophenetic(dendrogram2)))

# 4) Calculate correlation between cophenetic distance of a leave to all other leaves, between two trees
corPerLeave <- NULL # Vector to store correlations for each leave in two trees
for (leave in colnames(cophDistanceMatrix1)){
  cor <- cor(cophDistanceMatrix2[leave],cophDistanceMatrix1[leave])
  corPerLeave <- c(corPerLeave, unname(cor))
}

# 5) Convert cophenetic correlation to color to show in side bar of a heatmap
corPerLeave <-corPerLeave/max(corPerLeave) #Scale 0 to 1 correlation
byPal <- colorRampPalette(c('yellow','blue')) #blue yellow color palette, low correlatio = yellow
colCopheneticCor <- byPal(20)[as.numeric(cut(corPerLeave, breaks =20))]

# 6) Plot heatmap with dendrogram with side bar that shows cophenetic correlation for each leave 
row_dend  <- dendrogram2[enter image description here][1]
x  <- as.matrix(dat_dist)
heatmaply(x,colD = row_dend,row_side_colors=colCopheneticCor)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 2017-08-22
    • 2012-03-11
    • 1970-01-01
    • 2022-08-19
    相关资源
    最近更新 更多