-
Notifications
You must be signed in to change notification settings - Fork 5
/
A55234.sas
3512 lines (2910 loc) · 98.7 KB
/
A55234.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************\
| This file contains programs that appear in |
| "Applied Multivariate Statistics with SAS (R) Software," by |
| Ravindra Khattree and Dayanand N. Naik (pubcode 55234). |
| |
| Copyright (C) 1995 by SAS Institute Inc., Cary, NC, USA. |
| |
| SAS (R) is a registered trademark of SAS Institute Inc. |
| |
| SAS Institute does not assume responsibility for the accuracy of |
| any material presented in this file. |
\*******************************************************************/
---------------
Chapter 1
---------------
/* Program 1.1 */
options ls=78 ps=45 nodate nonumber;
data cork;
infile 'cork.dat' firstobs = 1;
input north east south west;
proc calis data = cork kurtosis ;
title1 j=l "Output 1.1";
title2 "Computation of Mardia's Kurtosis" ;
lineqs
north = e1 ,
east = e2 ,
south = e3 ,
west = e4 ;
std
e1=eps1, e2=eps2, e3=eps3, e4=eps4 ;
cov
e1=eps1, e2=eps2, e3=eps3, e4=eps4 ;
run ;
/* cork.dat */
72 66 76 77
60 53 66 63
56 57 64 58
41 29 36 38
32 32 35 36
30 35 34 26
39 39 31 27
42 43 31 25
37 40 31 25
33 29 27 36
32 30 34 28
63 45 74 63
54 46 60 52
47 51 52 43
91 79 100 75
56 68 47 50
79 65 70 61
81 80 68 58
78 55 67 60
46 38 37 38
39 35 34 37
32 30 30 32
60 50 67 54
35 37 48 39
39 36 39 31
50 34 37 40
43 37 39 50
48 54 57 43
/* Cork Boring Data: Source: C.R.Rao (1948). Reproduced with
permission of the Biometrika Trustees. */
/* Program 1.2 */
title 'Output 1.2';
options ls = 76 nodate nonumber;
/* In this program we are testing for the multivariate normality
of C. R. Rao's cork data using the Mardia's skewness and kurtosis
measures*/
proc iml ;
y ={
72 66 76 77,
60 53 66 63,
56 57 64 58,
41 29 36 38,
32 32 35 36,
30 35 34 26,
39 39 31 27,
42 43 31 25,
37 40 31 25,
33 29 27 36,
32 30 34 28,
63 45 74 63,
54 46 60 52,
47 51 52 43,
91 79 100 75,
56 68 47 50,
79 65 70 61,
81 80 68 58,
78 55 67 60,
46 38 37 38,
39 35 34 37,
32 30 30 32,
60 50 67 54,
35 37 48 39,
39 36 39 31,
50 34 37 40,
43 37 39 50,
48 54 57 43} ;
/* Here we determine the number of data points and the dimension
of the vector. dfchi is the degrees of freedom for the chi square
approximation of Multivariate skewness. */
n = nrow(y) ;
p = ncol(y) ;
dfchi = p*(p+1)*(p+2)/6 ;
/* q is projection matrix. s is the maximum likelihood estimate
of the variance covariance matrix. g_matrix is n by n the matrix
of g(i,j) elements. beta1hat and beta2hat are respectively the
Mardia's sample skewness and kurtosis measures. Kappa1 and kappa2
are the test statistics based on skewness and kurtosis to test
for normality and pvalskew and pvalkurt are corresponding p
values. */
q = i(n) - (1/n)*j(n,n,1) ;
s = (1/(n))*y`*q*y ; s_inv = inv(s) ;
g_matrix = q*y*s_inv*y`*q ;
beta1hat = ( sum(g_matrix#g_matrix#g_matrix) )/(n*n) ;
beta2hat =trace( g_matrix#g_matrix )/n ;
kappa1 = n*beta1hat/6 ;
kappa2 = (beta2hat - p*(p+2) ) /sqrt(8*p*(p+2)/n) ;
pvalskew = 1 - probchi(kappa1,dfchi) ;
pvalkurt = 2*( 1 - probnorm(abs(kappa2)) ) ;
print s ;
print s_inv ;
print beta1hat ;
print kappa1 ;
print pvalskew ;
print beta2hat ;
print kappa2 ;
print pvalkurt ;
run;
/* Program 1.3 */
title 'Output 1.3: Multivariate Normal Sample';
/*Generate n random vector from a p dimensional population with
mean mu and the covariance matrix sigma */
options ls = 76 nodate nonumber;
proc iml ;
seed = 549065467 ;
n = 4 ;
sigma = { 4 2 1,
2 3 1,
1 1 5 };
mu = {1, 3, 0};
p = nrow(sigma);
m = repeat(mu`,n,1) ;
g =inv(root(sigma)) ;
z =normal(repeat(seed,n,p)) ;
y = z*g + m ;
print y ;
run;
/* Program 1.4 */
title 'Output 1.4: Wishart Random Matrix';
/*Generate n p by p Wishart matrices with degrees of freedom f */
options ls=76 nodate nonumber;
proc iml;
n = 4 ;
f = 7 ;
seed = 4509049 ;
sigma = {4 2 1,
2 3 1,
1 1 5 } ;
g = inv( root(sigma) );
p = nrow(sigma) ;
do i = 1 to n ;
t = normal(repeat(seed,f,p)) ;
x = t*g ;
w = x`*x ;
print w ;
end ;
run;
---------------
Chapter 2
---------------
/* Program 2.1*/
title1 j=l 'Output 2.1';
options ls=64 ps=40 nodate nonumber;
data cork;
infile 'cork.dat';
input n e s w;
/* n:north,e:east,s:south,w:west*/
y1=n-s;
y2=e-w;
title2 'Two Dimensional Scatter Plot ';
title3 'Cork Boring Data: Source C.R.Rao (1948)';
proc plot data=cork;
plot n*e;
label n='Direction: North'
e='Direction: East';
run;
title2 'Two Dimensional Scatter Plot ';
title3 'Cork Boring Data: Source C.R.Rao (1948)';
proc plot data=cork;
plot y1*y2;
label y1='Contrast: North-South'
y2='Contrast: East-West';
run;
/* Program 2.2 */
filename gsasfile "prog22.graph";
goptions reset=all gaccess=gsasfile autofeed dev=pslmono;
options ls=64 ps=45 nodate nonumber;
data cork;
infile 'cork.dat';
input n e s w;
title1 j=l 'Output 2.2';
title1 'Three-D Scatter Plot for Cork Data';
title2 j=l 'Output 2.2';
title3 'by weight of cork boring';
title4 'Source: C.R.Rao (1948)';
footnote1 j=l 'N:Cork boring in North'
j=r 'E:Cork boring in East';
footnote2 j=l 'S:Cork boring in South'
j=r 'W:West boring is not shown';
proc g3d data=cork;
goptions horigin=1in vorigin=2in;
goptions hsize=6in vsize=8in;
scatter n*s=e;
run;
/* Program 2.3*/
filename gsasfile "prog23.graph";
goptions gaccess=gsasfile autofeed dev=pslmono;
options ls=64 ps=45 nodate nonumber;
data cork;
infile 'cork.dat';
input n e s w;
c1=n-e-w+s;
c2=n-s;
c3=e-w;
title1 'Three-Dimensional Scatter Plot for Cork Data';
title2 j=l 'Output 2.3';
title3 'Contrasts of weights of cork boring';
title4 'Source: C.R.Rao (1948)';
footnote1 j=l 'C1:Contrast N-E-W+S'
j=r 'C2:Contrast N-S';
footnote2 j=r 'C3:Contrast E-W';
proc g3d data=cork;
goptions horigin=1in vorigin=2in;
goptions hsize=6in vsize=8in;
scatter c1*c2=c3;
run;
/* Program 2.4 */
filename gsasfile "prog24.graph";
goptions reset=all gaccess=gsasfile autofeed dev=pslmono;
goptions horigin=1in vorigin=2in;
goptions hsize=6in vsize=8in;
options ls=64 ps=40 nodate nonumber;
title1 'Scatter Plot Matrix for Cork Data';
title2 'Output 2.4';
data cork;
infile 'cork.dat';
input n e s w;
proc insight data=cork;
run;
/* Program 2.5 */
title1 j=l 'Output 2.5';
filename gsasfile "prog25.graph";
goptions reset=all gaccess=gsasfile autofeed dev=pslmono;
options ls=64 ps=45 nodate nonumber;
data cork;
infile 'cork.dat';
input y1 y2 y3 y4;
/*y1=north, y2=east, y3=south, y4=west */
tree=_n_;
proc transpose data=cork
out=cork2 name=directn;
by tree;
proc gplot data=cork2(rename=(col1=bore));
goptions horigin=1in vorigin=2in;
goptions hsize=6in vsize=8in;
plot bore*directn=tree/
vaxis=axis1 haxis=axis2 legend=legend1;
axis1 label=(a=90 h=1.2 'Depth of Cork Boring');
axis2 offset=(2) label=(h=1.2 'Direction');
symbol1 i=join v=star;
symbol2 i=join v=+;
symbol3 i=join v=A;
symbol4 i=join v=B;
symbol5 i=join v=C;
symbol6 i=join v=D;
symbol7 i=join v=E;
symbol8 i=join v=F;
symbol9 i=join v=G;
symbol10 i=join v=H;
symbol11 i=join v=I;
symbol12 i=join v=J;
symbol13 i=join v=K;
symbol14 i=join v=L;
symbol15 i=join v=M;
symbol16 i=join v=N;
symbol17 i=join v=O;
symbol18 i=join v=P;
symbol19 i=join v=Q;
symbol20 i=join v=R;
symbol21 i=join v=S;
symbol22 i=join v=T;
symbol23 i=join v=U;
symbol24 i=join v=V;
symbol25 i=join v=W;
symbol26 i=join v=X;
symbol27 i=join v=Y;
symbol28 i=join v=Z;
legend1 across=4;
title1 'Profiles of Cork Data';
title2 j=l 'Output 2.5';
title3 'Source: C.R.Rao (1948)';
/*
data plot;
set cork;
array y{4} y1 y2 y3 y4;
do directn=1 to 4;
bore =y(directn);
output;
end;
drop y1 y2 y3 y4;
proc gplot data=plot;
goptions horigin=1in vorigin=2in;
goptions hsize=6in vsize=8in;
plot bore*directn=tree/
vaxis=axis1 haxis=axis2 legend=legend1;
axis1 label=(a=90 h=1.2 'Depth of Cork Boring');
axis2 offset=(2) label=(h=1.2 'Direction');
symbol1 i=join v=star;
symbol2 i=join v=+;
symbol3 i=join v=A;
symbol4 i=join v=B;
symbol5 i=join v=C;
symbol6 i=join v=D;
symbol7 i=join v=E;
symbol8 i=join v=F;
symbol9 i=join v=G;
symbol10 i=join v=H;
symbol11 i=join v=I;
symbol12 i=join v=J;
symbol13 i=join v=K;
symbol14 i=join v=L;
symbol15 i=join v=M;
symbol16 i=join v=N;
symbol17 i=join v=O;
symbol18 i=join v=P;
symbol19 i=join v=Q;
symbol20 i=join v=R;
symbol21 i=join v=S;
symbol22 i=join v=T;
symbol23 i=join v=U;
symbol24 i=join v=V;
symbol25 i=join v=W;
symbol26 i=join v=X;
symbol27 i=join v=Y;
symbol28 i=join v=Z;
legend1 across=4;
title1 'Profiles of Cork Data';
title2 j=l 'Output 2.5';
title3 'Source: C.R.Rao (1948)';
*/
run;
/* Program 2.6 */
filename gsasfile "prog26.graph";
goptions reset=all gaccess=gsasfile autofeed dev=pslmono;
options ls=64 ps=40 nodate nonumber;
title1 'Andrews Function Plot for Cork Data';
title2 j=l 'Output 2.6';
data andrews;
infile 'cork.dat';
input y1-y4;
tree=_n_;
pi=3.14159265;
s=1/sqrt(2);
inc=2*pi/100;
do t=-pi to pi by inc;
z=s*y1+sin(t)*y2+cos(t)*y3+sin(2*t)*y4;
output;
end;
*symbol1 c=black i=join v=plus;
symbol1 i=join v=star;
symbol2 i=join v=+;
symbol3 i=join v=A;
symbol4 i=join v=B;
symbol5 i=join v=C;
symbol6 i=join v=D;
symbol7 i=join v=E;
symbol8 i=join v=F;
symbol9 i=join v=G;
symbol10 i=join v=H;
symbol11 i=join v=I;
symbol12 i=join v=J;
symbol13 i=join v=K;
symbol14 i=join v=L;
symbol15 i=join v=M;
symbol16 i=join v=N;
symbol17 i=join v=O;
symbol18 i=join v=P;
symbol19 i=join v=Q;
symbol20 i=join v=R;
symbol21 i=join v=S;
symbol22 i=join v=T;
symbol23 i=join v=U;
symbol24 i=join v=V;
symbol25 i=join v=W;
symbol26 i=join v=X;
symbol27 i=join v=Y;
symbol28 i=join v=Z;
proc gplot data=andrews;
goptions horigin=1in vorigin=2in;
goptions hsize=6in vsize=8in;
plot z*t=tree/vaxis=axis1 haxis=axis2;
axis1 label=(a=90 h=1.5 f=duplex 'f(t)');
axis2 label=(h=1.5 f=duplex 't')offset=(2);
run;
/* Program 2.7*/
options ls=64 ps=45 nodate nonumber;
data cork;
infile 'newcork.dat';
input tree$ north east south west;
%include biplot; /* Include the macro "biplot.sas" */
%biplot( data = cork,
var = North East South West,
id = TREE, factype=SYM, std =STD );
filename gsasfile "prog27.graph";
goptions reset=all gaccess=gsasfile autofeed dev=pslmono;
proc gplot data=biplot;
plot dim2 * dim1 /anno=bianno frame
href=0 vref=0 lvref=3 lhref=3
vaxis=axis2 haxis=axis1
vminor=1 hminor=1;
axis1 length=6 in order=(-2 to 2 by .5)
offset=(2)
label = (h=1.3 'Dimension 1');
axis2 length=6 in order =(-2 to 2 by .5)
offset=(2)
label=(h=1.3 a=90 r=0 'Dimension 2');
symbol v=none;
title1 h=1.5 f=duplex 'Biplot of Cork Data ';
title2 j=l 'Output 2.7';
title3 h=1.3 f=duplex
'Observations are points, Variables are vectors';
run;
/* Program 2.7 continued; page 1 of biplot.sas */
/* This macro named as "biplot.sas", is called by
Program 2.7 */
%macro BIPLOT(
data=_LAST_,
var =_NUMERIC_,
id = ID,
dim = 2,
factype=SYM,
scale=1,
out=BIPLOT,
anno=BIANNO,
std=MEAN,
pplot=YES);
%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 %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[##,] / (N-1));
Y = Y * diag (1/S);
%end;
*_ _ 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;
Print "Singular values and variance accounted for",,
q [colname={'Singular Values'} format=9.4]
percent [colname={'Percent'} format=8.2]
cum [colname={'cum % '} format = 8.2];
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;
QL= diag(Q ## power);
QR= diag(Q ## (1-power));
A = U * QL;
B = V * QR # scale;
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];
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;
use &data;
read all var{&var} 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_ = scan(id,1);
_name_ = scan(id,2);
/*Annotate observation labels and variable vectors */
data &anno;
set &out;
length function text $8;
xsys='2';
ysys='2';
text=_name_;
if _type_='OBS' then do;
color = 'BLACK';
x = dim1;
y = dim2;
position='5';
function='LABEL ';
output;
end;
if _type_ ='VAR' then do; /*Draw line from*/
color='RED ';
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*/
function='LABEL;
output; /* variable name */
end;
%done:
%mend BIPLOT;
run;
/* newcork.dat */
/* In Cork Boring Data of C.R.Rao (1948) the trees have
have been numbered as T1-T28 */
T1 72 66 76 77
T2 60 53 66 63
T3 56 57 64 58
T4 41 29 36 38
T5 32 32 35 36
T6 30 35 34 26
T7 39 39 31 27
T8 42 43 31 25
T9 37 40 31 25
T10 33 29 27 36
T11 32 30 34 28
T12 63 45 74 63
T13 54 46 60 52
T14 47 51 52 43
T15 91 79 100 75
T16 56 68 47 50
T17 79 65 70 61
T18 81 80 68 58
T19 78 55 67 60
T20 46 38 37 38
T21 39 35 34 37
T22 32 30 30 32
T23 60 50 67 54
T24 35 37 48 39
T25 39 36 39 31
T26 50 34 37 40
T27 43 37 39 50
T28 48 54 57 43
/* Program 2.8*/
title1 j=l 'Output 2.8';
options ls=64 ps=45 nodate nonumber;
data a;
infile 'cork.dat';
input y1-y4;
totn=28.0; /* totn is the number of observations */
proc princomp data=a cov std out=b noprint;
var y1-y4;
data qq;
set b;
dsq=uss(of prin1-prin4);
/*
data qq;
set qq;
ndsq=dsq/2;
proc capability noprint;
qqplot ndsq/gamma(alpha=2);
probplot ndsq/gamma(alpha=2);
run;
*/
proc sort;
by dsq;
data qq;
set qq;
chisq=cinv(((_n_-.5)/ totn),4);
proc plot;
plot dsq*chisq='*';
title2 'Q-Q Plot for Assessing Normality';
label dsq='Mahalanobis D Square'
chisq='Chi-Square Quantile';
run;
/* Program 2.9 */
title1 j=l 'Output 2.9';
options ls=64 ps=45 nodate nonumber;
data a;
infile 'cork.dat';
input y1-y4;
proc princomp data=a cov std out=b noprint;
var y1-y4;
data chiq;
set b;
dsq=uss(of prin1-prin4);
proc sort;
by dsq;
proc means noprint;
var dsq;
output out=chiqn n=totn;
data chiqq;
if(_n_=1) then set chiqn;
set chiq;
chisq=cinv(((_n_-.5)/ totn),4);
if mod(_n_,5)=0 then chiline=chisq;
proc plot;
plot dsq*chisq='-' chiline*chisq='+'/overlay;
title2 Chi-square Q-Q Plot of Squared Distances;
label dsq='Mahalanobis D Square'
chisq='Chi-Square Quantile';
run;
/* Program 2.10 */
title1 j=l 'Output 2.10';
options ls=64 ps=45 nodate nonumber;
data a;
infile 'cork.dat';
input y1-y4;
totn=28.0; /* totn is the no. of observations*/
proc princomp data=a cov std out=b noprint;
var y1-y4;
data chiq;
set b;
tree=_n_;
dsq=uss(of prin1-prin4);
rdsq=(totn/(totn-1))**2*(((totn-2)*dsq/totn)/
(1-(totn*dsq/(totn-1)**2)));
proc sort;
by rdsq;
data chiq;
set chiq;
chisq=cinv(((_n_-.5)/ totn),4);
proc print data=chiq;
var tree rdsq chisq;
proc plot;
plot rdsq*chisq='*';
title2 Chi-square Q-Q Plot of Robust Squared Distances;
label rdsq='Robust Mahalanobis D Square'
chisq='Chi-Square Quantile';
run;
/* Program 2.11 */
filename gsasfile "prog211.graph";
goptions reset=all gaccess=gsasfile autofeed dev=pslmono;
options ls=64 ps=45 nodate nonumber;
title1 'PDF of Bivariate Normal Distribution';
title2 j=l 'Output 2.11';
title3 'Mu_1=0, Mu_2=0, Sigma_1^2=2, Sigma_2^2=1 and Rho=0.5';
data normal;
mu_1=0.0;
mu_2=0.0;
vx1=2;
vx2=1;
rho=.5;
keep x1 x2 z;
label z='Density';
con=1/(2*3.141592654*sqrt(vx1*vx2*(1-rho*rho)));
do x1=-4 to 4 by 0.3;
do x2=-3 to 3 by 0.10;
zx1=(x1-mu_1)/sqrt(vx1);
zx2=(x2-mu_2)/sqrt(vx2);
hx=zx1**2+zx2**2-2*rho*zx1*zx2;
z=con*exp(-hx/(2*(1-rho**2)));
if z>.001 then output;
end;
end;
proc g3d data=normal;
goptions horigin=1in vorigin=2in;
goptions hsize=6in vsize=8in;
plot x2*x1=z;
*plot x2*x1=z/ rotate=30;
run;
/* Program 2.12 */
filename gsasfile "prog212.graph";
goptions reset=all gaccess=gsasfile autofeed dev=pslmono;
options ls=64 ps=45 nodate nonumber;
title1 'Contours of Bivariate Normal Distribution';
title2 j=l 'Output 2.12';
title3 'Mu_1=0, Mu_2=0, Sigma_1^2=2, Sigma_2^2=1 and Rho=0.5';
data normal;
vx1=2;
vx2=1;
rho=.5;
keep x1 x2 z;
label z='Density';
con=1/(2*3.141592654*sqrt(vx1*vx2*(1-rho*rho)));
do x1=-4 to 4 by 0.3;
do x2=-3 to 3 by 0.10;
zx1=x1/sqrt(vx1);
zx2=x2/sqrt(vx2);
hx=zx1**2+zx2**2-2*rho*zx1*zx2;
z=con*exp(-hx/(2*(1-rho**2)));
if z>.001 then output;
end;
end;
proc gcontour data=normal;
goptions horigin=1in vorigin=2in;
goptions hsize=6in vsize=8in;
plot x2*x1=z/
levels=.02 .03 .04 .05 .06 .07 .08;
run;
---------------
Chapter 3
---------------
/* Program 3.1 */
option ls=76 ps=45 nodate nonumber;
data cork;
infile 'cork.dat' firstobs = 1 ;
input north east south west;
y1=north;
y2=east;
y3=south;
y4=west;
/* Hotelling's T-square by creating the differences*/
data cork;
set cork;
dne=y1-y2;
dns=y1-y3;
dnw=y1-y4;
proc glm data=cork;
model dne dns dnw= /nouni;
manova h=intercept;
title1 ' Output 3.1 ';
title2 ' Cork Bore Data: C. R. Rao (1948)' ;
run;
/* Program 3.2 */
option ls=76 ps=45 nodate nonumber;
data cork;
infile 'cork.dat' firstobs=1;
input north east south west;
y1=north;
y2=east;
y3=south;
y4=west;
/* Hotelling's T**2 using m statement*/
proc glm data=cork;
model y1 y2 y3 y4= /nouni;
manova h=intercept
m=y2-y1, y3-y1,y4-y1
mnames=d1 d2 d3;
title1 ' Output 3.2 ' ;
title2 ' Use of M statement for Cork Bore Data:
C. R. Rao (1948) ' ;
/* Testing for equality of bark contents in the opposite
direction, that is, South-North and West-East.*/
/*
proc glm data=cork;
model y1 y2 y3 y4= /nouni;
manova h=intercept
m=y3-y1, y4-y2
mnames=dy3y1 dy4y2;
*/
run ;
/* Program 3.3 */
option ls=76 ps=45 nodate nonumber;
title1 ' Output 3.3 ' ;
data fish;
infile 'fish.dat' firstobs = 1;
input p1 p2 p3 p4 p5 dose wt @@;
y1=arsin(sqrt(p1));
y2=arsin(sqrt(p2));
y3=arsin(sqrt(p3));
y4=arsin(sqrt(p4));
y5=arsin(sqrt(p5));
x1=log(dose);
x2=wt;
proc print data=fish;
var y1 y2 y3 y4 y5 x1 x2;
proc print data=fish;
var p1 p2 p3 p4 p5 dose x2;
title2 'Transformed Fish Data: Srivastava & Carter
(1983, p. 143)';
proc glm data=fish;
model y1 y2 y3 y4 y5=x1 x2/nouni;
manova h=x1 x2/printe printh;
/*
mtest option of proc reg can be used instead of manova
option of proc glm to get the same results;
*/
proc reg data=fish;
model y1 y2 y3 y4 y5=x1 x2;
Model: mtest x1, x2/print;