2016-08-13 77 views
0

我有一个存在/缺少数据集,需要通过成对删除缺失值来计算Ochiai距离矩阵。什么是最简单的方法来做到这一点?如何使用R中的成对删除计算Ochiai距离矩阵

我可以使用素食主义者包的设计师生成一个矩阵,但不知道它是如何处理缺失的值。如果他们被编码为“?”它会产生一个结果,但如果编码为“NA”,则会产生所有NA的矩阵。在vegdist中,您可以指定是否需要成对删除,但不能实现Ochiai系数。据我所知,其他包中的其他距离矩阵函数都没有这种组合。有任何想法吗?

干杯,

詹姆斯

回答

0

这可能在vegan::designdist()实现,但与目前的设计只为terms="minimum"。二进制数据应通过输入的0/1变换以直接R或使用decostand(..., "pa")进行处理。以下更改将在vegan::designdist()中执行此操作:

--- a/R/designdist.R 
+++ b/R/designdist.R 
@@ -1,7 +1,7 @@ 
`designdist` <- 
    function (x, method = "(A+B-2*J)/(A+B)", 
       terms = c("binary", "quadratic", "minimum"), 
-    abcd = FALSE, alphagamma = FALSE, name) 
+    abcd = FALSE, alphagamma = FALSE, name, na.rm = FALSE) 
{ 
    terms <- match.arg(terms) 
    if ((abcd || alphagamma) && terms != "binary") 
@@ -9,13 +9,16 @@ 
    x <- as.matrix(x) 
    N <- nrow(x) 
    P <- ncol(x) 
+ ## check NA 
+ if (na.rm && terms != "minimum" && any(is.na(x))) 
+  stop("'na.rm = TRUE' can only be used with 'terms = \"minimum\"\' ") 
    if (terms == "binary") 
     x <- ifelse(x > 0, 1, 0) 
    if (terms == "binary" || terms == "quadratic") 
     x <- tcrossprod(x) 
    if (terms == "minimum") { 
-  r <- rowSums(x) 
-  x <- dist(x, "manhattan") 
+  r <- rowSums(x, na.rm = na.rm) 
+  x <- vegdist(x, "manhattan", na.rm = na.rm) 
     x <- (outer(r, r, "+") - as.matrix(x))/2 
    } 
    d <- diag(x)