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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Sep 2 18:18:07 CEST 2009


Author: jarioksa
Date: 2009-09-02 18:18:06 +0200 (Wed, 02 Sep 2009)
New Revision: 985

Modified:
   pkg/vegan/R/ordiNAexclude.R
   pkg/vegan/R/scores.cca.R
   pkg/vegan/R/scores.rda.R
   pkg/vegan/inst/ChangeLog
   pkg/vegan/man/cca.object.Rd
   pkg/vegan/man/vegan-internal.Rd
Log:
na.action redesigned in cca/rda: internal structures are unchanged, but scores() can pad results with NA or WA for na.exclude

Modified: pkg/vegan/R/ordiNAexclude.R
===================================================================
--- pkg/vegan/R/ordiNAexclude.R	2009-09-02 14:48:38 UTC (rev 984)
+++ pkg/vegan/R/ordiNAexclude.R	2009-09-02 16:18:06 UTC (rev 985)
@@ -1,30 +1,48 @@
-### na.action = na.exclude puts NA values to removed observations, but
-### in constrained ordination WA scores can be found for observations
-### with NA values in constraints.
+### A pair of functions to handle na.action = na.exclude in cca and
+### rda (and capscale in the future?). Function ordiNAexclude finds
+### the WA scores for NA constraints if possible, and puts these into
+### ordination object. Function ordiNApredict pads the result scores
+### with NA or scores if available.
+
 `ordiNAexclude` <-
-    function(object, excluded)
+    function(x, excluded)
 {
     ## Check that there is a na.action of class "exclude"
-    nas <- object$na.action
-    if (is.null(nas) || !inherits(nas, "exclude"))
-        return(object)
-    ## Embed NA for excluded cases
-    object$rowsum <- napredict(nas, object$rowsum)
-    object$CCA$u <- napredict(nas, object$CCA$u)
-    object$CCA$u.eig <- napredict(nas, object$CCA$u.eig)
-    object$CCA$wa <- napredict(nas, object$CCA$wa)
-    object$CCA$wa.eig <- napredict(nas, object$CCA$wa.eig)
-    object$CA$u <- napredict(nas, object$CA$u)
-    object$CA$u.eig <- napredict(nas, object$CA$u.eig)
+    nas <- x$na.action
+    if (is.null(nas))
+        return(x)
     ## Estimate WA scores for NA cases with newdata of excluded
     ## observations
-    wa <- predict(object, newdata = excluded, type = "wa", model = "CCA")
-    wa.eig <- sweep(wa, 2, sqrt(object$CCA$eig), "*")
-    object$CCA$wa[nas,] <- wa
-    object$CCA$wa.eig[nas,] <- wa.eig
-    wa <- predict(object, newdata = excluded, type = "wa", model = "CA")
-    wa.eig <- sweep(wa, 2, sqrt(object$CA$eig), "*")
-    object$CA$u[nas,] <- wa
-    object$CA$u.eig[nas,] <- wa.eig
-    object
+    if (is.null(x$pCCA)) {
+        if (!is.null(x$CCA))
+            x$CCA$wa.excluded <- predict(x, newdata = excluded,
+                                         type = "wa", model = "CCA")
+        if (!is.null(x$CA))
+            x$CA$u.excluded <- predict(x, newdata = excluded,
+                                       type = "wa", model = "CA")
+    }
+    x
 }
+
+### Put NA or fitted WA among the scores
+
+`ordiNApredict` <-
+    function(omit, x)
+{
+    ## Only do this if omit is of class "exclude"
+    if (!inherits(omit, "exclude"))
+        return(x)
+    x$rowsum <- napredict(omit, x$rowsum) # or zero here?
+    if (!is.null(x$CCA)) {
+        x$CCA$u <- napredict(omit, x$CCA$u)
+        x$CCA$wa <- napredict(omit, x$CCA$wa)
+        if (!is.null(x$CCA$wa.excluded))
+            x$CCA$wa[omit,] <- x$CCA$wa.excluded
+    }
+    if (!is.null(x$CA)) {
+        x$CA$u <- napredict(omit, x$CA$u)
+        if (!is.null(x$CA$u.excluded))
+            x$CA$u[omit,] <- x$CA$u.excluded
+    }
+    x
+}

Modified: pkg/vegan/R/scores.cca.R
===================================================================
--- pkg/vegan/R/scores.cca.R	2009-09-02 14:48:38 UTC (rev 984)
+++ pkg/vegan/R/scores.cca.R	2009-09-02 16:18:06 UTC (rev 985)
@@ -6,6 +6,10 @@
         warning("looks like ade4::cca object: you better use ade4 functions")
         x <- ade2vegancca(x)
     }
