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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Mar 8 12:20:13 CET 2011


Author: jarioksa
Date: 2011-03-08 12:20:13 +0100 (Tue, 08 Mar 2011)
New Revision: 1526

Modified:
   pkg/vegan/R/rrarefy.R
   pkg/vegan/inst/ChangeLog
   pkg/vegan/man/diversity.Rd
Log:
drarefy accepts vector 'sample'

Modified: pkg/vegan/R/rrarefy.R
===================================================================
--- pkg/vegan/R/rrarefy.R	2011-03-07 11:01:32 UTC (rev 1525)
+++ pkg/vegan/R/rrarefy.R	2011-03-08 11:20:13 UTC (rev 1526)
@@ -7,7 +7,8 @@
     if (ncol(x) == 1)
         x <- t(x)
     if (length(sample) > 1 && length(sample) != nrow(x))
-        stop("length of 'sample' and number of rows of 'x' do not match")
+        stop(gettextf,
+             "length of 'sample' and number of rows of 'x' do not match")
     sample <- rep(sample, length=nrow(x))
     colnames(x) <- colnames(x, do.NULL = FALSE)
     nm <- colnames(x)
@@ -26,16 +27,20 @@
 `drarefy` <-
     function(x, sample)
 {
-    if (length(sample) > 1)
-        stop(gettextf("only scalar 'sample' is accepted"))
+    if (length(sample) > 1 &&  length(sample) != nrow(x))
+        stop(gettextf,
+             "length of  'sample' and number of rows of 'x' do not match")
     x <- drop(as.matrix(x))
+    ## dfun is kluge: first item of  vector x must be the sample size,
+    ## and the rest  is the community data. This  seemed an easy trick
+    ## to evaluate dfun in an apply() instead of a loop.
     dfun <- function(x, sample) {
-        J <- sum(x)
-        sample <- min(sample, J)
-        1 - exp(lchoose(J - x, sample) - lchoose(J, sample))
+        J <- sum(x[-1])
+        sample <- min(x[1], J)
+        1 - exp(lchoose(J - x[-1], sample) - lchoose(J, sample))
     }
     if (length(dim(x)) > 1)
-        t(apply(x, 1, dfun, sample = sample))
+        t(apply(cbind(sample, x), 1, dfun))
     else
-        dfun(x, sample)
+        dfun(c(sample, x))
 }

Modified: pkg/vegan/inst/ChangeLog
===================================================================
--- pkg/vegan/inst/ChangeLog	2011-03-07 11:01:32 UTC (rev 1525)
+++ pkg/vegan/inst/ChangeLog	2011-03-08 11:20:13 UTC (rev 1526)
@@ -25,7 +25,8 @@
 
 	* drarefy: a new function to return the probabilities that a
 	species occurs in a rarefied sample (= sampled without
-	replacement) of a given size.
+	replacement) of a given size, (or given sizes specified
+	separately for each row if 'sample' is a vector)..
 
 	* diversity, rarefy, rrarefy, specnumber: accept vector input. All
 	functions documented in ?diversity now accept vector input.

Modified: pkg/vegan/man/diversity.Rd
===================================================================
--- pkg/vegan/man/diversity.Rd	2011-03-07 11:01:32 UTC (rev 1525)
+++ pkg/vegan/man/diversity.Rd	2011-03-08 11:20:13 UTC (rev 1526)
@@ -47,27 +47,25 @@
   Function \code{rarefy} gives the expected species richness in random
   subsamples of size \code{sample} from the community. The size of
   \code{sample} should be smaller than total community size, but the
-  function will  silently work for larger \code{sample} as well and
+  function will silently work for larger \code{sample} as well and
   return non-rarefied species richness (and standard error = 0). If
-  \code{sample} is a vector, rarefaction is performed for each sample
-  size separately.
-  Rarefaction can be performed only with genuine counts of individuals.
-  The function \code{rarefy} is based on Hurlbert's (1971) formulation,
-  and the standard errors on Heck et al. (1975).
+  \code{sample} is a vector, rarefaction of all observations is
+  performed for each sample size separately.  Rarefaction can be
+  performed only with genuine counts of individuals.  The function
+  \code{rarefy} is based on Hurlbert's (1971) formulation, and the
+  standard errors on Heck et al. (1975).
 
   Function \code{rrarefy} generates one randomly rarefied community data
   frame or vector of given \code{sample} size. The \code{sample} can be
-  a vector, and its values must be less or equal to observed number of
-  individuals. The random rarefaction is made without replacement so
-  that the variance of rarefied communities is rather related to
-  rarefaction proportion than to to the size of the \code{sample}.  
+  a vector giving the sample sizes for each row, and its values must be
+  less or equal to observed number of individuals. The random
+  rarefaction is made without replacement so that the variance of
+  rarefied communities is rather related to rarefaction proportion than
+  to to the size of the \code{sample}.
 
-  Function \code{drarefy} returns probabilities that species occur in
-  a rarefied community of size \code{sample}. Only a single scalar of
-  \code{sample} size is accepted. If \code{x} is a single site, the
-  function returns a vector of species occurrence probabilities. If
-  \code{x} is a data frame or a matrix, a matrix of probabilities for
-  each row is returned.
+  Function \code{drarefy} returns probabilities that species occur in a
+  rarefied community of size \code{sample}. The \code{sample} can be a
+  vector giving the sample sizes for each row.
 
   \code{fisher.alpha} estimates the \eqn{\alpha} parameter of
   Fisher's logarithmic series (see \code{\link{fisherfit}}). 



More information about the Vegan-commits mailing list