【问题标题】:hierarchical clustering based on the similarity of different matrices [duplicate]基于不同矩阵相似性的层次聚类[重复]
【发布时间】:2012-10-21 21:09:23
【问题描述】:

可能重复:
How to apply a hierarchical or k-means cluster analysis using R?

考虑这四个列数相同但行数不同的矩阵

library(gtools)

m1 <- matrix(sample(c(-1, 0, 1), 15, replace=T), 3)
m2 <- matrix(sample(c(-1, 0, 1), 25, replace=T), 5)
m3 <- matrix(sample(c(-1, 0, 1), 25, replace=T), 5)
m4 <- matrix(sample(c(-1, 0, 1), 30, replace=T), 6)   
rownames(m1) <- c(1:3)
rownames(m2) <- c(4:8)
rownames(m3) <- c(9:13)
rownames(m4) <- c(14:19)

我想将hclust()应用到这四个矩阵,按以下格式排列:

mat <- list(m1, m2, m3, m4)

unite <- rbind(m1,m2,m3, m4)
rownames(unite) <- c(1:19)
distUnite <- as.matrix(dist(unite, method="manhattan"))

## empty matrix for storing the distance between pairwise matrices
dist4m <- matrix(0, nrow=4, ncol=4)
indices <- combinations(4,2)
distance <- apply(indices, 1,
                  function(pair){
                      print(pair)
                      s1=pair[1]
                      s2=pair[2] 
                      pairmean <- mean(distReads[which(m$Sample==samples[s1]), which(m$Sample==samples[s2])])

                      dist4m[s1,s2] <<- pairmean
                      dist4m[s2,s1] <<- pairmean
                  })

print(dist4m)
## then use hclust(), and plot()     

上面的脚本应该可以工作,但我想知道是否有更有效和可靠的方法来解决?

感谢您的建议。

【问题讨论】:

  • 你会在这个回答here的集群部分找到一些帮助
  • 感谢您的信息。但有不同的问题。
  • 我很欣赏差异位是不同的。聚类位不是。
  • 问题也是如此;如何对矩阵dist4m进行聚类分析?
  • 现在吉姆,你太迟钝了。您如何尝试计算矩阵之间的相似性?您定义两个矩阵的相似性的度量或方法是什么。对于单个矩阵,我可能会说我将(不)相似性定义为向量之间的欧几里得距离。你正在做各种各样的采样。如果我不知道您要实现什么(我无法从代码中看出!)您需要告诉我们您如何定义矩阵之间的相似性,以便我们看看是否有更有效的方法。

标签: r matrix


【解决方案1】:

对它们进行分组(我假设您要 cbind 和填充):

m.list <- list(m1,m2,m3,m4)
n <- max(sapply(m.list, nrow))
m.all <- do.call(cbind, lapply(m.list, function (x)
rbind(x, matrix(, n-nrow(x), ncol(x))))) 

m.dist <- dist(m.all)
m.hclust <- hclust(m.dist)
plot(m.hclust)

个人:

m1 <- matrix(sample(c(-1, 0, 1), 15, replace=T), 3) 
m2 <- matrix(sample(c(-1, 0, 1), 25, replace=T), 5)
m3 <- matrix(sample(c(-1, 0, 1), 25, replace=T), 5)
m4 <- matrix(sample(c(-1, 0, 1), 30, replace=T), 6)

m1.dist <- dist(m1)
m2.dist <- dist(m2)
m3.dist <- dist(m3)
m4.dist <- dist(m4)

m1.hclust <- hclust(m1.dist)
m2.hclust <- hclust(m2.dist)
m3.hclust <- hclust(m3.dist)
m4.hclust <- hclust(m4.dist)

plot(m1.hclust)
plot(m2.hclust)
plot(m3.hclust)
plot(m4.hclust)

【讨论】:

  • 谢谢布兰登!第一种方法很好,对我来说更有意义....还有其他方法可以计算(m1和m2,m3,m4;m2和m3,m4;m3和m4)的距离矩阵吗?不介绍 NA..
  • 您可以将 na 转换为零。 m.all[is.na(m.all)] &lt;- 0
  • 我在这里添加了一个算法:stackoverflow.com/questions/12936050/… 有点复杂,因此想知道是否有更好的方法来获取距离矩阵..
  • 对不起,我不太清楚你想要什么我已经提供了。如果不以某种方式将它们组合起来,就无法计算两个或多个矩阵之间的距离矩阵。
  • 听起来您的问题与集群无关。这是关于如何合并数据。如果是这样,您应该开始一个新问题,解释矩阵需要的形状才能正确计算 dist()。
猜你喜欢
  • 2018-04-10
  • 2015-10-17
  • 2023-03-12
  • 2018-04-29
  • 2015-12-14
  • 2012-04-22
  • 2017-07-27
  • 2018-09-25
  • 2020-07-12
相关资源
最近更新 更多