R cluster with Tanimoto/Jaccard

不羁的心 提交于 2019-12-30 05:35:06

问题


Input file is

Mydata <- read.table(con <- textConnection('
gene treatment1 treatment2 treatment3
aaa 1 0 1
bbb 1 1 1
ccc 0 0 0
eee 0 1 0
'), header=TRUE)
close(con)

Mydata is

  gene treatment1 treatment2 treatment3
1  aaa          1          0          1
2  bbb          1          1          1
3  ccc          0          0          0
4  eee          0          1          0

In order to built cluster, I have done

d <- dist(mydata, method = "euclidean")
fit <- hclust(d, method="ward") 
plot(fit)

I got the cluster based on "euclidean" distance.

In my previous message in stackoverflow How to use R to compute Tanimoto/Jacquard Score as distance matrix

I found I can also calculate tanimoto-jacquard distance matrix with R. Could you mind to teach me how to incorporate tanimoto-jacquard with the previous steps to get a cluster based on distance matrix calculated by tanimoto-jacquard distance instead of euclidean? Thanks a lot.


回答1:


What is it you don't understand? ?vegdist tells us that it returns an object of class "dist" so you can just remove the dist(....) line and replace it with one calling vegdist(....). For example:

require(vegan)
d <- vegdist(Mydata[, -1], method = "jaccard")
fit <- hclust(d, method="ward") 
plot(fit)

You need to drop the first column (and should have done in the Euclidean version you showed in your Q) as this is not data that should be used to form the dissimilarity matrix.

That will generate a warning:

Warning message:
In vegdist(Mydata[, -1], method = "jaccard") :
  you have empty rows: their dissimilarities may be meaningless in method jaccard

because row 3 contains no information to form the jaccard distance between it and the other samples. You might want to consider if the jaccard is most appropriate in such cases.

The OP now wants the gene labels as row names. The easiest option is to tell R this when reading the data in, using the row.names argument to read.table():

mydata2 <- read.table(con <- textConnection("gene treatment1 treatment2 treatment3
aaa 1 0 1
bbb 1 1 1
ccc 0 0 0
eee 0 1 0
"), header = TRUE, row.names = 1)
close(con)

giving:

> mydata2
    treatment1 treatment2 treatment3
aaa          1          0          1
bbb          1          1          1
ccc          0          0          0
eee          0          1          0

Or if the data are already in R and it is a pain to reload and redo previous computations, just assign the gene column to the row names and remove the gene column (using the original mydata):

rownames(mydata) <- mydata$gene
mydata <- mydata[, -1]

giving:

> mydata
    treatment1 treatment2 treatment3
aaa          1          0          1
bbb          1          1          1
ccc          0          0          0
eee          0          1          0


来源:https://stackoverflow.com/questions/5755070/r-cluster-with-tanimoto-jaccard

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!