forked from friendly/SAS-macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
biplotj.sas
308 lines (274 loc) · 11.3 KB
/
biplotj.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*------------------------------------------------------------------*
* BIPLOT SAS - Macro to construct a biplot of observations and *
* variables. Uses IML. *
*------------------------------------------------------------------*
* Author: Michael Friendly <FRIENDLY@YORKVM1> *
* Created: 1 Mar 1989 13:16:36 *
* Revised: 20 Dec 1989 09:54:19 *
* Version: 1.2 *
* *
* From ``SAS System for Statistical Graphics, First Edition'' *
* Copyright(c) 1991 by SAS Institute Inc., Cary, NC, USA *
* *
* ?CõÌ?FÇÝÆéÃ?v ( 1998.5 ) *
* ?EÃóÆ{?Þ?Œ???x???æ?í?ö?†?«?Ý?ñ?¨?‰??Ó *
* ?E?x?N?g???ŒÃã?‰?×?¶?Ü?L???æ?ƒ?Å?Ó *
* ?CõÌ?FÇÝÆéÃ?v ( 1998.8 ) *
* ?E?í?ö?I?v?V???Ã?æ‡?‚??Ó *
*------------------------------------------------------------------*/
%macro BIPLOT
(
data = _LAST_, /* Data set for biplot */
var = _NUMERIC_, /* Variables for biplot */
id = ID, /* Observation ID variable */
dim = 2, /* Number of biplot dimensions */
factype= SYM, /* Biplot factor type: GH, SYM, JK or COV */
std = STD, /* How to standardize columns: NONE|MEAN|STD*/
scale = 1, /* Scale factor for variable vectors */
out = BIPLOT, /* Output dataset: biplot coordinates */
anno = BIANNO, /* Output dataset: annotate labels */
pplot = no, /* Produce printer plot? */
vardef = df ,/* Åã?U?v?Z?Î?ŒÅãÅÞ?F DF | N */
fontolab = hwdmx001,/* ?ƒÁŒ???x???Œ?t?H?Ã?g?D1?MS?S?V?b?N */
sizeolab = 0.6 ,/* ?ƒÁŒ???x???Œ?T?C?Y */
fontvlab = hwdmx003,/* ÅõÄ???x???Œ?t?H?Ã?g?D1?MS?S?V?b?N */
sizevlab = 0.6 ,/* ÅõÄ???x???Œ?T?C?Y */
sizevtop = 0.6 ,/* ?x?N?g??Ãã?iÜV?j?Œ?T?C?Y */
markvtop = C ,/* ?x?N?g??Ãã?iÜV?jMARKER?t?H?Ã?g?ŒÂl */
/* ?uC?v??u?£?v?D?uS?v??u?{?v */
/* ?uK?v??ž?¢?u?ã?v?D?uG?v?ÁÔ?¢?u?ã?v */
plot = n , /* ?O???t?í?ö?ŒÇLÆÌ: Y | N */
device = win , /* ?oÇ?f?o?C?X: WIN | WINPRTM | WINPRTC*/
horigin = 0 cm, /* ?O???t?í?ö?Î?Œ?¶Ç]Ä */
vorigin = 0 cm, /* ?O???t?í?ö?Î?Œ?ëÇ]Ä */
hlength = 15 cm , /* ?O???t?í?ö?Î?Œõ?ÅÓ?É?ŒÂ´?Ì */
vlength = 15 cm , /* ?O???t?í?ö?Î?Œõ?ÂÒ?É?ŒÂ´?Ì */
/* A4?¡?Œ?Þ??? hlength = 25 cm ÂðÃx */
horder =, /* %str( order = ( ------ ) ) , ?á?wÂÜ?´?Ý */
vorder =, /* %str( order = ( ------ ) ) , ?á?wÂÜ?´?Ý */
/* AXIS ?æ?}?N???O?†?ÆÁO?wÂÜ?´?Ý?Þ?? */
haxis =, /* AXIS statement for horizontal axis */
vaxis =, /* and for vertical axis- use to equate axes */
hsym = 0.4, /* height of plot symbols */
hval = 0.6, /* ?ÉƘõ´?Œ???Ì */
fval = simplex, /* ?ÉƘõ´?Œ?t?H?Ã?g */
lvref = 2, /* õ?ÂÒõö?Œõö?âÄ”?? */
lhref = 2, /* õ?ÅÓõö?Œõö?âÄ”?? */
dim1lab = f = hwdmx003 h = 0.8 "&factype Biplot of &std DATA",
dim2lab = ' '
);
/*----- ?f?[?^?Z?b?gÂ??ŒÅõÄ???x???æ?ñÃÔ?´?Ý -----------------------*/
%if &data = _LAST_ %then %let data = %scan( &sysdata , 2 ) ;
%put NOTE: dataset name = &data . ;
data format ;
if 0 then set &data ;
keep start label fmtname type ;
length start $8 label $40 ;
fmtname = 'VLABELF' ;
type = 'C' ;
array vv( * ) &var ;
do i = 1 to dim( vv ) ;
call vname( vv(i), start ) ;
call label( vv(i), label ) ;
output ;
end ;
stop ;
run ;
proc format cntlin = format ;
run ;
%if %upcase( &vardef ) = DF %then %let vardef = %str( N - 1 ) ;
%let std = %upcase( &std ) ;
%let factype = %upcase( &factype ) ;
%if &factype = GH %then %let p = 0 ;
%else %if &factype = SYM %then %let p = .5 ;
%else %if &factype = JK %then %let p = 1 ;
%else %if &factype = COV %then %let p = 0 ;
%else
%do;
%put BIPLOT: FACTYPE must be GH, SYM, or JK. "&factype" is not valid.;
%goto done;
%end;
Proc IML;
Start BIPLOT(Y, ID, VARS, OUT, power, scale ) ;
N = nrow( Y ) ;
P = ncol( Y ) ;
%if &std = NONE
%then Y = Y - Y[:] %str(;); /* remove grand mean */
%else Y = Y - J(N,1,1)*Y[:,] %str(;); /* remove column means */
%if &std = STD %then %do;
S = sqrt(Y[##,] / ( &vardef ) );
Y = Y * diag (1 / S );
%end;
print "Standardized Type: &std , VARDEF = &vardef " ;
*-- Singular value decomposition:
Y is expressed as U diag(Q) V prime
Q contains singular values, in descending order;
call svd(u,q,v,y);
reset fw=8 noname;
percent = 100*q##2 / q[##];
*-- cumulate by multiplying by lower triangular matrix of 1s;
j = nrow( q ) ;
tri= (1:j)` * repeat(1,1,j) >= repeat(1, j, 1) * ( 1:j ) ;
cum = tri * percent ;
c1={'Singular Values'} ;
c2={'Percent' } ;
c3={'Cum % ' } ;
Print "Singular values and variance accounted for",,
q [ colname=c1 format=10.5 ]
percent [ colname=c2 format=10.5 ]
cum [ colname=c3 format=10.5 ];
d = &dim ;
*-- Extract first d columns of U & V, and first d elements of Q;
U = U[,1:d];
V = V[,1:d];
Q = Q[1:d];
*-- Scale the vectors by QL, QR;
* Scale factor 'scale' allows expanding or contracting the variable
vectors to plot in the same space as the observations;
QL= diag( Q ## power ) ;
QR= diag( Q ## (1-power) ) ;
A = U * QL ;
B = V * QR # scale ;
%if %upcase( &factype ) = COV %then
%do ;
A = SQRT( &vardef ) # A ;
B = ( 1 / SQRT(&vardef) ) # B ;
%end ;
OUT = A // B;
*-- Create observation labels;
id = id // vars`;
type = repeat({"OBS "},n,1) // repeat({"VAR "},p,1);
id = concat(type, id);
factype = {"GH" "Symmetric" "JK" }[ 1 + 2#power ] ;
%if %upcase( &factype ) = COV %then
%do ;
factype = { "COV" } ;
%end ;
print "Biplot Factor Type:" factype ;
cvar = concat(shape({"DIM"},1,d), char(1:d,1.));
print "Biplot coordinates",
out[rowname=id colname=cvar];
%if &pplot = YES %then
call pgraf(out,substr(id,5),'Dimension 1', 'Dimension 2', 'Biplot');
;
create &out from out[rowname=id colname=cvar];
append from out[rowname=id];
finish;
%if %upcase( &var ) = _NUMERIC_ %then %let vlist = var _NUM_ ;
%else %let vlist = var{ &var } ;
use &data;
read all &vlist into y[ colname = vars rowname = &id ] ;
power = &p;
scale = &scale;
run biplot(y, &id,vars,out, power, scale );
quit;
/*----------------------------------*
| Split ID into _TYPE_ and _NAME_ |
*----------------------------------*/
data &out;
set &out;
drop id;
length _type_ $3 _name_ $16;
_type_ = kscan(id,1);
_name_ = kscan(id,2);
run ;
/*--------------------------------------------------*
| Annotate observation labels and variable vectors |
*--------------------------------------------------*/
data &anno;
set &out;
length function style $8 text $60 ;
xsys='2'; ysys='2';
text = _name_;
if _type_ = 'OBS' then do; /* Label the observation */
color='BLACK';
x = dim1;
y = dim2;
position = '5' ;
style = "&fontolab" ;
size = &sizeolab ;
function ='LABEL ' ;
output;
end;
if _type_ = 'VAR' then do; /* Draw line from */
color='BLACK ';
x = 0; y = 0; /* the origin to */
function='MOVE' ; output;
x = dim1; y = dim2; /* the variable point */
function='DRAW' ; output;
if dim1 >=0
then position='6'; /* left justify */
else position='2'; /* right justify */
/*------- ?™?ÅŠ?‰?ŠÂu?æ?…ÂÜ?` -------------------------*/
select ;
when( x >= 0 & y >= 0 ) position = '3' ;
when( x >= 0 & y < 0 ) position = '9' ;
when( x < 0 & y >= 0 ) position = '1' ;
when( x < 0 & y < 0 ) position = '7' ;
end ;
/*---- ÅõÄ???x???ã?€?Þ?ŽÅ\?µ?´?Ý ----------------------*/
function = 'LABEL ' ;
text = put( text, $vlabelf. ) ;
style = "&fontvlab" ;
size = &sizevlab ;
output; /* variable name */
/*------ ÜV?i?×?¶?Ü?j?æÅ`?± ----------------------------*/
function = 'LABEL' ;
x = dim1 ;
y = dim2 ;
style = 'MARKER ' ;
text = "&markvtop" ;
size = &sizevtop ;
position = '5' ;
ab = sqrt( x ** 2 + y ** 2 ) ; /* ?ŽÅ“?ŒÂ´?Ì?æ???û?Ý */
sin = y / ab ; /* SIN?æ???û?Ý */
radian = arsin( sin ) ; /* ?t?T?C?Ã?–õÄ?†???W?A?Ã */
angle = abs( radian ) * 57.29578 ; /* ???W?A?Ã?æ?pÃx(õ×ÁŽÂl) */
select ; /* ?™??Œ?æ??Åã?Å0-360Ãx */
when( sign(x) = 1 & sign(y) = 1 ) angle = angle + 270 ;
when( sign(x) = -1 & sign(y) = 1 ) angle = 90 - angle ;
when( sign(x) = -1 & sign(y) = -1 ) angle = angle + 90 ;
when( sign(x) = 1 & sign(y) = -1 ) angle = 270 - angle ;
when( sign(x) = 0 & sign(y) = 1 ) angle = 0 ;
when( sign(x) = 0 & sign(y) = -1 ) angle = 180 ;
when( sign(x) = 1 & sign(y) = 0 ) angle = 270 ;
when( sign(x) = -1 & sign(y) = 0 ) angle = 90 ;
end ;
output ;
end;
run ;
%if %upcase( &plot ) = Y %then
%do;
%let hlength = %str( length = &hlength ) ;
%let vlength = %str( length = &vlength ) ;
goptions horigin = &horigin
vorigin = &vorigin
device = &device
;
symbol v = none ;
%if %length( &haxis ) = 0 %then
%do ;
axis2 label = ( &dim1lab )
&horder
value = ( f = &fval h = &hval ) &hlength ;
%let haxis = axis2 ;
%end;
%if %length( &vaxis ) = 0 %then
%do ;
axis1 label = ( &dim2lab )
&vorder
value = ( f = &fval h = &hval ) &vlength ;
%let vaxis = axis1 ;
%end;
proc gplot data = &out ;
plot dim2 * dim1 / anno = &anno frame
href = 0 vref = 0 lvref = &lvref lhref = &lhref
vaxis =&vaxis haxis = &haxis
hm = 1 vm = 1
des = "Biplot of &data"
;
run ;
quit;
%end;
%done:
%mend BIPLOT;