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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jul 5 15:31:41 CEST 2009


Author: jarioksa
Date: 2009-07-05 15:31:40 +0200 (Sun, 05 Jul 2009)
New Revision: 888

Added:
   pkg/vegan/R/fitted.capscale.R
Modified:
   pkg/vegan/R/capscale.R
   pkg/vegan/R/predict.rda.R
   pkg/vegan/R/wcmdscale.R
   pkg/vegan/inst/ChangeLog
   pkg/vegan/man/predict.cca.Rd
Log:
capscale got 'fitted' (and hence 'residuals') method, and 'predict' with type = 'response' works -- these return dissimilarities

Modified: pkg/vegan/R/capscale.R
===================================================================
--- pkg/vegan/R/capscale.R	2009-07-04 14:35:07 UTC (rev 887)
+++ pkg/vegan/R/capscale.R	2009-07-05 13:31:40 UTC (rev 888)
@@ -85,6 +85,7 @@
             sol$CA$imaginary.chi <- sum(negax)
             sol$tot.chi <- sol$tot.chi + sol$CA$imaginary.chi
             sol$CA$imaginary.rank <- length(negax)
+            sol$CA$imaginary.u.eig <- X$negaxes
         }
     }
     if (!is.null(comm)) {

Added: pkg/vegan/R/fitted.capscale.R
===================================================================
--- pkg/vegan/R/fitted.capscale.R	                        (rev 0)
+++ pkg/vegan/R/fitted.capscale.R	2009-07-05 13:31:40 UTC (rev 888)
@@ -0,0 +1,17 @@
+fitted.capscale <-
+    function(object, model = c("CCA", "CA", "Imaginary"),
+             type = c("response", "working"), ...)
+{
+    model <- match.arg(model)
+    type <- match.arg(type)
+    ## Return scaled eigenvalues
+    U <- switch(model,
+                CCA = object$CCA$u.eig,
+                CA = object$CA$u.eig,
+                Imaginary = object$CA$imaginary.u.eig)
+    ## Distances or working scores U
+    if (type == "response")
+        dist(U)
+    else
+        U
+}

Modified: pkg/vegan/R/predict.rda.R
===================================================================
--- pkg/vegan/R/predict.rda.R	2009-07-04 14:35:07 UTC (rev 887)
+++ pkg/vegan/R/predict.rda.R	2009-07-05 13:31:40 UTC (rev 888)
@@ -23,15 +23,18 @@
         w <- u
     slam <- diag(sqrt(object[[model]]$eig[1:take] * nr), nrow = take)
     if (type == "response") {
-        if (inherits(object, "capscale")) 
-            stop("Prediction of 'response' not available in capscale")
         if (!is.null(object$pCCA)) 
             warning("Conditional ('partial') component ignored")
-        if (take > 0) 
-            out <- u %*% slam %*% t(v)
-        if (!is.null(scal)) 
-            out <- sweep(out, 2, scal, "*")
-        out <- sweep(out, 2, cent, "+")
+        if (inherits(object, "capscale")) {
+            if (take > 0)
+                out <- dist(u %*% slam/sqrt(nr))
+        } else {
+            if (take > 0) 
+                out <- u %*% slam %*% t(v)
+            if (!is.null(scal)) 
+                out <- sweep(out, 2, scal, "*")
+            out <- sweep(out, 2, cent, "+")
+        }
     }
     else if (type == "lc") {
         if (model == "CA") 

Modified: pkg/vegan/R/wcmdscale.R
===================================================================
--- pkg/vegan/R/wcmdscale.R	2009-07-04 14:35:07 UTC (rev 887)
+++ pkg/vegan/R/wcmdscale.R	2009-07-05 13:31:40 UTC (rev 888)
@@ -26,7 +26,7 @@
     ## Remove zero eigenvalues, keep negative
     keep <- abs(e$values) > ZERO
     e$values <- e$values[keep]
-    e$vectors <- e$vectors[, keep]
+    e$vectors <- e$vectors[, keep, drop = FALSE]
     ## Deweight and scale axes -- also negative
     points <- sweep(e$vectors, 1, sqrt(w), "/")
     points <- sweep(points, 2, sqrt(abs(e$values)), "*")

Modified: pkg/vegan/inst/ChangeLog
===================================================================
--- pkg/vegan/inst/ChangeLog	2009-07-04 14:35:07 UTC (rev 887)
+++ pkg/vegan/inst/ChangeLog	2009-07-05 13:31:40 UTC (rev 888)
@@ -4,6 +4,15 @@
 
 Version 1.16-21 (opened July 4, 2009)
 
+	* capscale: has now 'fitted' method (and hence, 'residuals'), and
+	'predict' works with 'type = "response"'. These give
+	dissimilarities that produce same ordination as original data, or
+	their low-rank approximations. Function 'fitted' has 'model =
+	"Imaginary"' that returns correction distances of complex
+	eigenvectors with negative eigenvalues.  The dissimilarities are
+	not additive, and the "Imaginary" component is not used in
+	estimating the unconstrained component.
+
 	* wcmdscale: returns negative eigevalues scaled by
 	sqrt(abs(eigenvalues)) if 'k' is not given or 'k' would include
 	any negative eigenvalue.

Modified: pkg/vegan/man/predict.cca.Rd
===================================================================
--- pkg/vegan/man/predict.cca.Rd	2009-07-04 14:35:07 UTC (rev 887)
+++ pkg/vegan/man/predict.cca.Rd	2009-07-05 13:31:40 UTC (rev 888)
@@ -1,6 +1,7 @@
 \name{predict.cca}
 \alias{fitted.cca}
 \alias{fitted.rda}
+\alias{fitted.capscale}
 \alias{residuals.cca}
 \alias{residuals.rda}
 \alias{predict.cca}
@@ -19,6 +20,9 @@
 \usage{
 \method{fitted}{cca}(object, model = c("CCA", "CA"),
     type =  c("response", "working"), ...)
+\method{fitted}{capscale}(object, model = c("CCA", "CA", "Imaginary"),
+    type = c("response", "working"), ...)
+\method{residuals}{cca}(object, ...)
 \method{predict}{cca}(object, newdata, type = c("response", "wa", "sp", "lc"),
     rank = "full", model = c("CCA", "CA"), scaling = FALSE, ...)
 calibrate.cca(object, newdata, rank = "full", ...)
@@ -31,7 +35,8 @@
   \item{object}{A result object from \code{\link{cca}},
     \code{\link{rda}}, \code{\link{capscale}} or \code{\link{decorana}}. }
   \item{model}{Show constrained (\code{"CCA"}) or unconstrained
-    (\code{"CA"}) results. }
+    (\code{"CA"}) results. For \code{\link{capscale}} this can also be 
+    \code{"Imaginary"} for imaginary components with negative eigenvalues. }
   \item{newdata}{New data frame to be used in
     prediction of species and site scores or for calibration.  Usually
     this a new community data frame, but for \code{predict.cca}
@@ -41,12 +46,15 @@
     \code{fitted} and \code{residuals}, \code{"response"} scales results so
     that the same ordination gives the same results, and \code{"working"}
     gives the values used internally, that is after Chi-square
-    standardization in \code{cca} and scaling and centring in \code{rda}.
+    standardization in \code{\link{cca}} and scaling and centring in 
+    \code{\link{rda}}. In \code{\link{capscale}} the \code{"response"} gives
+    the dissimilarities, and \code{"working"} the scaled scores that produce
+    the dissimlarities as Euclidean distances.
     In \code{predict} \code{"response"} 
-    gives an approximation of the original data matrix, \code{"wa"} the
-    site scores as weighted averages of the community data, \code{"lc"}
-    the site scores as linear combinations of environmental data, and
-    \code{"sp"} the species scores.  In \code{predict.decorana} the
+    gives an approximation of the original data matrix or dissimilarities, 
+    \code{"wa"} the site scores as weighted averages of the community data, 
+    \code{"lc"} the site scores as linear combinations of environmental data, 
+    and \code{"sp"} the species scores. In \code{predict.decorana} the
     alternatives are scores for \code{"sites"} or \code{"species"}.} 
   \item{rank}{The rank or the number of axes used in the approximation.
     The default is to use all axes (full rank) of the \code{"model"} or
@@ -58,22 +66,29 @@
 }
 \details{
   Function \code{fitted} gives the approximation of the original data
-  matrix from the ordination result either in the scale of the response
+  matrix or dissimilarities from the ordination result either in the 
+  scale of the response
   or as scaled internally by the function.  Function \code{residuals} gives
   the approximation of the original data from the unconstrained
   ordination.  With argument \code{type = "response"} the
   \code{fitted.cca} and \code{residuals.cca} function 
   both give the same marginal totals as the original data matrix, and
-  their entries do not add up to the original data. They are defined so
+  their entries do not add up to the original data. 
+  Functions \code{fitted.capscale} and \code{residuals.capscale} give the
+  dissimilarities with \code{type = "response"}, but these are not additive,
+  but the \code{"working"} scores are additive. 
+  All variants of \code{fitted} and \code{residuals} are defined so
   that for model \code{mod <- cca(y ~ x)}, \code{cca(fitted(mod))} is equal
   to constrained ordination, and \code{cca(residuals(mod))} is equal to
   unconstrained part of the ordination.
 
   Function \code{predict} can find the estimate of the original data
-  matrix (\code{type = "response"}) with any rank. With \code{rank =
-    "full"} it is identical to \code{fitted}.  In addition, the function
+  matrix or dissimilarites (\code{type = "response"}) with any rank. 
+  With \code{rank = "full"} it is identical to \code{fitted}.
+  In addition, the function
   can find the species scores or site scores from the community data
-  matrix.  The function can be used with new data, and it can be used to
+  matrix for \code{\link{cca}} or \code{\link{rda}}.  
+  The function can be used with new data, and it can be used to
   add new species or site scores to existing ordinations.  The function
   returns (weighted) orthonormal scores by default, and you must
   specify explicit \code{scaling} to
@@ -122,7 +137,7 @@
 }
 
 \value{
-  The functions return matrices or vectors as is appropriate.
+  The functions return matrices, vectors or dissimilarities as is appropriate.
 }
 \references{
   Greenacre, M. J. (1984). Theory and applications of correspondence



More information about the Vegan-commits mailing list