An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-ecology/attachments/20110717/25ff01f1/attachment.pl>
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:
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 %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
On 18/07/11 13:39 PM, "Gavin Simpson" <gavin.simpson at ucl.ac.uk> wrote:
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.) ...[cut]...
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 :
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
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%