On Sun, 2011-07-17 at 15:51 +0200, magali proffit wrote: > Dear all, > linked to MRPP in Vegan there is a really nice function (meandist) > which calculates a matrix of mean of distance, based on a grouping > factor, from matrices of dissimilarity. I was wondering if there is an > equivalent function calculating the standard deviation instead of the > mean distance or if anyone has an idea how to do it. > thanks a lot! > Magali
meandist() computes the mean within and between block dissimilarities. Do you want the within block standard deviation of dissimilarities and the between block standard deviation of dissimilarities? If so, you could just grab the sources for meandist and change this line: out[take] <- tapply(dist, cl, mean) to be out[take] <- tapply(dist, cl, sd) (or use `var` in place of `sd` if you want the variance instead of the standard deviation.). Here is a function that does just that, using `sd`: sddist <- function (dist, grouping, ...) { 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)] n <- table(grouping) take <- matrix(TRUE, nlevels(grouping), nlevels(grouping)) diag(take) <- n > 1 take[upper.tri(take)] <- FALSE out <- matrix(NA, nlevels(grouping), nlevels(grouping)) out[take] <- tapply(dist, cl, sd) out[upper.tri(out)] <- t(out)[upper.tri(out)] rownames(out) <- colnames(out) <- levels(grouping) class(out) <- c("meandist", "matrix") attr(out, "n") <- table(grouping) out } ## meandist require(vegan) data(dune) data(dune.env) dune.md <- with(dune.env, meandist(vegdist(dune), Management)) dune.sd <- with(dune.env, sddist(vegdist(dune), Management)) HTH G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% _______________________________________________ R-sig-ecology mailing list R-sig-ecology@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology