[Vegan-commits] r1869 - in pkg/vegan: R inst
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Sep 22 12:26:10 CEST 2011
Author: jarioksa
Date: 2011-09-22 12:26:09 +0200 (Thu, 22 Sep 2011)
New Revision: 1869
Modified:
pkg/vegan/R/meandist.R
pkg/vegan/inst/ChangeLog
Log:
meandist could internally reorder the 'grouping' levels and return a randomly permuted matrix
Modified: pkg/vegan/R/meandist.R
===================================================================
--- pkg/vegan/R/meandist.R 2011-09-21 19:27:18 UTC (rev 1868)
+++ pkg/vegan/R/meandist.R 2011-09-22 10:26:09 UTC (rev 1869)
@@ -1,26 +1,27 @@
`meandist` <-
function(dist, grouping, ...)
{
+ if (!inherits(dist, "dist"))
+ stop("'dist' must be dissimilarity object inheriting from", dQuote(dist))
## check that 'dist' are dissimilarities (non-negative)
if (any(dist < -sqrt(.Machine$double.eps)))
warning("some dissimilarities are negative -- is this intentional?")
- ## merge levels so that lower is always first (filling lower triangle)
- mergenames <- function(X, Y, ...) {
- xy <- cbind(X, Y)
- xy <- apply(xy, 1, sort)
- apply(xy, 2, paste, collapse = " ")
- }
grouping <- factor(grouping, exclude = NULL)
- cl <- outer(grouping, grouping, mergenames)
- cl <- cl[lower.tri(cl)]
+ ## grouping for rows and columns
+ grow <- grouping[as.dist(row(as.matrix(dist)))]
+ gcol <- grouping[as.dist(col(as.matrix(dist)))]
+ ## The row index must be "smaller" of the factor levels so that
+ ## all means are in the lower triangle, and upper is NA
+ first <- as.numeric(grow) >= as.numeric(gcol)
+ cl1 <- ifelse(first, grow, gcol)
+ cl2 <- ifelse(!first, grow, gcol)
## Cannot have within-group dissimilarity for group size 1
n <- table(grouping)
take <- matrix(TRUE, nlevels(grouping), nlevels(grouping))
diag(take) <- n > 1
take[upper.tri(take)] <- FALSE
## Get output matrix
- out <- matrix(NA, nlevels(grouping), nlevels(grouping))
- out[take] <- tapply(dist, cl, mean)
+ out <- tapply(dist, list(cl1, cl2), mean)
out[upper.tri(out)] <- t(out)[upper.tri(out)]
rownames(out) <- colnames(out) <- levels(grouping)
class(out) <- c("meandist", "matrix")
Modified: pkg/vegan/inst/ChangeLog
===================================================================
--- pkg/vegan/inst/ChangeLog 2011-09-21 19:27:18 UTC (rev 1868)
+++ pkg/vegan/inst/ChangeLog 2011-09-22 10:26:09 UTC (rev 1869)
@@ -4,31 +4,37 @@
Version 2.1-1 (opened September 20, 2011)
+ * meandist bug fix: tapply() function used to find mean group x
+ group dissimilarities could reorder the class levels and return a
+ confused matrix. This could happen in particular when the
+ 'grouping' was a vector of integers which then were ordered
+ alphabetically so that "1" < "10" < "2". Now uses internally more
+ stable way of applying tapply() which should have the danger of
+ reordering the levels. Incidentally, this also seems to be
+ faster. The problem was found by Dr Miguel Alvarez (Univ Bonn).
+
* nestedness.c: changed interface in "swapcount" and "rswapcount"
which now require integer data matrix. The first argument in .C
call should now be defined as as.integer() instead of old
as.double().
* New functions: commsim is used to define Null Model Algorithms
- via a function that returns n x m x nsim array of permuted
- matrices based on structural constraints. make.commsim
- contains Null Model Algorithms already defined in vegan
- from commsimulator and permat* functions (and some more).
- The nullmodel function creates an environment, where statistics
- of the input matrix are stored. The environment also stores
- updated status of sequential algorithms and current number of
- iterations. The update and simulate methods are used to
- update the nullmodel (for sequential algorithms) or
- simulate random matrices, respectively. The simulate
- method returns the n x m nsim array (simmat class).
- Efficiency gains are sometimes high (because marginal
- statistics are calculate only once by nullmodel),
- but not significant in most cases.
- Most advanageously, this implementation
- can unite the commsimulator and permat* branches
- and can serve as basis for further extensions.
- Current intent is to investigate how this low level
- infrastructure can be used within oecosimu and permat*
+ via a function that returns n x m x nsim array of simulated
+ matrices based on structural constraints. make.commsim contains
+ Null Model Algorithms already defined in vegan from commsimulator
+ and permat* functions (and some more). The nullmodel function
+ creates an environment, where statistics of the input matrix are
+ stored. The environment also stores updated status of sequential
+ algorithms and current number of iterations. The update and
+ simulate methods are used to update the nullmodel (for sequential
+ algorithms) or simulate random matrices, respectively. The
+ simulate method returns the n x m x nsim array (simmat class).
+ Efficiency gains are sometimes high (because marginal statistics
+ are calculate only once by nullmodel), but not significant in most
+ cases. Most advantageously, this implementation can unite the
+ commsimulator and permat* branches and can serve as basis for
+ further extensions. Current intent is to investigate how this low
+ level infrastructure can be used within oecosimu and permat*
functions without breaking current vegan functionality.
Version 2.1-0 (closed September 20, 2011)
More information about the Vegan-commits
mailing list