s-news
[Top] [All Lists]

Re: [S] Mahanobis Distance Matrix

To: S-News mail list <s-news@wubios.wustl.edu>
Subject: Re: [S] Mahanobis Distance Matrix
From: Kim Elmore <elmore@nssl.noaa.gov>
Date: Tue, 16 Feb 1999 16:37:40 -0600 (CST)
In-reply-to: <9902162220.AA16792@attunga.stats.adelaide.edu.au>
Sender: owner-s-news@wubios.wustl.edu
On Wed, 17 Feb 1999, Bill Venables wrote:

> 
> >>>>> "Marc" == Marc Feldesman <feldesmanm@pdx.edu> writes:
> 
>     Marc> I have need to compute a Mahanobis distance matrix
>     Marc> between group centroids of 28 different groups.
>     Marc> (Because the analysis will need to be related to a
>     Marc> discriminant analysis of the identical data, and can be
>     Marc> derived from same, I need to derive the distances from
>     Marc> the pooled within groups covariance matrix).  I'm not
>     Marc> interested in the distances between individuals within
>     Marc> a group and the group centroid, or distances between
>     Marc> individuals in one group and the centroids of another
>     Marc> group.  All of this information is useful, but not
>     Marc> germane to the analysis (and display of results) I want
>     Marc> to perform.
> 
>     Marc> The mahalanobis function does not seem to do what I
>     Marc> need.  Can some one point me to a routine that gives
>     Marc> the necessary information or already does this type of
>     Marc> calculation.  I can roll my own if necessary (I think),
>     Marc> but I'd rather not reinvent the wheel if there exists a
>     Marc> well-tested routine already out there to do the same
>     Marc> thing.
> 
> I think all you need do is use mahalanobis() within apply().
> Here is a mock-up example:
> 
> > X <- matrix(rnorm(28*10*4), 28*10, 4)
> > f <- factor(rep(1:28, 10))
> 
> > fm <- aov(X ~ f-1)  # using `-1' here makes the next line work
> > B <- coef(fm)       # matrix of group means 
> 
> > V <- crossprod(resid(fm))/(28*9)  # `within' variance matrix
>  
> > DM <- apply(B, 1, function(x) mahalanobis(B, x, V))
> 
> Now DM is the (symmetric) matrix of Mahalanobis distances between
> group means relative to the within-group variance matrix estimate.
> 
> Notes:
> 
> 1. This actually does twice the work needed since it computes
>    both halves of the symmetric distance matrix, but I can't see
>    any snazzy way of avoiding that.... (challenge?)

        It turns out that MathSoft supplies a function on their help page 
for the "dist" function.  They call it dist2full and it looks like:

function(dis)
{
        N <- attr(dis, "Size")
        full <- matrix(0, n ,n)
        full[lower.tri(full)] <- dis
        full + t(full)
}

Kim Elmore
Research Associate
University of Oklahoma/National Severe Storms Laboratory

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>