+    ## Check the na.action, and pad the result with NA or WA if class
+    ## "exclude"
+    if (!is.null(x$na.action) && inherits(x$na.action, "exclude"))
+        x <- ordiNApredict(x$na.action, x)
     tabula <- c("species", "sites", "constraints", "biplot", 
                 "centroids")
     names(tabula) <- c("sp", "wa", "lc", "bp", "cn")

Modified: pkg/vegan/R/scores.rda.R
===================================================================
--- pkg/vegan/R/scores.rda.R	2009-09-02 14:48:38 UTC (rev 984)
+++ pkg/vegan/R/scores.rda.R	2009-09-02 16:18:06 UTC (rev 985)
@@ -2,6 +2,10 @@
     function (x, choices = c(1, 2), display = c("sp", "wa", "cn"), 
               scaling = 2, const, ...) 
 {
+    ## Check the na.action, and pad the result with NA or WA if class
+    ## "exclude"
+    if (!is.null(x$na.action) && inherits(x$na.action, "exclude"))
+        x <- ordiNApredict(x$na.action, x)
     tabula <- c("species", "sites", "constraints", "biplot", 
                 "centroids")
     names(tabula) <- c("sp", "wa", "lc", "bp", "cn")

Modified: pkg/vegan/inst/ChangeLog
===================================================================
--- pkg/vegan/inst/ChangeLog	2009-09-02 14:48:38 UTC (rev 984)
+++ pkg/vegan/inst/ChangeLog	2009-09-02 16:18:06 UTC (rev 985)
@@ -4,6 +4,14 @@
 
 Version 1.16-27 (opened September 1, 2009)
 
+	* cca & rda: New way of handling na.action. Function ordiNAexclude
+	in adds items CCA$wa.excluded or CA$u.excluded for NA rows into
+	ordination result, and function ordiNApredict called in scores.cca
+	and scores.rda pads the results with these excluded data. In this
+	way the internal structure of the cca object remains unchanged,
+	but functions accessing the result with scores will get the NA
+	data.  
+	
 	* plot.cca: works with when there are NA values in scores.
 	
 Version 1.16-26 (closed September 1, 2009)

Modified: pkg/vegan/man/cca.object.Rd
===================================================================
--- pkg/vegan/man/cca.object.Rd	2009-09-02 14:48:38 UTC (rev 984)
+++ pkg/vegan/man/cca.object.Rd	2009-09-02 16:18:06 UTC (rev 985)
@@ -109,6 +109,9 @@
     \item{\code{wa.eig}}{The direct result of weighted avaraging or weighted
       summation  (matrix multiplication)
       with the resulting eigenvalue inflation.}
+    \item{\code{wa.excluded, u.excluded}}{WA scores for rows removed by
+      \code{na.action = na.exclude} in \code{CCA} and \code{CA}
+      components if these could be calculated.}
     \item{\code{Xbar}}{The standardized data matrix after previous stages of
       analysis. In \code{CCA} this is after possible \code{pCCA} or
       after partialling out the effects of conditions, and in \code{CA}

Modified: pkg/vegan/man/vegan-internal.Rd
===================================================================
--- pkg/vegan/man/vegan-internal.Rd	2009-09-02 14:48:38 UTC (rev 984)
+++ pkg/vegan/man/vegan-internal.Rd	2009-09-02 16:18:06 UTC (rev 985)
@@ -2,6 +2,7 @@
 \alias{ordiGetData}
 \alias{ordiParseFormula}
 \alias{ordiNAexclude}
+\alias{ordiNApredict}
 \alias{permuted.index}
 \alias{centroids.cca}
 \alias{ordiTerminfo}
@@ -18,7 +19,8 @@
 ordiGetData(call, env)
 ordiParseFormula(formula, data, xlev = NULL, envdepth = 2, na.action = na.fail)
 ordiTerminfo(d, data)
-ordiNAexclude(object, excluded)
+ordiNAexclude(x, excluded)
+ordiNApredict(omit, x)
 ordiArrowMul(x, at = c(0,0), fill = 0.75)
 ordiArgAbsorber(..., shrink, origin, scaling, triangular,
                 display, choices, const, FUN)
@@ -48,7 +50,9 @@
   \code{\link{cca.object}}. \code{ordiNAexclude} implements
   \code{na.action = na.exclude} for constrained ordination finding WA
   scores of CCA components and site scores of unconstrained component
-  from \code{excluded} rows of observations.
+  from \code{excluded} rows of observations. Function
+  \code{ordiNApredict} puts pads the result object with these or with
+  WA scores similary as \code{\link{napredict}}.
 
   \code{ordiArgAbsorber} absorbs arguments of \code{\link{scores}}
   function of \pkg{vegan} so that these do not cause superfluous



More information about the Vegan-commits mailing list