Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Steve Samuels <sjsamuels@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Calculating Mahalanobis distance |

Date |
Mon, 3 May 2010 17:47:07 -0400 |

To get Mahalanobis D2, you must multiply T2 by: (1/n1 + 1/n2), still a very easy calculation. Steve 2010/5/3 Lachenbruch, Peter <Peter.Lachenbruch@oregonstate.edu>: > From the Stata manual we have > Methods and formulas > hotelling is implemented as an ado-file. > See Wilks (1962, 556-561) for a general discussion. The original formulation was by Hotelling (1931) and Mahalanobis (1930, 1936). > For the test that the means of k variables are 0, let x be a 1 x k matrix of the means and S be the estimated covariance matrix. Then T2 = xbarS(inverse)xbar'. > For two groups, the test of equality is T2 = (x1bar - x2bar)S(inverse)(x1bar - x2bar)'. > This seems a simpler way than the code given > > Tony > > Peter A. Lachenbruch > Department of Public Health > Oregon State University > Corvallis, OR 97330 > Phone: 541-737-3832 > FAX: 541-737-4001 > > > -----Original Message----- > From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Tirthankar Chakravarty > Sent: Monday, May 03, 2010 2:09 PM > To: statalist@hsphsun2.harvard.edu > Subject: Re: st: Calculating Mahalanobis distance > > <> > To calculate the centred Mahalanobis distance between two variables as > here try this: > *********************************************** > clear* > sysuse auto, clear > mata > real vector maha_dist(string scalar varlist, string scalar touse) > { > real matrix mX > real matrix mZ > st_view(mX=., ., tokens(varlist), touse) > mZ = mX :- mean(mX) > return(sqrt(diagonal(mZ*invsym(cross(mZ, mZ)/(rows(mZ)-1) )*mZ'))) > > } > end > cap prog drop md > program define md > version 11 > syntax varlist [if] [in], [GENerate(string )] > marksample touse > mata: vReturn = maha_dist("`varlist'", "`touse'") > getmata `generate' = vReturn > end > md weight length, gen(md_w_l) > su md_w_l > *********************************************** > > You can put this in two -foreach- loops to calculate the distance > between a bunch of variables. > > T > > 2010/5/3 Schijven, Mario <MSchijven@mays.tamu.edu>: >> Hi all, >> >> Does anyone know whether Stata has a pre-programmed command to compute Mahalanobis distance? So far, I've manually computed the Euclidean distance between two sets of firms in a dataset, but I need to compute the Mahalanobis distance. If Stata cannot do this, perhaps someone knows how to do it in Excel (i.e., how do I modify the formula for calculating Euclidean distance into one for Mahalanobis distance)? >> >> All the very best and many thanks in advance, >> >> Mario Schijven >> Mays Business School >> Texas A&M University >> >> * >> * For searches and help try: >> * http://www.stata.com/help.cgi?search >> * http://www.stata.com/support/statalist/faq >> * http://www.ats.ucla.edu/stat/stata/ >> > > > > -- > To every ω-consistent recursive class κ of formulae there correspond > recursive class signs r, such that neither v Gen r nor Neg(v Gen r) > belongs to Flg(κ) (where v is the free variable of r). > > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ > > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ > -- Steven Samuels sjsamuels@gmail.com 18 Cantine's Island Saugerties NY 12477 USA Voice: 845-246-0774 Fax: 206-202-4783 * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**RE: st: Calculating Mahalanobis distance***From:*"Lachenbruch, Peter" <Peter.Lachenbruch@oregonstate.edu>

**Re: st: Calculating Mahalanobis distance***From:*Tirthankar Chakravarty <tirthankar.chakravarty@gmail.com>

**References**:**st: Calculating Mahalanobis distance***From:*"Schijven, Mario" <MSchijven@mays.tamu.edu>

**Re: st: Calculating Mahalanobis distance***From:*Tirthankar Chakravarty <tirthankar.chakravarty@gmail.com>

**RE: st: Calculating Mahalanobis distance***From:*"Lachenbruch, Peter" <Peter.Lachenbruch@oregonstate.edu>

- Prev by Date:
**Re: st: creating variable from svy: proportion** - Next by Date:
**Re: st: creating variable from svy: proportion** - Previous by thread:
**RE: st: Calculating Mahalanobis distance** - Next by thread:
**Re: st: Calculating Mahalanobis distance** - Index(es):