SAS中文论坛

标题: Mahalanobis distances on a heat map [打印本页]

作者: shiyiming    时间: 2012-2-17 12:52
标题: Mahalanobis distances on a heat map
From Dapangmao's blog on sas-analysis

<div class="separator" style="clear: both; text-align: center;"><a href="http://1.bp.blogspot.com/-bccx7mtmT64/Tz3NhKhJ9hI/AAAAAAAAA7U/ixPJe2yxiIM/s1600/SGRender20.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="http://1.bp.blogspot.com/-bccx7mtmT64/Tz3NhKhJ9hI/AAAAAAAAA7U/ixPJe2yxiIM/s400/SGRender20.png" width="400" /></a></div><br />
I just learned Mahalanobis distance from <a href="http://blogs.sas.com/content/iml/2012/02/15/what-is-mahalanobis-distance/">Rick’s blog post</a> yesterday, and realized <a href="http://blogs.sas.com/content/iml/2012/02/02/detecting-outliers-in-sas-part-3-multivariate-location-and-scatter/">its significance in detecting outliers</a>. One of SAS’s online documents shows how to <a href="http://support.sas.com/kb/30/662.html">use PCA method to find Mahalanobis distances</a>. And&nbsp;in SAS 9.3, the popular&nbsp;<a href="http://blogs.sas.com/content/graphicallyspeaking/2011/12/08/calendar-heatmaps-in-gtl/">heat map</a> becomes available<br />
<br />
SAS’s classic help dataset SASHELP.CLASS has weight, height, age and some other information for 19 teenagers. I calculated the pair-wise Mahalanobis distances according to their age, weight and height, and showed those distances on a heat map. It seems that it is helpful to tell how similar two teenagers are to each other.<br />
<br />
<pre style="background-color: #ebebeb; border: 1px dashed rgb(153, 153, 153); color: #000001; font-size: 14px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
/* 1 -- Find pairwise Mahalanobis distances */
proc princomp data=sashelp.class std out=_1 noprint;
&nbsp; &nbsp; &nbsp;var age weight height;
run;

proc distance data=_1 out=_2;
&nbsp; &nbsp;var interval(prin:);
&nbsp; &nbsp;id name;
run;

/* 2 -- Restructrue data */
data _3(where=(missing(distance)=0));
&nbsp; &nbsp;set _2;
&nbsp; &nbsp;array a[*] _numeric_;
&nbsp; &nbsp;do i = 1 to dim(a);
&nbsp; &nbsp; &nbsp; x = name;
&nbsp; &nbsp; &nbsp; y = vlabel(a[i]);
&nbsp; &nbsp; &nbsp; distance = a[i];
&nbsp; &nbsp; &nbsp; output;
&nbsp; &nbsp;end;
&nbsp; &nbsp;keep x y distance;
run;

data _4;
&nbsp; &nbsp;set _3 _3(rename=(x=y y=x));
run;

/* 3 -- Draw Mahalanobis distances on a heat map */
proc template;
&nbsp; define statgraph heatmapparm;
&nbsp; &nbsp; begingraph;
&nbsp; &nbsp; &nbsp; layout overlay / xaxisopts=(label=" ") yaxisopts=(label=" ");
&nbsp; &nbsp; &nbsp; &nbsp; heatmapparm x = x y = y colorresponse = distance / name = "heatmap";
&nbsp; &nbsp; &nbsp; &nbsp; continuouslegend "heatmap" / orient = vertical location = outside;
&nbsp; &nbsp; &nbsp; endlayout;
&nbsp; &nbsp; endgraph;
&nbsp; end;
run;

ods html style = money;
proc sgrender data=_4 template=heatmapparm;
run;</code><code></code></pre><br />
<div><br />
</div><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3256159328630041416-6588531911349796886?l=www.sasanalysis.com' alt='' /></div><img src="http://feeds.feedburner.com/~r/SasAnalysis/~4/d3D3P8o3GFg" height="1" width="1"/>




欢迎光临 SAS中文论坛 (http://www.mysas.net/forum/) Powered by Discuz! X3.2