Skip to content
Prev 2263 / 7419 Next

standard variation in dissimilarity matrix

On Sun, 2011-07-17 at 15:51 +0200, magali proffit wrote:
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