-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathcanrotat.sas
515 lines (435 loc) · 14.4 KB
/
canrotat.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*-----------------------------------------------------------*
* CANROTAT.SAS *
* - (Varimax-)Rotates the raw coefficients resulting from a *
* canonical discriminant analysis (proc candisc) *
* using proc factor and computes new rotated discriminant *
* scores by means of proc score. *
* - Rotates canonical loadings (structure coefficients) in *
* a seperate step using again proc factor. *
* - Plots both rotated discriminant scores and centroids *
* as well as seperately rotated discriminant loadings *
* in one joint discriminant space via a modification *
* of the CANPLOT-MACRO by Michael Friendly (1998) *
*-----------------------------------------------------------*/
%macro canrotat(
data=_last_, /* name of data set to analyze */
class=, /* name of class variable */
var=, /* list of classification variables */
scale=4, /* scale factor for variable vectors in plot */
conf=.99, /* confidence probability for canonical means*/
out=_dscore_, /* output data set containing discrim scores */
anno=_danno_, /* output data set containing annotations */
plot=YES, /* or NO to suppress plot */
rotate=YES, /* or NO to supress rotation */
haxis=, /* AXIS statement for horizontal axis */
vaxis=, /* and for vertical axis- use to equate axes */
legend=, /* LEGEND statement */
hsym=1.2, /* height of plot symbols */
canx=can1,
cany=can2,
name=CANPLOT, /* name for graphic catalog entry */
colors=RED GREEN BLUE BLACK PURPLE YELLOW BROWN ORANGE,
symbols=+ square star - plus : $ = );
%let plot = %upcase(&PLOT);
%if %length(&var)=0 %then %do;
%put CANPLOT: You must specify VAR= variables list ;
%goto DONE;
%end;
%if %length(&class)=0 or %length(%scan(&class,2))>0 %then %do;
%put CANPLOT: You must specify one CLASS= variable ;
%goto DONE;
%end;
proc candisc data=&data
out=&out /*short*/
outstat=_dstat_;
class &class;
var &var ;
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*
*!!!!!!!!!! BEGIN OF MODIFICATION !!!!!!!!!!*
*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
%if &rotate=YES %then %do;
DATA _dstat_;
SET _dstat_;
IF &class='' THEN DELETE='NO';
IF DELETE NE 'NO' THEN DELETE;
DROP DELETE;
RUN;
/*------------------------------*
| Rotation of raw Coefficients *
*------------------------------*/
DATA _canraw_ (TYPE='FACTOR');
SET _dstat_;
IF _TYPE_ ='RAWSCORE' THEN DELETE='NO';
IF _TYPE_ ='N' THEN DELETE='NO';
IF _TYPE_ ='MEAN' THEN DELETE='NO';
IF _TYPE_='CORR' THEN DELETE='NO';
IF DELETE NE 'NO' THEN DELETE;
IF _TYPE_='RAWSCORE' THEN _TYPE_='PATTERN';
RUN;
PROC FACTOR
DATA=_canraw_
ROTATE=VARIMAX
METHOD=PATTERN
PREROTATE=NONE
OUTSTAT=_patter_;
RUN;
/*--------------------------------------------*
| Computation of rotated discriminant scores *
*--------------------------------------------*/
DATA _patter_;
SET _patter_;
IF _TYPE_ = 'PATTERN' THEN _TYPE_='SCORE';
run;
PROC SCORE
DATA=&data
SCORE=_patter_
OUT= &out;
VAR &VAR;
RUN;
/*--------------------------------------------*
| Centering of rotated discriminant scores *
*--------------------------------------------*/
PROC STANDARD DATA=&out MEAN=0 OUT=&out;
VAR &canx &cany;
RUN;
/*-----------------------------------*
| Rotation of discriminant loadings *
*-----------------------------------*/
DATA _dstat_(type=corr);
SET _dstat_;
RUN;
PROC FACTOR
DATA=_dstat_
ROTATE=VARIMAX
METHOD=SCORE
PREROTATE=NONE
OUTSTAT=_dstat_
PRINT
;
RUN;
DATA _dstat_;
set _dstat_;
if _type_='PATTERN' then _type_='STRUCTUR';
RUN;
%end;
/*----------------------------------------------*
| Computation of Standard Deviations and Means *
*----------------------------------------------*/
proc sort data=&out;
by &class;
proc means data= &out noprint css n;
output out=_dcss_ css=css1 css2 n=n1 n2 mean=mean1 mean2;
var &canx &cany;
by &class;
proc print;
data _dcss_;
set _dcss_;
drop _TYPE_ _FREQ_;
proc means data=_dcss_ noprint sum;
output out=_dcss_ sum=sumcss1 sumcss2 sumn1 sumn2;
proc print;
data _dcss_;
set _dcss_;
keep PSTD1 PSTD2;
PSTD1= SUMCSS1/(SUMN1 - _FREQ_);
PSTD2= SUMCSS2/(SUMN2 - _FREQ_);
proc print;
proc means data= &out mean noprint;
var &canx &cany;
output out=_dmean_ mean=mean1 mean2;
proc print;
run;
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*
*!!!!!!!!! END OF MODIFICATION !!!!!!!!!*
*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
proc sort data=&out;
by &class;
proc means noprint;
var &canx &cany;
by &class;
output out=_means_ mean=&canx &cany n=n;
data &anno;
set _means_ end=eof;
length text color function $8;
retain xsys '2' ysys '2';
drop &canx &cany n;
x = &canx;
y = &cany;
color=scan("&colors",_n_);
/* mark the class mean */
text = 'PLUS';
hsys = '4'; size = 4;
function = 'SYMBOL'; output;
/* draw confidence region */
size = sqrt( cinv(&conf, 2, 0) / n ) ; * radius ;
line = 20;
do a = 0 to 360 by 10; * draw a "circle" ;
ang = a*arcos(-1)/180; * convert to radians;
xp= size * cos(ang);
yp= size * sin(ang);
x = xp+ &canx; * translate to means;
y = yp+ &cany;
if a=0 then FUNCTION = 'MOVE ';
else FUNCTION = 'DRAW ';
output;
end;
if eof then do; * save number of groups ;
call symput('NGP',put(_n_,best5.));
end;
data _coeff_;
set _dstat_;
drop _TYPE_ ;
if _type_ = 'STRUCTUR';
proc transpose out=_coeff_;
proc print data=_coeff_;
%if &ngp < 3 %then %do;
%put WARNING: CANPLOT cannot produce a plot for &NGP groups.;
%goto done;
%end;
data _vector_;
set _coeff_;
where (&canx^=.);
length function text $8;
retain xsys '2' ysys '2' position '6';
x = 0 ; y = 0;
function = 'MOVE ' ; output;
x = &scale * &canx ;
y = &scale * &cany ;
function = 'DRAW ' ; output;
text = _NAME_;
function = 'LABEL' ; output;
data &anno;
set &anno _vector_;
%if &plot = YES %then %do;
%gensym(n=&ngp, h=&hsym, symbols=&symbols, colors=&colors);
%if %length(&haxis)=0 %then %do;
axis2 label=(h=&hsym) value=(h=&hsym);
%let haxis=axis2;
%end;
%if %length(&vaxis)=0 %then %do;
axis1 label=(h=&hsym) value=(h=&hsym);
%let vaxis=axis1;
%end;
%if %length(&legend)=0 %then %do;
legend1 label=(h=&hsym) value=(h=&hsym);
%let legend=legend1;
%end;
proc gplot data=&out ;
plot &cany * &canx = &class
/ anno=&anno frame
vaxis=&vaxis haxis=&haxis legend=&legend
href=0 lhref=3 vref=0 lvref=3 /* additional option */
hm=1 vm=1 name="&name"
des="canplot of &data" ;
run; quit;
%end;
/*proc datasets lib=work memtype=data nolist;
delete _coeff_ _dstat_ _means_ _vector_;*/
%done:
%mend;
/*----------------------------------------------------*
| Macro to generate SYMBOL statement for each GROUP |
*----------------------------------------------------*/
%macro gensym(n=1, h=1.5, i=none,
symbols=%str(- + : $ = X _ Y),
colors=BLACK RED GREEN BLUE BROWN YELLOW ORANGE PURPLE);
%*-- note: only 8 symbols & colors are defined;
%*-- if more than 8 groups symbols and colors are recycled;
%local chr col k;
%do k=1 %to &n ;
%if %length(%scan(&symbols, &k, %str( ))) = 0
%then %let symbols = &symbols &symbols;
%if %length(%scan(&colors, &k, %str( ))) = 0
%then %let colors = &colors &colors;
%let chr =%scan(&symbols, &k,%str( ));
%let col =%scan(&colors, &k, %str( ));
symbol&k h=&h v=&chr c=&col i=&i;
%end;
%mend gensym;
/*----------------------------------------------------*
| Example 1: Information on Resort Visits |
| (Malhotra (1996, p. 622)) |
*----------------------------------------------------*/
options ls=90 ps=50 pageno=1 nodate;
title "Information on Resort Visits (Malhotra (1996))";
data resort;
label visit='Resort Visit'
amount='Amount spent on family vacation'
income='Annual Family Income'
travel='Attitude towards travel'
vacation='Importance Attached to Family Vacation'
hsize='Household Size'
age='Age of Head of Household';
input visit $ amount $ income travel vacation hsize age;
cards;
1 M 50.2 5 8 3 43
1 H 70.3 6 7 4 61
1 H 62.9 7 5 6 52
1 L 48.5 7 5 5 36
1 H 52.7 6 6 4 55
1 H 75.0 8 7 5 68
1 M 46.2 5 3 3 62
1 M 57.0 2 4 6 51
1 H 64.1 7 5 4 57
1 H 68.1 7 6 5 45
1 H 73.4 6 7 5 44
1 H 71.9 5 8 4 64
1 M 56.2 1 8 6 54
1 H 49.3 4 2 3 56
1 H 62.0 5 6 2 58
2 L 32.1 5 4 3 58
2 L 36.2 4 3 2 55
2 M 43.2 2 5 2 57
2 M 50.4 5 2 4 37
2 M 44.1 6 6 3 42
2 L 38.3 6 6 2 45
2 M 55.0 1 2 2 57
2 L 46.1 3 5 3 51
2 L 35.0 6 4 5 64
2 L 37.3 2 7 4 54
2 M 41.8 5 1 3 56
2 M 57.0 8 3 2 36
2 L 33.4 6 8 2 50
2 L 37.5 3 2 3 48
2 L 41.3 3 3 2 42
;
run;
goptions ftext=zapf htitle=0.5cm htext=0.3cm;
axis order=(-4 to 4 by 1) length=7.5cm;
title2"Unrotated Discriminant Space";
%canrotat(
data=resort,
class=amount,
var=income travel vacation hsize age,
rotate=NO,
haxis=axis, vaxis=axis,
hsym=0.3cm
);
run;
title2"Rotated Discriminant Space";
%canrotat(
data=resort,
class=amount,
var=income travel vacation hsize age,
haxis=axis, vaxis=axis,
hsym=0.3cm
);
run;
goptions reset=all;
/*----------------------------------------------------*
| Example 2: Business-to-Business Segmentation |
| (Hair et al. (1995, pp. 742-743)) |
*----------------------------------------------------*/
options ls=90 ps=50 pageno=1 nodate;
title" Business-to-Business Segmentation (Hair et al. (1995))";
data hatco;
input ID $ SAMPLE $ x1 x2 x3 x7 x14 $;
label x1 = 'Delivery Speed'
x2 = 'Price Level'
x3 = 'Price Flexibility'
x7 = 'Product Quality'
x14= 'Type of Buying Situation';
cards;
01 A 4.1 0.6 6.9 5.2 1
02 A 1.8 3.0 6.3 8.4 1
05 A 6.0 0.9 9.6 4.5 3
06 A 1.9 3.3 7.9 9.7 2
07 A 4.6 2.4 9.5 7.6 1
08 A 1.3 4.2 6.2 6.9 2
11 A 2.4 1.6 8.8 5.8 1
12 A 3.9 2.2 9.1 8.3 2
13 A 2.8 1.4 8.1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.1 4.2 5.1 5.9 2
95 A 4.0 0.5 6.7 5.0 1
96 A 0.6 1.6 6.4 8.4 1
97 A 6.1 0.5 9.2 7.1 3
99 A 3.1 2.2 6.7 8.4 1
;
run;
goptions ftext=zapf htitle=0.5cm htext=0.3cm;
axis order=(-4 to 4 by 1) length=7.5cm;
title2"Unrotated Discriminant Space";
%canrotat(
data=hatco,
class=x14,
var=x1 x2 x3 x7,
rotate=NO,
haxis=axis, vaxis=axis,
hsym=0.3cm
);
run;
title2"Rotated Discriminant Space";
%canrotat(
data=hatco,
class=x14,
var=x1 x2 x3 x7,
haxis=axis, vaxis=axis,
hsym=0.3cm
);
run;
goptions reset=all;
/*----------------------------------------------------*
Literature:
Friendly, M. (1998):
http://www.math.yorku.ca/SCS/sasmac/canplot.html
Malhotra, N. K. (1996):
Marketing Research, 2nd Ed., Englewood Cliffs,
Prentice Hall.
Hair, J. F.; R. E. Anderson; R. L. Tatham; W. C. Black (1995):
Multivariate Data Analysis, 4th Ed., New Jearsy,
Prentice Hall.
Perreault, Jr., W. D.; D. N. Behrman, G. M. Armstrong (1979):
Alternative Approaches for Interpretation of Multiple
Discriminant Analysis in Marketing Research, Journal
of Business Research, Vol. 7, 151 - 173.
Watson, C. J. (1981):
An Additional Approach for Interpretation of Multiple
Discriminant Analysis in Business Research, Journal
of Business Research, Vol. 9, 1 - 11.
*----------------------------------------------------*/