【发布时间】:2013-12-24 15:18:25
【问题描述】:
我有一个数据,其元素中包含一些 NA 值。 我想要做的是在不删除行的情况下进行聚类 NA 存在的地方。
我知道daisy 中的gower 距离测量允许这种情况。
但是为什么我下面的代码不起作用?
我欢迎“雏菊”以外的其他选择。
# plot heat map with dendogram together.
library("gplots")
library("cluster")
# Arbitrarily assigning NA to some elements
mtcars[2,2] <- "NA"
mtcars[6,7] <- "NA"
mydata <- mtcars
hclustfunc <- function(x) hclust(x, method="complete")
# Initially I wanted to use this but it didn't take NA
#distfunc <- function(x) dist(x,method="euclidean")
# Try using daisy GOWER function
# which suppose to work with NA value
distfunc <- function(x) daisy(x,metric="gower")
d <- distfunc(mydata)
fit <- hclustfunc(d)
# Perform clustering heatmap
heatmap.2(as.matrix(mydata),dendrogram="row",trace="none", margin=c(8,9), hclust=hclustfunc,distfun=distfunc);
我得到的错误信息是这样的:
Error in which(is.na) : argument to 'which' is not logical
Calls: distfunc.g -> daisy
In addition: Warning messages:
1: In data.matrix(x) : NAs introduced by coercion
2: In data.matrix(x) : NAs introduced by coercion
3: In daisy(x, metric = "gower") :
binary variable(s) 8, 9 treated as interval scaled
Execution halted
最后,我想使用 NA 允许的数据执行层次聚类。
更新
使用as.numeric 进行转换使用上面的示例。
但是为什么从文本文件中读取这段代码会失败呢?
library("gplots")
library("cluster")
# This time read from file
mtcars <- read.table("http://dpaste.com/1496666/plain/",na.strings="NA",sep="\t")
# Following suggestion convert to numeric
mydata <- apply( mtcars, 2, as.numeric )
hclustfunc <- function(x) hclust(x, method="complete")
#distfunc <- function(x) dist(x,method="euclidean")
# Try using daisy GOWER function
distfunc <- function(x) daisy(x,metric="gower")
d <- distfunc(mydata)
fit <- hclustfunc(d)
heatmap.2(as.matrix(mydata),dendrogram="row",trace="none", margin=c(8,9), hclust=hclustfunc,distfun=distfunc);
我得到的错误是这样的:
Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
Error in hclust(x, method = "complete") :
NA/NaN/Inf in foreign function call (arg 11)
Calls: hclustfunc -> hclust
Execution halted
~
【问题讨论】:
-
"NA"与NA不同。但除此之外,当 NA 是其中一个值时,您建议如何定义两点之间的距离? -
我不明白,你是怎么解决这个问题的?我遇到了同样的错误消息,我找不到任何解释该做什么的网站。我不想简单地删除 NA 值,我希望它们在我的热图中作为“缺失”或类似的东西。如果您想通了,请发布答案。谢谢。
标签: r cluster-analysis bioconductor