s-news
[Top] [All Lists]

Re: [S] Mahanobis Distance Matrix

To: Marc Feldesman <feldesmanm@pdx.edu>
Subject: Re: [S] Mahanobis Distance Matrix
From: Bill Venables <wvenable@attunga.stats.adelaide.edu.au>
Date: Wed, 17 Feb 1999 08:50:54 +1030
Cc: s-news@wubios.wustl.edu
In-reply-to: <4.1.19990216092053.00985d40@odin.cc.pdx.edu>
References: <4.1.19990216092053.00985d40@odin.cc.pdx.edu>
Sender: owner-s-news@wubios.wustl.edu
>>>>> "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?)

2. If you were doing the last step within a function you may need
   to use the `extra arguments' dodge with apply() to take care
   of visibility issues:

DM <- apply(B, 1, function(x, B, V) mahalanobis(B, x, V), B=B, V=V)

Bill Venables.

-- 
_________________________________________________________________
Bill Venables, Department of Statistics,    Tel.: +61 8 8303 3026
The University of Adelaide,                 Fax.: +61 8 8303 3696
South AUSTRALIA.     5005.   Email: Bill.Venables@adelaide.edu.au
-----------------------------------------------------------------------
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>