[Vegan-commits] r2342 - in pkg/vegan: R inst tests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Dec 31 20:06:36 CET 2012


Author: gsimpson
Date: 2012-12-31 20:06:36 +0100 (Mon, 31 Dec 2012)
New Revision: 2342

Modified:
   pkg/vegan/R/envfit.default.R
   pkg/vegan/R/factorfit.R
   pkg/vegan/inst/ChangeLog
   pkg/vegan/tests/vegan-tests.R
Log:
fix issue of un-used levels and use of p.max for factor constraints in plot.envfit

Modified: pkg/vegan/R/envfit.default.R
===================================================================
--- pkg/vegan/R/envfit.default.R	2012-12-19 09:50:43 UTC (rev 2341)
+++ pkg/vegan/R/envfit.default.R	2012-12-31 19:06:36 UTC (rev 2342)
@@ -13,7 +13,7 @@
         if (!na.rm)
             stop("missing values in data: consider na.rm = TRUE")
         X <- X[keep,, drop=FALSE]
-        env <- env[keep,, drop=FALSE]
+        env <- droplevels(env[keep,, drop=FALSE]) ## drop any lost levels
         na.action <- structure(seq_along(keep)[!keep], class="omit")
     }
     ## make permutation matrix for all variables handled in the next loop

Modified: pkg/vegan/R/factorfit.R
===================================================================
--- pkg/vegan/R/factorfit.R	2012-12-19 09:50:43 UTC (rev 2341)
+++ pkg/vegan/R/factorfit.R	2012-12-31 19:06:36 UTC (rev 2342)
@@ -2,6 +2,7 @@
     function (X, P, permutations = 0, strata, w,  ...) 
 {
     P <- as.data.frame(P)
+    P <- droplevels(P) ## make sure only the used levels are present
     if (any(!sapply(P, is.factor))) 
         stop("All fitted variables must be factors")
     NR <- nrow(X)

Modified: pkg/vegan/inst/ChangeLog
===================================================================
--- pkg/vegan/inst/ChangeLog	2012-12-19 09:50:43 UTC (rev 2341)
+++ pkg/vegan/inst/ChangeLog	2012-12-31 19:06:36 UTC (rev 2342)
@@ -36,6 +36,13 @@
 	predictors higher than number of observations). Pierre Legendre
 	needed this option for some checks with huge AEM/PCNM scopes.
 
+	* envfit, plot.envfit: Plotting an object fitted by envfit() would fail
+	if p.max was used and there were un-used levels for one or more factor
+	constraints. The un-used levels could result from deletion of
+	observations with missing values or simply the result of supplying
+	a subset of a larger data set to envfit(). Both cases are now handled
+	through the use of droplevels().
+
 Version 2.1-21 (closed November 19, 2012)
 
 	* New version opened with the CRAN release of vegan 2.0-5 on Oct

Modified: pkg/vegan/tests/vegan-tests.R
===================================================================
--- pkg/vegan/tests/vegan-tests.R	2012-12-19 09:50:43 UTC (rev 2341)
+++ pkg/vegan/tests/vegan-tests.R	2012-12-31 19:06:36 UTC (rev 2342)
@@ -134,3 +134,20 @@
 identical(nodf1, nodfq)
 rm(x, m, m1, nodfq, nodf1)
 ### end nestednodf
+
+### envfit & plot.envfit: latter failed if na.action resulted in only
+### observation with a given factor level was removed. plot.envfit would
+### fail with error about too long subscript
+### fixed case where data presented to envfit also has extraneous levels
+data(dune)
+data(dune.env)
+## add a new level to one of the factors
+levels(dune.env$Management) <- c(levels(dune.env$Management), "foo")
+## fit nMDS and envfit
+set.seed(1)
+mod <- metaMDS(dune)
+ef <- envfit(mod, dune.env, permutations = 99)
+plot(mod)
+plot(ef, p.max = 0.1)
+rm(mod, ef)
+### end envfit & plot.envfit



More information about the Vegan-commits mailing list