[Vegan-commits] r1442 - in branches/1.17: R inst inst/doc man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jan 8 08:54:53 CET 2011


Author: jarioksa
Date: 2011-01-08 08:54:53 +0100 (Sat, 08 Jan 2011)
New Revision: 1442

Modified:
   branches/1.17/R/CCorA.R
   branches/1.17/R/capscale.R
   branches/1.17/R/eigenvals.R
   branches/1.17/R/metaMDS.R
   branches/1.17/R/print.cca.R
   branches/1.17/R/summary.cca.R
   branches/1.17/R/wascores.R
   branches/1.17/inst/ChangeLog
   branches/1.17/inst/doc/decision-vegan.Rnw
   branches/1.17/man/capscale.Rd
   branches/1.17/man/metaMDS.Rd
Log:
merge vegan/pkg to branches/1.17 (r1428 to 1441)

Modified: branches/1.17/R/CCorA.R
===================================================================
--- branches/1.17/R/CCorA.R	2011-01-08 07:44:22 UTC (rev 1441)
+++ branches/1.17/R/CCorA.R	2011-01-08 07:54:53 UTC (rev 1442)
@@ -1,7 +1,6 @@
 `CCorA` <-
     function(Y, X, stand.Y = FALSE, stand.X = FALSE, nperm = 0, ...)
 {
-    require(MASS) || stop("Requires package 'MASS'")
     epsilon <- sqrt(.Machine$double.eps)
     ##
     ## BEGIN: Internal functions
@@ -78,7 +77,6 @@
         }
     if(nY != nX) stop("Different numbers of rows in Y and X")
     n <- nY
-    if((p+q) >= (n-1)) stop("Not enough degrees of freedom: (p+q) >= (n-1)")
     if(is.null(rownames(X)) & is.null(rownames(Y))) {
         rownoms <- paste("Obj", 1:n, sep="")
         } else {
@@ -104,6 +102,9 @@
     X <- temp$mat
     qq <- temp$m
     rownames(X) <- rownoms
+    ## Correction PL, 26dec10
+    if(max(pp,qq) >= (n-1)) 
+    	stop("Not enough degrees of freedom: max(pp,qq) >= (n-1)")
     ## Covariance matrices, etc. from the PCA scores
     S11 <- cov(Y)
     if(sum(abs(S11)) < epsilon) return(0)
@@ -129,8 +130,8 @@
     V <- K.svd$v
     A <- S11.chol.inv %*% U
     B <- S22.chol.inv %*% V
-    Cy <- (Y %*% A)/sqrt(n-1)
-    Cx <- (X %*% B)/sqrt(n-1)
+    Cy <- (Y %*% A)    # Correction 27dec10: remove /sqrt(n-1)
+    Cx <- (X %*% B)    # Correction 27dec10: remove /sqrt(n-1)
     ## Compute the 'Biplot scores of Y and X variables' a posteriori --
     corr.Y.Cy <- cor(Y.c, Cy)  # To plot Y in biplot in space Y
     corr.Y.Cx <- cor(Y.c, Cx)  # Available for plotting Y in space of X

Modified: branches/1.17/R/capscale.R
===================================================================
--- branches/1.17/R/capscale.R	2011-01-08 07:44:22 UTC (rev 1441)
+++ branches/1.17/R/capscale.R	2011-01-08 07:54:53 UTC (rev 1442)
@@ -67,8 +67,12 @@
     ## 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)
+    if (add) {
         X <- cmdscale(X, k = k, eig = TRUE, add = add)
+        ## All eigenvalues *should* be positive, but see that they are
+        X$points <- X$points[, X$eig[-(k+1)] > 0]
+        X$eig <- X$eig[X$eig > 0]
+    }
     else
         X <- wcmdscale(X, eig = TRUE)
     if (is.null(rownames(X$points))) 
@@ -76,9 +80,7 @@
     X$points <- adjust * X$points
     if (adjust == 1)
         X$eig <- X$eig/k
-    neig <- min(which(X$eig < 0) - 1, sum(X$eig > EPS))
-    sol <- X$points[, 1:neig]
-    sol <- rda.default(sol, d$Y, d$Z, ...)
+    sol <- rda.default(X$points, d$Y, d$Z, ...)
     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) <-
@@ -87,14 +89,12 @@
     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
+        ## update for negative eigenvalues
         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$imaginary.chi <- sum(negax)
-            sol$tot.chi <- sol$tot.chi + abs(sol$CA$imaginary.chi)
+            sol$tot.chi <- sol$tot.chi + sol$CA$imaginary.chi
             sol$CA$imaginary.rank <- length(negax)
             sol$CA$imaginary.u.eig <- X$negaxes
         }

Modified: branches/1.17/R/eigenvals.R
===================================================================
--- branches/1.17/R/eigenvals.R	2011-01-08 07:44:22 UTC (rev 1441)
+++ branches/1.17/R/eigenvals.R	2011-01-08 07:54:53 UTC (rev 1442)
@@ -92,7 +92,7 @@
     ## capscale
     vars <- object/sum(abs(object))
     importance <- rbind(`Eigenvalue` = object,
-                        `Proportion Explained` = round(vars, 5),
+                        `Proportion Explained` = round(abs(vars), 5),
                         `Cumulative Proportion`= round(cumsum(abs(vars)), 5))
     out <- list(importance = importance)
     class(out) <- c("summary.eigenvals", "summary.prcomp")

Modified: branches/1.17/R/metaMDS.R
===================================================================
--- branches/1.17/R/metaMDS.R	2011-01-08 07:44:22 UTC (rev 1441)
+++ branches/1.17/R/metaMDS.R	2011-01-08 07:54:53 UTC (rev 1442)
@@ -4,6 +4,15 @@
               plot = FALSE, previous.best, old.wa = FALSE, ...) 
 {
     commname <- deparse(substitute(comm))
+    ## metaMDS was written for community data which should be all
+    ## positive. Check this here, and set arguments so that they are
+    ## suitable for non-negative data.
+    if (any(autotransform, noshare > 0, wascores) && any(comm < 0)) {
+        warning("'comm' has negative data: 'autotransform', 'noshare' and 'wascores' set to FALSE")
+        wascores <- FALSE
+        autotransform <- FALSE
+        noshare <- FALSE
+    }
     if (inherits(comm, "dist")) {
         dis <- comm
         if (is.null(attr(dis, "method")))

Modified: branches/1.17/R/print.cca.R
===================================================================
--- branches/1.17/R/print.cca.R	2011-01-08 07:44:22 UTC (rev 1441)
+++ branches/1.17/R/print.cca.R	2011-01-08 07:54:53 UTC (rev 1442)
@@ -7,16 +7,23 @@
     }
     writeLines(strwrap(pasteCall(x$call)))
     cat("\n")
-    chi <- c(x$tot.chi, x$pCCA$tot.chi, x$CCA$tot.chi, x$CA$tot.chi,
+    chi <- c(x$tot.chi, if (!is.null(x$CA$imaginary.chi)) x$tot.chi - x$CA$imaginary.chi,
+                            x$pCCA$tot.chi, x$CCA$tot.chi, x$CA$tot.chi,
              x$CA$imaginary.chi)
-    props <- abs(chi)/sum(abs(chi[-1]))
-    rnk <- c(NA, x$pCCA$rank, x$CCA$rank, x$CA$rank, x$CA$imaginary.rank)
+    ## Proportions of inertia only for Real dimensions in capscale
+    if (is.null(x$CA$imaginary.chi))
+        props <- chi/chi[1]
+    else
+        props <- c(NA, chi[-c(1, length(chi))]/chi[2], NA)
+    rnk <- c(NA, if (!is.null(x$CA$imaginary.rank)) NA, x$pCCA$rank, x$CCA$rank, x$CA$rank,
+             x$CA$imaginary.rank)
     tbl <- cbind(chi, props, rnk)
     colnames(tbl) <- c("Inertia", "Proportion", "Rank")
-    rn <- c("Total", "Conditional", "Constrained", "Unconstrained",
+    rn <- c("Total", "Real Total",  "Conditional", "Constrained", "Unconstrained",
             "Imaginary")
-    rownames(tbl) <- rn[c(TRUE, !is.null(x$pCCA), !is.null(x$CCA), 
-                          !is.null(x$CA), !is.null(x$CA$imaginary.chi))]
+    rownames(tbl) <- rn[c(TRUE, !is.null(x$CA$imaginary.chi), !is.null(x$pCCA),
+                          !is.null(x$CCA),  !is.null(x$CA),
+                          !is.null(x$CA$imaginary.chi))]
     printCoefmat(tbl, digits = digits, na.print = "")
     cat("Inertia is", x$inertia, "\n")
     if (!is.null(x$CCA$alias))

Modified: branches/1.17/R/summary.cca.R
===================================================================
--- branches/1.17/R/summary.cca.R	2011-01-08 07:44:22 UTC (rev 1441)
+++ branches/1.17/R/summary.cca.R	2011-01-08 07:54:53 UTC (rev 1442)
@@ -28,6 +28,9 @@
     }
     summ$call <- object$call
     summ$tot.chi <- object$tot.chi
+    ## only the Real component for capscale() with negative eigenvalues
+    if (!is.null(object$CA$imaginary.chi))
+        summ$tot.chi <- summ$tot.chi - object$CA$imaginary.chi
     summ$partial.chi <- object$pCCA$tot.chi
     summ$constr.chi <- object$CCA$tot.chi
     summ$unconst.chi <- object$CA$tot.chi

Modified: branches/1.17/R/wascores.R
===================================================================
--- branches/1.17/R/wascores.R	2011-01-08 07:44:22 UTC (rev 1441)
+++ branches/1.17/R/wascores.R	2011-01-08 07:54:53 UTC (rev 1442)
@@ -1,6 +1,8 @@
 `wascores` <-
     function (x, w, expand = FALSE) 
 {
+    if(any(w < 0) || sum(w) == 0)
+        stop("weights must be non-negative and not all zero")
     x <- as.matrix(x)
     w <- as.matrix(w)
     nc <- ncol(x)

Modified: branches/1.17/inst/ChangeLog
===================================================================
--- branches/1.17/inst/ChangeLog	2011-01-08 07:44:22 UTC (rev 1441)
+++ branches/1.17/inst/ChangeLog	2011-01-08 07:54:53 UTC (rev 1442)
@@ -2,8 +2,25 @@
 
 VEGAN RELEASE VERSIONS at http://cran.r-project.org/
 
-Version 1.17-6 (opened December 18, 2010)
+Version 1.17-6 (release scheduled for January 10, 2011)
 
+	* merged revs 1427:1441 (versions 1.18-19 and 1.18-20) or
+	everything after release 1.17-5. 
+
+	* capscale, print.cca, summary.cca, eigenvals: handling negative
+	eigenvalues.
+
+	* capscale: robustness of add = TRUE in stats:::cmdscale.
+
+	* metaMDS: check negative data and adjust arguments.
+
+	* wascores: check negative weights and stop.
+
+	* CCorA: change scaling, tiny fixes.
+
+	* decision vignette: typos and removing text on Canoco 3 which
+	needs checking.
+
 Version 1.17-5 (released December 17, 2010)
 
 	* based on development version 1.18-18 at

Modified: branches/1.17/inst/doc/decision-vegan.Rnw
===================================================================
--- branches/1.17/inst/doc/decision-vegan.Rnw	2011-01-08 07:44:22 UTC (rev 1441)
+++ branches/1.17/inst/doc/decision-vegan.Rnw	2011-01-08 07:54:53 UTC (rev 1442)
@@ -239,15 +239,16 @@
 
 \begin{table}
   \caption{\label{tab:scales} Alternative scalings for \textsc{rda} used
-    in the functions \texttt{prcomp} and \texttt{princomp} (package
-    \texttt{mva}), and the one used in the \texttt{vegan} function \texttt{rda}
+    in the functions \texttt{prcomp} and \texttt{princomp}, and the
+    one used in the \texttt{vegan} function \texttt{rda} 
     and the proprietary software \texttt{Canoco}
     scores in terms of orthonormal species ($u_{ik}$) and site scores
     ($v_{jk}$), eigenvalues ($\lambda_k$), number of sites  ($n$) and
     species standard deviations ($s_j$). In \texttt{rda},
     $\mathrm{const} = \sqrt[4]{(n-1) \sum \lambda_k}$.  Corresponding
     negative scaling in \texttt{vegan}
-    and corresponding positive scaling in \texttt{Canoco 3}  is derived
+   % and corresponding positive scaling in \texttt{Canoco 3}  
+    is derived
     dividing each  species by its standard deviation $s_j$ (possibly
     with some additional constant multiplier).  }
 \begin{tabular}{lcc}
@@ -269,17 +270,17 @@
 \texttt{rda, scaling < 0} &
 $u_{ik}^*$ &
 $\sqrt{\sum \lambda_k /(n-1)} s_j^{-1} v_{jk}^*$
-\\
-\texttt{Canoco 3, scaling=-1} &
-$u_{ik} \sqrt{n} \sqrt{\lambda_k / \sum \lambda_k}$ &
-$v_{jk} \sqrt{n}$ \\
-\texttt{Canoco 3, scaling=-2} &
-$u_{ik} \sqrt{n}$ &
-$v_{jk} \sqrt{n} \sqrt{\lambda_k / \sum \lambda_k}$
-\\
-\texttt{Canoco 3, scaling=-3} &
-$u_{ik} \sqrt{n} \sqrt[4]{\lambda_k / \sum \lambda_k}$ &
-$v_{jk} \sqrt{n} \sqrt[4]{\lambda_k / \sum \lambda_k}$
+% \\
+% \texttt{Canoco 3, scaling=-1} &
+% $u_{ik} \sqrt{n-1} \sqrt{\lambda_k / \sum \lambda_k}$ &
+% $v_{jk} \sqrt{n}$ \\
+% \texttt{Canoco 3, scaling=-2} &
+% $u_{ik} \sqrt{n-1}$ &
+% $v_{jk} \sqrt{n} \sqrt{\lambda_k / \sum \lambda_k}$
+% \\
+% \texttt{Canoco 3, scaling=-3} &
+% $u_{ik} \sqrt{n-1} \sqrt[4]{\lambda_k / \sum \lambda_k}$ &
+% $v_{jk} \sqrt{n} \sqrt[4]{\lambda_k / \sum \lambda_k}$
 \end{tabular}
 \end{table}
 
@@ -291,14 +292,15 @@
 constant.  In functions \texttt{princomp}, \texttt{prcomp} and
 \texttt{rda}, $c=1$ and the plotted scores are a biplot so that the
 singular values (or eigenvalues) are expressed for sites, and species
-are left unscaled.  For \texttt{Canoco 3} $c = n^{-1} \sqrt{n-1}
-\sqrt{\sum \lambda_k}$ with negative \texttt{Canoco} scaling
-values. All these $c$ are constants for a matrix, so these are all
-biplots with different internal scaling of species and site scores
-with respect to each other.  For \texttt{Canoco} with positive scaling
-values and \texttt{vegan} with negative scaling values, no constant
-$c$ can be found, but the correction is dependent on species standard
-deviations $s_j$, and these scores do not define a biplot.
+are left unscaled.  
+% For \texttt{Canoco 3} $c = n^{-1} \sqrt{n-1}
+% \sqrt{\sum \lambda_k}$ with negative \texttt{Canoco} scaling
+% values. All these $c$ are constants for a matrix, so these are all
+% biplots with different internal scaling of species and site scores
+% with respect to each other.  For \texttt{Canoco} with positive scaling
+% values and \texttt{vegan} with negative scaling values, no constant
+% $c$ can be found, but the correction is dependent on species standard
+% deviations $s_j$, and these scores do not define a biplot.
 
 There is no natural way of scaling species and site scores to each
 other.  The eigenvalues in redundancy and principal components
@@ -313,7 +315,7 @@
 column scores to be invariant against scaling of data.  The solution
 in R standard function \texttt{biplot} is to scale site and species
 scores independently, and typically very differently, but plot each
-independenty to fill the graph area.  The solution in \texttt{Canoco} and 
+independently to fill the graph area.  The solution in \texttt{Canoco} and 
 and \texttt{rda} is to use proportional eigenvalues $\lambda_k / \sum
 \lambda_k$ instead of original eigenvalues.  These proportions are
 invariant with scale changes, and typically they have a nice range for
@@ -398,7 +400,7 @@
 measurements contain some errors, and therefore it is safer to use WA
 scores.
 \end{itemize}
-This articles studies mainly the first point.  The users of
+This article studies mainly the first point.  The users of
 \texttt{vegan} have a choice of either LC or WA (default) scores, but
 after reading this article, I believe that most of them do not want to
 use LC scores, because they are not what they were looking for in

Modified: branches/1.17/man/capscale.Rd
===================================================================
--- branches/1.17/man/capscale.Rd	2011-01-08 07:44:22 UTC (rev 1441)
+++ branches/1.17/man/capscale.Rd	2011-01-08 07:54:53 UTC (rev 1442)
@@ -165,13 +165,22 @@
 }
 \author{ Jari Oksanen }
 
-\note{ The function produces negative eigenvalues with many
+\note{ The function produces negative eigenvalues with non-Euclidean
   dissimilarity indices. The non-Euclidean component of inertia is
-  given under the title \code{Imaginary}, and the negative eigenvalues
-  are listed after unconstrained eigenvalues with prefix \code{NEG}.
-  The total inertia is the sum of absolute values of all eigenvalues
-  (Gower 1985). No ordination scores are given for negative
-  eigenvalues.  If the negative eigenvalues are disturbing, you can
+  given under the title \code{Imaginary} in the printed output. The
+  \code{Total} inertia is the sum of all eigenvalues, but the sum of
+  all non-negative eigenvalues is given as \code{Real Total} (which is
+  higher than the \code{Total}). The ordination is based only on the
+  real dimensions with positive eigenvalues, and therefore the
+  proportions of inertia components only apply to the \code{Real
+  Total} and ignore the \code{Imaginary} component. Permutation tests
+  with \code{\link{anova.cca}} use only the real solution of positive
+  eigenvalues. Function \code{\link{adonis}} gives similar
+  significance tests, but it also handles the imaginary dimensions
+  (negative eigenvalues) and therefore its results may differ from
+  permutation test results of \code{capscale}.
+
+  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.
   Alternatively, after square root transformation of distances

Modified: branches/1.17/man/metaMDS.Rd
===================================================================
--- branches/1.17/man/metaMDS.Rd	2011-01-08 07:44:22 UTC (rev 1441)
+++ branches/1.17/man/metaMDS.Rd	2011-01-08 07:54:53 UTC (rev 1442)
@@ -254,7 +254,15 @@
   not save the used dissimilarity matrix, but \code{metaMDSredist}
   tries to reconstruct the used dissimilarities with original data
   transformation and possible \code{\link{stepacross}}.
+
+  The \code{metaMDS} function was designed to be used with community
+  data.  If you have other type of data, you should probably set some
+  arguments to non-default values: probably at least \code{wascores},
+  \code{autotransform} and \code{noshare} should be \code{FALSE}. If
+  you have negative data entries, \code{metaMDS} will set the previous
+  to \code{FALSE} with a warning.
 }
+
 \section{Warning}{
   The calculation of \code{\link{wascores}} for species was changed in
   \pkg{vegan} version 1.12-6. They are now based on the community data



More information about the Vegan-commits mailing list