Skip to content

standard variation in dissimilarity matrix

4 messages · Gavin Simpson, Jari Oksanen, magali proffit

#
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
#
On 18/07/11 13:39 PM, "Gavin Simpson" <gavin.simpson at ucl.ac.uk> wrote:

            
You can do like Gav suggests in this message, but it is better to use the
function betadisper() that Gav wrote for the vegan package. Gav's betadisper
does things more correctly. It doesn't use SD or variance directly, but it
gives you dispersion for classes: I have no idea what is the SD of
*dissimilarities* (and the same applies for the averages of
dissimilarities), but betadisper() gives you meaningful values.

Cheers, Jari Oksanen
#
Dear Gavin,

thanks a lot for your reply! 
I realized yesterday evening that I could do what you suggested. I did and it works!

thanks again!

Magali

Le 18 juil. 2011 ? 12:39, Gavin Simpson a ?crit :