From 4169c74e4766bc8cbdc37deee44b202db83511b0 Mon Sep 17 00:00:00 2001 From: Jari Oksanen Date: Fri, 17 Jan 2025 14:14:50 +0200 Subject: [PATCH 1/3] designdist: do not over-write input 'x' within function This allows using scalar or dist structures of 'x' in method. Zero-adjusted Bray-Curtis of Clarke & al can be written as method="(A+B-2*J)/(A+B+2*min(x[x>0]))", terms="minimum", and Bray-Curtis as method="dist(x, 'manhattan')/(A+B)", terms="minimum". --- R/designdist.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/designdist.R b/R/designdist.R index 2009e4c66..7ad100fb5 100644 --- a/R/designdist.R +++ b/R/designdist.R @@ -17,13 +17,13 @@ if (terms == "binary") x <- ifelse(x > 0, 1, 0) if (terms == "binary" || terms == "quadratic") - x <- tcrossprod(x) + XX <- tcrossprod(x) if (terms == "minimum") - x <- .Call(do_minterms, as.matrix(x)) - d <- diag(x) + XX <- .Call(do_minterms, as.matrix(x)) + d <- diag(XX) A <- as.dist(outer(rep(1, N), d)) B <- as.dist(outer(d, rep(1, N))) - J <- as.dist(x) + J <- as.dist(XX) ## 2x2 contingency table notation if (abcd) { a <- J From 99318b3fa99f1d274ce9fe5d608f951be4e51042 Mon Sep 17 00:00:00 2001 From: Jari Oksanen Date: Fri, 17 Jan 2025 14:40:58 +0200 Subject: [PATCH 2/3] designdist: document ability to use 'x' in model formula --- man/designdist.Rd | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/man/designdist.Rd b/man/designdist.Rd index d9183f944..b17018217 100644 --- a/man/designdist.Rd +++ b/man/designdist.Rd @@ -41,7 +41,8 @@ chaodist(x, method = "1 - 2*U*V/(U+V)", name) number of columns (species) or in \code{chaodist} it can use terms \code{U} and \code{V}. The equation can also contain any \R functions that accepts vector arguments and returns vectors of the - same length. } + same length. It can also include functions of input \code{x} that + return a scalar or a \code{dist} vector.} \item{terms}{How shared and total components are found. For vectors \code{x} and \code{y} the \code{"quadratic"} terms are \code{J = sum(x*y)}, \code{A = sum(x^2)}, \code{B = sum(y^2)}, and \code{"minimum"} terms @@ -162,18 +163,22 @@ chaodist(x, method = "1 - 2*U*V/(U+V)", name) large when the number of columns is high, and particularly large with quadratic terms. For precise calculations it is better to use functions like \code{\link{dist}} and \code{\link{vegdist}} which are - more robust against numerical problems. + moqre robust against numerical problems. } \seealso{ \code{\link{vegdist}}, \code{\link{betadiver}}, \code{\link{dist}}, \code{\link{raupcrick}}.} \examples{ data(BCI) -## Four ways of calculating the same Sørensen dissimilarity +## Five ways of calculating the same Sørensen dissimilarity d0 <- vegdist(BCI, "bray", binary = TRUE) d1 <- designdist(BCI, "(A+B-2*J)/(A+B)") d2 <- designdist(BCI, "(b+c)/(2*a+b+c)", abcd = TRUE) d3 <- designdist(BCI, "gamma/alpha - 1", alphagamma = TRUE) +d4 <- designdist(BCI, "dist(x, 'manhattan')/(A+B)") +## Zero-adjusted Bray-Curtis of Clarke et al. (J Exp Marine Biol & Ecol +## 330:55-80; 2006) +dbr0 <- designdist(BCI, "(A+B-2*J)/(A+B+2*min(x[x>0]))", terms = "minimum") ## Arrhenius dissimilarity: the value of z in the species-area model ## S = c*A^z when combining two sites of equal areas, where S is the ## number of species, A is the area, and c and z are model parameters. From 33e609aaf4fb69e529fdbb482709f186b505b167 Mon Sep 17 00:00:00 2001 From: Jari Oksanen Date: Fri, 17 Jan 2025 14:41:31 +0200 Subject: [PATCH 3/3] update tests for designdist examples Separate commit in case designdist changes are cherry-picked. --- tests/Examples/vegan-Ex.Rout.save | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/Examples/vegan-Ex.Rout.save b/tests/Examples/vegan-Ex.Rout.save index 79d843e29..fd161780e 100644 --- a/tests/Examples/vegan-Ex.Rout.save +++ b/tests/Examples/vegan-Ex.Rout.save @@ -2480,11 +2480,15 @@ Icmaeric Cladcerv Claddefo Cladphyl > ### ** Examples > > data(BCI) -> ## Four ways of calculating the same Sørensen dissimilarity +> ## Five ways of calculating the same Sørensen dissimilarity > d0 <- vegdist(BCI, "bray", binary = TRUE) > d1 <- designdist(BCI, "(A+B-2*J)/(A+B)") > d2 <- designdist(BCI, "(b+c)/(2*a+b+c)", abcd = TRUE) > d3 <- designdist(BCI, "gamma/alpha - 1", alphagamma = TRUE) +> d4 <- designdist(BCI, "dist(x, 'manhattan')/(A+B)") +> ## Zero-adjusted Bray-Curtis of Clarke et al. (J Exp Marine Biol & Ecol +> ## 330:55-80; 2006) +> dbr0 <- designdist(BCI, "(A+B-2*J)/(A+B+2*min(x[x>0]))", terms = "minimum") > ## Arrhenius dissimilarity: the value of z in the species-area model > ## S = c*A^z when combining two sites of equal areas, where S is the > ## number of species, A is the area, and c and z are model parameters. @@ -9142,7 +9146,7 @@ Procrustes sum of squares: > cleanEx() > options(digits = 7L) > base::cat("Time elapsed: ", proc.time() - base::get("ptime", pos = 'CheckExEnv'),"\n") -Time elapsed: 8.684 0.353 9.056 0 0 +Time elapsed: 8.741 0.323 9.079 0 0 > grDevices::dev.off() null device 1