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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Oct 28 10:38:13 CET 2009


Author: jarioksa
Date: 2009-10-28 10:38:12 +0100 (Wed, 28 Oct 2009)
New Revision: 1058

Modified:
   pkg/vegan/R/pcnm.R
   pkg/vegan/man/pcnm.Rd
Log:
Adding 'pcnm' from sedarVegan with history: weighted PCNM

Modified: pkg/vegan/R/pcnm.R
===================================================================
--- pkg/vegan/R/pcnm.R	2009-10-28 09:36:14 UTC (rev 1057)
+++ pkg/vegan/R/pcnm.R	2009-10-28 09:38:12 UTC (rev 1058)
@@ -1,5 +1,5 @@
 "pcnm" <-
-    function(matdist, threshold, support = c("vegan", "ade4"))
+    function(matdist, threshold, support = c("vegan", "ade4"), w)
 {
     EPS <- sqrt(.Machine$double.eps)
     wa.old <- options(warn = -1)
@@ -7,6 +7,8 @@
     matdist <- as.dist(matdist)
     if (missing(threshold)) {
         support <- match.arg(support)
+        if (!missing(w) && support == "ade4")
+            stop("weights are not supported with 'ade4'")
         threshold <- 
             switch(support,
                    vegan =  max(spantree(matdist)$dist),
@@ -14,15 +16,15 @@
                    )
     }
     matdist[matdist > threshold] <- 4*threshold
-    k <- attr(matdist, "Size") - 1
-    mypcnm <- cmdscale(matdist, k = k, eig = TRUE)
-    eq0 <- abs(mypcnm$eig/max((mypcnm$eig))) <= EPS
-    inf0 <- mypcnm$eig < 0
-    res <- list()
-    res$values <- mypcnm$eig[!(eq0|inf0)]
-    res$vectors <- mypcnm$points[,!(eq0|inf0), drop = FALSE]
-    res$vectors <- sweep(res$vectors, 2, sqrt(res$values), "/")
-    colnames(res$vectors) <- paste("PCNM", 1:ncol(res$vectors), sep="")
+    ## vegan:::wcmdscale used to be able to use weights which also
+    ## means that 'k' need not be given, but all vecctorw with >0
+    ## eigenvalues will be found
+    mypcnm <- wcmdscale(matdist, eig = TRUE, w=w)
+    res <- list(vectors = mypcnm$points, values = mypcnm$eig,
+                weights = mypcnm$weig)
+    k <- ncol(mypcnm$points)
+    res$vectors <- sweep(res$vectors, 2, sqrt(res$values[1:k]), "/")
+    colnames(res$vectors) <- paste("PCNM", 1:k, sep="")
     res$threshold <- threshold
     class(res) <- "pcnm"
     res

Modified: pkg/vegan/man/pcnm.Rd
===================================================================
--- pkg/vegan/man/pcnm.Rd	2009-10-28 09:36:14 UTC (rev 1057)
+++ pkg/vegan/man/pcnm.Rd	2009-10-28 09:38:12 UTC (rev 1058)
@@ -2,10 +2,11 @@
 \alias{pcnm}
 \title{ Function to compute classical PCNM(Principal Coordinates of Neighborhood Matrix). }
 \description{
-  This function computed classical PCNM by the principal coordinate analysis of a truncated distance matrix.
+  This function computed classical PCNM by the principal coordinate
+  analysis of a truncated distance matrix. 
 }
 \usage{
-pcnm(matdist, threshold, support = c("vegan", "ade4"))
+pcnm(matdist, threshold, support = c("vegan", "ade4"), w)
 }
 
 \arguments{
@@ -16,8 +17,20 @@
     \code{matdist}. }
   \item{support}{Use either \pkg{vegan} or \pkg{ade4} function for the
     minimum spanning tree.}
+  \item{w}{Prior weights for rows.}
 }
 
+\details{
+  The function is based on \code{pcnm} function in Dray's
+  \pkg{spacemakeR} package. The differences are that the current
+  function can use either \code{\link[vegan]{spantree}} of \pkg{vegan}
+  or \code{\link[ade4]{mstree}} and \code{\link[ade4]{neig2mat}} of
+  \pkg{ade4} as internal support function. The current function also can
+  use prior weights for rows by using weighted metric scaling of
+  \code{\link[vegan]{wcmscale}} of \pkg{vegan}. The use of row weights
+  allows finding orthonormal PCNMs also for correspondence analysis
+  (e.g., \code{\link[vegan]{cca}} or \code{\link[ade4]{cca}}).  }
+
 \value{
   A list of three elements:
   \item{values }{Eigenvalues obtained by the principal coordinates analysis.}
@@ -35,6 +48,10 @@
 ordisurf(mite.xy, pcnm1$vectors[,2])
 ordisurf(mite.xy, pcnm1$vectors[,3])
 par(op)
-
+## Weighted PCNM for CCA
+data(mite)
+rs <- rowSums(mite)/sum(mite)
+pcnmw <- pcnm(dist(mite.xy), w = rs)
+ord <- cca(mite ~ pcnmw$vectors)
 }
 \keyword{ spatial }



More information about the Vegan-commits mailing list