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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Feb 28 22:02:32 CET 2013


Author: jarioksa
Date: 2013-02-28 22:02:32 +0100 (Thu, 28 Feb 2013)
New Revision: 2455

Modified:
   pkg/vegan/NAMESPACE
   pkg/vegan/R/boxplot.specaccum.R
   pkg/vegan/R/plot.specaccum.R
   pkg/vegan/R/specaccum.R
   pkg/vegan/man/specaccum.Rd
Log:
start to make weighted specaccum a first class vegan citizen

Modified: pkg/vegan/NAMESPACE
===================================================================
--- pkg/vegan/NAMESPACE	2013-02-27 19:35:21 UTC (rev 2454)
+++ pkg/vegan/NAMESPACE	2013-02-28 21:02:32 UTC (rev 2455)
@@ -200,6 +200,7 @@
 S3method(lines, radline)
 S3method(lines, radfit)
 S3method(lines, spantree)
+S3method(lines, specaccum)
 ## logLik: stats
 S3method(logLik, radfit)
 S3method(logLik, radfit.frame)

Modified: pkg/vegan/R/boxplot.specaccum.R
===================================================================
--- pkg/vegan/R/boxplot.specaccum.R	2013-02-27 19:35:21 UTC (rev 2454)
+++ pkg/vegan/R/boxplot.specaccum.R	2013-02-28 21:02:32 UTC (rev 2455)
@@ -1,11 +1,11 @@
-"boxplot.specaccum" <-
+`boxplot.specaccum` <-
     function(x, add=FALSE, ...)
 {
     if (x$method != "random")
         stop("boxplot available only for method=\"random\"")
     if (!add) {
         plot(x$sites, x$richness, type="n", xlab="Sites", ylab="Species",
-             ylim=c(1, max(x$richness)),  ...)
+             ylim=c(1, max(x$richness, na.rm = TRUE)),  ...)
     }
     tmp <- boxplot(data.frame(t(x$perm)), add=TRUE, at=x$sites, axes=FALSE, ...)
     invisible(tmp)

Modified: pkg/vegan/R/plot.specaccum.R
===================================================================
--- pkg/vegan/R/plot.specaccum.R	2013-02-27 19:35:21 UTC (rev 2454)
+++ pkg/vegan/R/plot.specaccum.R	2013-02-28 21:02:32 UTC (rev 2455)
@@ -1,17 +1,32 @@
 `plot.specaccum` <-
-    function(x, add = FALSE, ci = 2, ci.type = c("bar","line","polygon"), 
-             col = par("fg"), ci.col = col, ci.lty = 1, xlab,
-             ylab = x$method, ylim, xvar = c("sites", "individuals"), ...)
+    function(x, add = FALSE, random = FALSE, ci = 2,
+             ci.type = c("bar","line","polygon"), col = par("fg"), ci.col = col,
+             ci.lty = 1, xlab, ylab = x$method, ylim,
+             xvar = c("sites", "individuals", "effort"), ...)
 {
+    if(random && x$method != "random")
+        stop("random = TRUE can be used only with method='random'")
     xvar <- match.arg(xvar)
+    ## adjust weights to number of sites
+    if (random && !is.null(x$weights) && xvar == "sites") {
+        n <- length(x$effort)
+        adj <- n/x$effort[n]
+    } else {
+        adj <- 1
+    }
     xaxvar <- x[[xvar]]
     if (missing(xlab))
         xlab <- paste(toupper(substring(xvar, 1, 1)),
                               substring(xvar, 2), sep="")
+    if (random)
+        ci <- FALSE
     ci.type <- match.arg(ci.type)
     if (!add) {
         if (missing(ylim))
-            ylim <- c(1, max(x$richness, x$richness + ci*x$sd))
+            if (random)
+                ylim <- c(1, max(x$perm, na.rm = TRUE))
+            else
+                ylim <- c(1, max(x$richness, x$richness + ci*x$sd, na.rm = TRUE))
         plot(xaxvar, x$richness, xlab=xlab, ylab=ylab, ylim=ylim,
              type="n", ...)
     }
@@ -25,6 +40,21 @@
                  c(x$richness - ci*x$sd, rev(x$richness + ci*x$sd)), col=ci.col,
                  lty=ci.lty,  ...)
                )
