Bookmark and Share

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, is already up and running.

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

[no subject]


Peter A. Lachenbruch
Department of Public Health
Oregon State University
Corvallis, OR 97330
Phone: 541-737-3832
FAX: 541-737-4001

-----Original Message-----
From: [] On Behalf Of Tirthankar Chakravarty
Sent: Monday, May 03, 2010 2:09 PM
Subject: Re: st: Calculating Mahalanobis distance

To calculate the centred Mahalanobis distance between two variables as
here try this:
sysuse auto, clear
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')))
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
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.


2010/5/3 Schijven, Mario <>:
> 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:
> *
> *
> *

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:

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index