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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun May 27 12:34:38 CEST 2012


Author: jarioksa
Date: 2012-05-27 12:34:37 +0200 (Sun, 27 May 2012)
New Revision: 2195

Modified:
   pkg/vegan/R/betadisper.R
   pkg/vegan/man/betadisper.Rd
Log:
Squashed commit of the following:

commit 82fd87fbbc0176b36aaf90d84f5f7d5b64b9399f
Author: Jari Oksanen <jari.oksanen at oulu.fi>
Date:   Sun May 27 09:05:39 2012 +0300

    Revert "Fix calculation of ordimedian"

    This reverts commit 8f956f1ebd9c56174d6c54b1633f4c27133071f3.

commit bc0a08ff5b03206db1b6af1c17422641a66aea98
Merge: 2814a63 eec3be8
Author: jarioksa <jari.oksanen at oulu.fi>
Date:   Sat May 26 22:59:41 2012 -0700

    Merge pull request #11 from bbolker/betadisper

    Betadisper

commit 2814a6332324d7741c828f4d5b0d1f8ca9fec49a
Merge: fc05cf3 b871568
Author: Jari Oksanen <jari.oksanen at oulu.fi>
Date:   Sun May 27 08:43:27 2012 +0300

    Merge branch 'r-forge-svn-local'

commit eec3be81187b21aec10ee64ccdb729d779bfb60d
Author: Ben Bolker <bbolker at gmail.com>
Date:   Sat May 26 19:43:41 2012 -0300

    ported changes from main branch (ugh), with slight tweaks

commit 45ba30d6c72f2159d781200f65cdb20149792c7d
Author: Ben Bolker <bbolker at gmail.com>
Date:   Sat May 26 18:08:56 2012 -0300

    save bias adjustment setting as an attribute

commit 25897cf540705449ae49cbb7e1852bbec6ee5527
Merge: 2a95a08 8f956f1
Author: Jari Oksanen <jari.oksanen at oulu.fi>
Date:   Tue May 8 16:21:35 2012 +0300

    Merge branch 'ordimedian' into betadisper

commit 2a95a08b1c3d4b81cb1361bbd20c1fcdd870b807
Merge: a10ca2c 3bb2f74
Author: Jari Oksanen <jari.oksanen at oulu.fi>
Date:   Tue May 8 16:19:22 2012 +0300

    Merge branch 'master' into betadisper

commit 8f956f1ebd9c56174d6c54b1633f4c27133071f3
Author: Jari Oksanen <jari.oksanen at oulu.fi>
Date:   Tue May 8 15:48:14 2012 +0300

    Fix calculation of ordimedian

    Influences also betadisper

commit a10ca2cf02d8ce15fd3f2556bc36380464e284b1
Author: Jari Oksanen <jari.oksanen at oulu.fi>
Date:   Thu Apr 19 09:23:19 2012 +0300

    document bias.adjust in betadisper()

commit 2064496d70067ceebefa559de26513da5ed6ea30
Author: Jari Oksanen <jari.oksanen at oulu.fi>
Date:   Thu Apr 19 09:19:43 2012 +0300

    canonical way of creating 1x1 diagonal matrices