-    lines(xaxvar, x$richness,col=col, ...)
+    if (random) {
+        if (is.null(x$weights)) {
+            for(i in seq_len(NCOL(x$perm)))
+                lines(xaxvar, x$perm[,i], col=col, ...)
+        } else {
+            for(i in seq_len(NCOL(x$perm)))
+                lines(x$weights[,i]*adj, x$perm[,i], col=col, ...)
+        }
+    } else
+        lines(xaxvar, x$richness,col=col, ...)
     invisible()
 }
+
+`lines.specaccum` <-
+    function(x, ...)
+{
+    plot(x, add = TRUE, ...)
+}

Modified: pkg/vegan/R/specaccum.R
===================================================================
--- pkg/vegan/R/specaccum.R	2013-02-27 19:35:21 UTC (rev 2454)
+++ pkg/vegan/R/specaccum.R	2013-02-28 21:02:32 UTC (rev 2455)
@@ -36,8 +36,17 @@
                 weights[,i] <- cumsum(w[ord])
         }
         sites <- 1:n
-        specaccum <- apply(perm, 1, mean)
-        sdaccum <- apply(perm, 1, sd)
+        if (is.null(w)) {
+            specaccum <- apply(perm, 1, mean)
+            sdaccum <- apply(perm, 1, sd)
+        } else {
+            sumw <- sum(w)
+            xout <- seq(sumw/n, sumw, length.out = n)
+            intx <- sapply(seq_len(n), function(i)
+                           approx(weights[,i], perm[,i], xout = xout)$y)
+            specaccum <- apply(intx, 1, mean)
+            sdaccum <- apply(intx, 1, sd)
+        }
     }, exact = {
         freq <- colSums(x > 0)
         freq <- freq[freq > 0]
@@ -94,8 +103,10 @@
     })
     out <- list(call = match.call(), method = method, sites = sites,
                 richness = specaccum, sd = sdaccum, perm = perm)
-    if (!is.null(w))
+    if (!is.null(w)) {
         out$weights <- weights
+        out$effort <- xout
+    }
     if (method == "rarefaction")
         out$individuals <- ind
     class(out) <- "specaccum"

Modified: pkg/vegan/man/specaccum.Rd
===================================================================
--- pkg/vegan/man/specaccum.Rd	2013-02-27 19:35:21 UTC (rev 2454)
+++ pkg/vegan/man/specaccum.Rd	2013-02-28 21:02:32 UTC (rev 2455)
@@ -18,9 +18,10 @@
 \usage{
 specaccum(comm, method = "exact", permutations = 100,
           conditioned =TRUE, gamma = "jack1",  w = NULL, ...)
-\method{plot}{specaccum}(x, add = FALSE, ci = 2, ci.type = c("bar", "line", "polygon"), 
-    col = par("fg"), ci.col = col, ci.lty = 1, xlab, 
-    ylab = x$method, ylim, xvar = c("sites", "individuals"), ...)
+\method{plot}{specaccum}(x, add = FALSE, random = FALSE, ci = 2, 
+    ci.type = c("bar", "line", "polygon"), col = par("fg"), ci.col = col, 
+    ci.lty = 1, xlab, ylab = x$method, ylim, 
+    xvar = c("sites", "individuals", "effort"), ...)
 \method{boxplot}{specaccum}(x, add = FALSE, ...)
 fitspecaccum(object, model, method = "random", ...)
 \method{plot}{fitspecaccum}(x, col = par("fg"), lty = 1, xlab = "Sites", 
@@ -48,6 +49,7 @@
     that may be removed).}
   \item{x}{A \code{specaccum} result object}
   \item{add}{Add to an existing graph.}
+  \item{random}{\dots}
   \item{ci}{Multiplier used to get confidence intervals from standard
     deviation (standard error of the estimate). Value \code{ci = 0}
     suppresses drawing confidence intervals.}



More information about the Vegan-commits mailing list