[Vegan-commits] r2810 - branches/2.0/R pkg/vegan/R pkg/vegan/inst

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Dec 8 08:36:18 CET 2013


Author: jarioksa
Date: 2013-12-08 08:36:14 +0100 (Sun, 08 Dec 2013)
New Revision: 2810

Modified:
   branches/2.0/R/envfit.default.R
   pkg/vegan/R/envfit.default.R
   pkg/vegan/R/factorfit.R
   pkg/vegan/R/vectorfit.R
   pkg/vegan/inst/ChangeLog
Log:
cleaner handling of all non-numeric variables as factors in envfit

Modified: branches/2.0/R/envfit.default.R
===================================================================
--- branches/2.0/R/envfit.default.R	2013-12-06 18:37:41 UTC (rev 2809)
+++ branches/2.0/R/envfit.default.R	2013-12-08 07:36:14 UTC (rev 2810)
@@ -1,4 +1,4 @@
-"envfit.default" <-
+`envfit.default` <-
     function (ord, env, permutations = 999, strata, choices = c(1, 2), 
              display = "sites", w = weights(ord), na.rm = FALSE, ...) 
 {
@@ -17,10 +17,14 @@
         na.action <- structure(seq_along(keep)[!keep], class="omit")
     }
     if (is.data.frame(env)) {
-        facts <- unlist(lapply(env, is.factor))
-        if (sum(facts)) {
+        facts <- sapply(env, is.factor)
+        vects <- sapply(env, is.numeric)
+        if (!all(facts | vects))
+            warning("the following variables are ignored because they are neither numeric nor factors:\n",
+                    paste(colnames(env)[!(facts | vects)], collapse=", "))
+        if (sum(facts)) {  # have factors
             Pfac <- env[, facts, drop = FALSE]
-            P <- env[, !facts, drop = FALSE]
+            P <- env[, vects, drop = FALSE]
             if (length(P)) {
                 if (permutations) {
                     if (!exists(".Random.seed", envir = .GlobalEnv, 

Modified: pkg/vegan/R/envfit.default.R
===================================================================
--- pkg/vegan/R/envfit.default.R	2013-12-06 18:37:41 UTC (rev 2809)
+++ pkg/vegan/R/envfit.default.R	2013-12-08 07:36:14 UTC (rev 2810)
@@ -31,13 +31,9 @@
                           ncol(permat), nr))
     }
     if (is.data.frame(env)) {
-        facts <- sapply(env, is.factor)
         vects <- sapply(env, is.numeric)
-        if (!all(facts | vects))
-            warning("the following variables are ignored because they are neither numeric nor factors:\n",
-                    paste(colnames(env)[!(facts | vects)], collapse=", "))
-        if (sum(facts)) {  # have factors
-            Pfac <- env[, facts, drop = FALSE]
+        if (any(!vects)) {  # have factors
+            Pfac <- env[, !vects, drop = FALSE]
             P <- env[, vects, drop = FALSE]
             if (length(P)) { # also have vectors
                 vectors <- vectorfit(X, P, permutations, strata, 

Modified: pkg/vegan/R/factorfit.R
===================================================================
--- pkg/vegan/R/factorfit.R	2013-12-06 18:37:41 UTC (rev 2809)
+++ pkg/vegan/R/factorfit.R	2013-12-08 07:36:14 UTC (rev 2810)
@@ -2,9 +2,13 @@
     function (X, P, permutations = 0, strata, w,  ...) 
 {
     P <- as.data.frame(P)
+    ## Check that all variables are factors, and coerce if necessary
+    if(any(!sapply(P, is.factor)))
+        P <- data.frame(lapply(P, function(x)
+                        if (is.factor(x)) x else factor(x)))
     P <- droplevels(P) ## make sure only the used levels are present
     if (any(!sapply(P, is.factor))) 
-        stop("All fitted variables must be factors")
+        stop("All non-numeric variables must be factors")
     NR <- nrow(X)
     NC <- ncol(X)
     NF <- ncol(P)

Modified: pkg/vegan/R/vectorfit.R
===================================================================
--- pkg/vegan/R/vectorfit.R	2013-12-06 18:37:41 UTC (rev 2809)
+++ pkg/vegan/R/vectorfit.R	2013-12-08 07:36:14 UTC (rev 2810)
@@ -54,7 +54,7 @@
         ## permutations are the matrix columns and variables are rows
         if (!is.matrix(permstore))
             permstore <- matrix(permstore, ncol=permutations)
-        permstore <- sweep(permstore, 1, r, ">")
+        permstore <- sweep(permstore, 1, r, ">=")
         validn <- rowSums(is.finite(permstore))
         pvals <- (rowSums(permstore, na.rm = TRUE) + 1)/(validn + 1)
     }

Modified: pkg/vegan/inst/ChangeLog
===================================================================
--- pkg/vegan/inst/ChangeLog	2013-12-06 18:37:41 UTC (rev 2809)
+++ pkg/vegan/inst/ChangeLog	2013-12-08 07:36:14 UTC (rev 2810)
@@ -22,13 +22,15 @@
 
 	* envfit: function assumed that environmental variables are either
 	factors or numeric, and choked if they were neither but, say,
-	character strings. Now the function ignores variables which are
-	neither factors nor numeric (with a warning). This is one possible
-	reason for problems reported in
+	character strings. Now the function tries to coerce all
+	non-numeric variables into factors, including character strings
+	and logical. This is one possible reason for problems reported in
 	https://stat.ethz.ch/pipermail/r-sig-ecology/2013-December/004217.html,
 	although the reported case is irreproducible and we cannot be
-	sure.
+	sure. Also fixed handling of tied values in assessing the P-values
+	in vectorfit.
 
+
 	* tests for cca/rda/capscale: commented out test that failed with
 	the anova.cca. Known issue was that 'by = "term"' and 'by =
 	"axis"' stop with error with missing data. This was designed and



More information about the Vegan-commits mailing list