[Vegan-commits] r851 - in pkg/vegan: R inst man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jun 2 15:28:34 CEST 2009


Author: jarioksa
Date: 2009-06-02 15:28:33 +0200 (Tue, 02 Jun 2009)
New Revision: 851

Modified:
   pkg/vegan/R/capscale.R
   pkg/vegan/inst/ChangeLog
   pkg/vegan/man/capscale.Rd
Log:
capscale handles neg eigenvalues without warning, reports them, and gives inertia as sum of all eigenvalues, incl. negative

Modified: pkg/vegan/R/capscale.R
===================================================================
--- pkg/vegan/R/capscale.R	2009-06-02 12:15:27 UTC (rev 850)
+++ pkg/vegan/R/capscale.R	2009-06-02 13:28:33 UTC (rev 851)
@@ -2,6 +2,7 @@
     function (formula, data, distance = "euclidean", comm = NULL, 
               add = FALSE, dfun = vegdist, metaMDSdist = FALSE, ...) 
 {
+    EPS <- sqrt(.Machine$double.eps)
     if (!inherits(formula, "formula")) 
         stop("Needs a model formula")
     if (missing(data)) {
@@ -34,7 +35,7 @@
     inertia <- paste("squared", inertia, "distance")
     if (add) 
         inertia <- paste(inertia, "(euclidified)")
-    k <- attr(X, "Size") - 1
+    k <- attr(X, "Size") - 1 
     if (max(X) >= 4 + .Machine$double.eps) {
         inertia <- paste("mean", inertia)
         adjust <- 1
@@ -43,17 +44,26 @@
         adjust <- k
     }
     nm <- attr(X, "Labels")
-    X <- cmdscale(X, k = k, eig = TRUE, add = add)
+    ## cmdscale is only used if 'add = TRUE': it cannot properly
+    ## handle negative eigenvalues and therefore we normally use
+    ## wcmdscale. If we have 'add = TRUE' there will be no negative
+    ## eigenvalues and this is not a problem.
+    if (add)
+        X <- cmdscale(X, k = k, eig = TRUE, add = add)
+    else
+        X <- wcmdscale(X, eig = TRUE)
     if (is.null(rownames(X$points))) 
         rownames(X$points) <- nm
     X$points <- adjust * X$points
-    neig <- min(which(X$eig < 0) - 1, k)
+    X$eig <- adjust * X$eig
+    tot.chi <- sum(X$eig) 
+    neig <- min(which(X$eig < 0) - 1, sum(X$eig > EPS))
     sol <- X$points[, 1:neig]
     fla <- update(formula, sol ~ .)
     environment(fla) <- environment()
     d <- ordiParseFormula(fla, data, envdepth = 1)
     sol <- rda.default(d$X, d$Y, d$Z, ...)
-    sol$tot.chi <- sol$tot.chi
+    sol$tot.chi <- tot.chi
     if (!is.null(sol$CCA)) {
         colnames(sol$CCA$u) <- colnames(sol$CCA$biplot) <- names(sol$CCA$eig) <-
             colnames(sol$CCA$wa) <- colnames(sol$CCA$v) <-
@@ -62,6 +72,14 @@
     if (!is.null(sol$CA)) {
         colnames(sol$CA$u) <- names(sol$CA$eig) <- colnames(sol$CA$v) <-
             paste("MDS", 1:ncol(sol$CA$u), sep = "")
+        ## Add negative eigenvalues to the list and update tot.chi
+        poseig <- length(sol$CA$eig)
+        if (any(X$eig < 0)) {
+            negax <- X$eig[X$eig < 0]
+            names(negax) <- paste("NEG", seq_along(negax), sep="")
+            sol$CA$eig <- c(sol$CA$eig, negax)
+            sol$CA$tot.chi <- abs(sum(sol$CA$eig))
+        }
     }
     if (!is.null(comm)) {
         comm <- scale(comm, center = TRUE, scale = FALSE)
@@ -76,7 +94,7 @@
         }
         if (!is.null(sol$CA)) {
             sol$CA$v.eig <- t(comm) %*% sol$CA$u/sqrt(k)
-            sol$CA$v <- sweep(sol$CA$v.eig, 2, sqrt(sol$CA$eig), 
+            sol$CA$v <- sweep(sol$CA$v.eig, 2, sqrt(sol$CA$eig[1:poseig]), 
                               "/")
         }
     }

Modified: pkg/vegan/inst/ChangeLog
===================================================================
--- pkg/vegan/inst/ChangeLog	2009-06-02 12:15:27 UTC (rev 850)
+++ pkg/vegan/inst/ChangeLog	2009-06-02 13:28:33 UTC (rev 851)
@@ -4,6 +4,15 @@
 
 Version 1.16-19 (opened May 14, 2009)
 
+	* capscale: handles now negative eigenvalues with semimetric
+	indices. These cause no more warnings. The MDS engine is now
+	'wcmdscale' instead of 'cmdscale' unless 'add = TRUE' was
+	specified (and then negative eigenvalues are not produced). The
+	negative eigenvalues are listed after positive unconstrained
+	eigenvalues, but no scores are given for them. The total inertia
+	and the total unconstrained inertia are now sums of all
+	eigenvalues, including the negative ones.
+
 	* wcmdscale: remove zero eigenvalues, keep negative, and do not
 	assume that the last eigenvalue is zero. 
 

Modified: pkg/vegan/man/capscale.Rd
===================================================================
--- pkg/vegan/man/capscale.Rd	2009-06-02 12:15:27 UTC (rev 850)
+++ pkg/vegan/man/capscale.Rd	2009-06-02 13:28:33 UTC (rev 851)
@@ -149,12 +149,15 @@
   Edition. Elsevier
 }
 \author{ Jari Oksanen }
-\note{
-  Warnings of negative eigenvalues are issued with most dissimilarity
-  indices.  These are harmless, and negative eigenvalues will be ignored
-  in the analysis.  If the warnings are disturbing, you can use argument
-  \code{add = TRUE} passed to \code{\link{cmdscale}}, or, preferably, a
-  distance measure that does not cause these warnings.  In
+
+\note{ The function produces negative eigenvalues with many
+  dissimilarity indices. The negative eigenvalues are listed after
+  positive unconstrained eigenvalues with prefix \code{NEG}.  The total
+  inertia and total unconstrained inertia are sums of all eigenvalues,
+  including negative ones. No ordination scores are given for negative
+  eigenvalues.  If the negative eigenvalues are disturbing, you can use
+  argument \code{add = TRUE} passed to \code{\link{cmdscale}}, or,
+  preferably, a distance measure that does not cause these warnings.  In
   \code{\link{vegdist}}, \code{method = "jaccard"} gives such an index.
   Alternatively, after square root transformation many indices do not
   cause warnings.



More information about the Vegan-commits mailing list