commit 53c589e7c4d02682155e04deeddb21f57eacfc06
Author: Adrian Stier <adrian.stier at gmail.com>
Date:   Wed Apr 18 10:05:52 2012 +0300

    Bias correction in betadisper with unequal sample sizes

    HI Gavin, Jari, and Marti,

    I'm writing to inquire about the importance of unequal sample sizes in
    deriving and comparing beta diversity estimates across different groups.
    My phd has focused on coral reef fish communities and in response to an
    experiment with unequal sample sizes, a reviewer recently raised the
    issue of unequal sample sizes in biasing beta diversity estimates when
    comparing across groups. I've been working on this project with my
    collaborator Ben Bolker (cc'd here). A numerical simulation he
    constructed suggests that there is indeed bias in estimates of beta
    diversity when sample sizes are unequal and sample sizes are small.

    The solution that Ben converged on was a sqrt(n/(n-1)) correction factor
    that can be applied to estimated distances to centroid for each group
    (i.e. analogous to the correction commonly applied at the for sample
    standard deviations).

     We attach a PDF file showing how we guessed this correction factor
    (which makes sense in hindsight, although I would have difficulty coming
    up with a rigorous derivation).

     We also attach a diff file from the current vegan version on r-forge
    (r2127) which includes an option to correct the bias in this way (as
    well as a one-line fix for an edge case, where there is only one
    non-zero eigenvalue -- we came across this situation in the course of
    running simulations, although it's probably not practically important
    much of the time).  We think this should also work in the presence of NA
    values, although we haven't tested it in that case.)

Modified: pkg/vegan/R/betadisper.R
===================================================================
--- pkg/vegan/R/betadisper.R	2012-05-26 18:29:22 UTC (rev 2194)
+++ pkg/vegan/R/betadisper.R	2012-05-27 10:34:37 UTC (rev 2195)
@@ -1,5 +1,5 @@
 `betadisper` <-
-    function(d, group, type = c("median","centroid"))
+    function(d, group, type = c("median","centroid"), bias.adjust=FALSE)
 {
     ## inline function for spatial medians
     spatialMed <- function(vectors, group, pos) {
@@ -67,7 +67,8 @@
     ## Remove zero eigenvalues
     eig <- eig[(want <- abs(eig/eig[1]) > TOL)]
     ## scale Eigenvectors
-    vectors <- vectors[, want, drop = FALSE] %*% diag(sqrt(abs(eig)))
+    vectors <- vectors[, want, drop = FALSE] %*% diag(sqrt(abs(eig)),
+                               nrow = length(eig))
     ## store which are the positive eigenvalues
     pos <- eig > 0
     ## group centroids in PCoA space
@@ -89,6 +90,10 @@
 
     ## zij are the distances of each point to its group centroid
     zij <- sqrt(abs(dist.pos - dist.neg))
+    if (bias.adjust) {
+        n.group <- table(group)
+        zij <- zij*sqrt(n.group[group]/(n.group[group]-1))
+    }
     ## add in correct labels
     colnames(vectors) <- names(eig) <- paste("PCoA", seq_along(eig), sep = "")
     if(is.matrix(centroids))
@@ -101,5 +106,6 @@
     class(retval) <- "betadisper"
     attr(retval, "method") <- attr(d, "method")
     attr(retval, "type") <- type
+    attr(retval, "bias.adjust") <- bias.adjust
     retval
 }

Modified: pkg/vegan/man/betadisper.Rd
===================================================================
--- pkg/vegan/man/betadisper.Rd	2012-05-26 18:29:22 UTC (rev 2194)
+++ pkg/vegan/man/betadisper.Rd	2012-05-27 10:34:37 UTC (rev 2195)
@@ -25,7 +25,7 @@
   Tukey's 'Honest Significant Difference' method.
 }
 \usage{
-betadisper(d, group, type = c("median","centroid"))
+betadisper(d, group, type = c("median","centroid"), bias.adjust = FALSE)
 
 \method{anova}{betadisper}(object, \dots)
 
@@ -50,7 +50,8 @@
     \code{\link[base]{as.factor}}. Can consist of a factor with a single
     level (i.e.~one group).}
   \item{type}{the type of analysis to perform. Use the spatial median or
-  the group centroid? The spatial median is now the default.}
+    the group centroid? The spatial median is now the default.}
+  \item{bias.adjust}{logical: adjust for small sample bias in beta diversity estimates?}
   \item{display}{character; partial match to access scores for
     \code{"sites"} or \code{"species"}.}
   \item{object, x}{an object of class \code{"betadisper"}, the result of a
@@ -133,6 +134,14 @@
   One additional use of these functions is in assessing beta diversity
   (Anderson \emph{et al} 2006). Function \code{\link{betadiver}}
   provides some popular dissimilarity measures for this purpose.
+
+  As noted in passing by Anderson (2001) and in a related
+  context by O'Neill (2000), estimates of dispersion around a
+  central location (median or centroid) that is calculated from the same data
+  will be biased downward. This bias matters most when comparing diversity
+  among treatments with small, unequal numbers of samples.  Setting
+  \code{bias.adjust=TRUE} when using \code{betadisper} imposes a 
+  \eqn{\sqrt{n/(n-1)} correction (Stier et al. 2012).
 }
 \value{
   The \code{anova} method returns an object of class \code{"anova"}
@@ -184,12 +193,23 @@
   error rates.
 }
 \references{
+  Anderson, M. J. (2001) A new method for non-parametric multivariate 
+  analysis of variance. \emph{Austral Ecology} \strong{26}, 32--46.
+
   Anderson, M.J. (2006) Distance-based tests for homogeneity of
-  multivariate dispersions. \emph{Biometrics} \strong{62(1)}, 245--253.
+  multivariate dispersions. \emph{Biometrics} \strong{62}(1), 245--253.
 
   Anderson, M.J., Ellingsen, K.E. & McArdle, B.H. (2006) Multivariate
   dispersion as a measure of beta diversity. \emph{Ecology Letters}
-  \strong{9(6)}, 683--693.
+  \strong{9}(6), 683--693.
+
+  O'Neill, M.E. (2000) A Weighted Least Squares Approach to Levene's 
+  Test of Homogeneity of Variance. \emph{Australian & New Zealand Journal of 
+  Statistics \strong{42}(1), 81-–100.
+
+  Stier, A.C., Geange, S.W., Hanson, K.M., & Bolker, B.M. (2012) Predator 
+  density and timing of arrival affect reef fish community assembly. Ms.
+  in revision, \emph{Oikos}.
 }
 \author{Gavin L. Simpson}
 \seealso{\code{\link{permutest.betadisper}}, \code{\link[stats]{anova.lm}},
@@ -249,6 +269,10 @@
 plot(mod3)
 boxplot(mod3)
 plot(TukeyHSD(mod3))
+
+## try out bias correction; compare with mod3
+(mod3B <- betadisper(dis, groups, type = "median", bias.adjust=TRUE))
+
 }
 \keyword{methods}
 \keyword{multivariate}



More information about the Vegan-commits mailing list