如何在R中进行聚类分析而不移除包含NA值的行。

9

我有一组数据,其中一些元素的值是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

~


2
“NA”和NA不同。但除此之外,当NA是其中一个值时,您建议如何定义两点之间的距离? - Dason
1
在我理解中daisy负责这个:http://stat.ethz.ch/R-manual/R-devel/library/cluster/html/daisy.html - neversaint
1
我不明白,你是怎么解决这个问题的?我遇到了同样的错误信息,但是找不到任何解释该如何处理的网站。我不想简单地删除NA值,我希望它们在我的热图中显示为“缺失”或类似的内容。如果你已经找到答案,请发布出来。谢谢。 - AHegde
2个回答

5

这个错误是由于数据中存在非数字变量(以字符串编码的数字)引起的。 您可以将它们转换为数字:

mydata <- apply( mtcars, 2, as.numeric )
d <- distfunc(mydata)

5
当你给一个数值型数据框赋缺失值时,请不要使用引号。这会导致问题。引号用于分隔字符常量。如果意图是将其转换为数值矩阵,那么数据中存在的字符值将强制将矩阵中的其他值转换为字符。 - TWL
1
在你的更新中,文件不是以制表符分隔的:你最终只有一列,而且由于它的内容(整行)无法转换为数字,所以所有东西都被替换为“NA”。 - Vincent Zoonekynd

3

在这种情况下,使用as.numeric可能会有帮助,但我认为原始问题指向了daisy函数中的一个错误。具体来说,它有以下代码:

    if (any(ina <- is.na(type3))) 
    stop(gettextf("invalid type %s for column numbers %s", 
        type2[ina], pColl(which(is.na))))

由于which(is.na)是错误的,所以预期的错误消息没有被打印出来。正确的写法应该是which(ina)

我猜我现在应该找出如何提交这个 bug。


1
确实,感谢@rakensi的帮助,也感谢他报告了拼写错误/思维错误,这导致了一个“不太有用”的错误信息而不是一个有用的信息。正如你所知道的,我已经在cluster软件包的开发版本中修复了代码(http://svn.r-project.org/R-packages/trunk/cluster/R/daisy.q)。 - Martin Mächler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接