-
Notifications
You must be signed in to change notification settings - Fork 5
/
A55809.sas
7537 lines (6784 loc) · 233 KB
/
A55809.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
/*-------------------------------------------------------------------*/
/* Univariate and Multivariate General Linear Models: */
/* Theory and Applications Using SAS(R) Software */
/* by Neil H. Timm and Tammy A. Mieczkowski */
/* Copyright(c) 1997 by SAS Institute Inc., Cary, NC, USA */
/* SAS Publications order # 55809 */
/* ISBN 1-55544-987-5 */
/*-------------------------------------------------------------------*/
/* */
/* This material is provided "as is" by SAS Institute Inc. There */
/* are no warranties, express or implied, as to merchantability or */
/* fitness for a particular purpose regarding the materials or code */
/* contained herein. The Institute is not responsible for errors */
/* in this material as it now exists or will exist, nor does the */
/* Institute provide technical support for it. */
/* */
/*-------------------------------------------------------------------*/
/* Questions or problem reports concerning this material may be */
/* addressed to the author: */
/* */
/* Neil H. Timm, Ph.D. */
/* Professor */
/* Department of Psychology in Education */
/* 5C01 Forbes Quadrangle */
/* Pittsburgh PA 15260 */
/* */
/* or by email: */
/* TIMM@vms.cis.pitt.edu */
/* */
/*-------------------------------------------------------------------*/
This file contains example code and data sets that are used in the
book, Univariate and Multivariate General Linear Models: Theory and
Applications Using SAS(R) Software by Neil H. Timm and Tammy A. Mieczkowski.
*************************************************************************
*************************************************************************
EXAMPLE CODE USED IN THIS BOOK
***************************************************
/* The following code appears on page 8, */
/* where it is used to produce output 1.6. */
***************************************************
/* Program 1_6.sas */
/* Program to create a multivariate normal data set */
options ls=80 ps=60 nodate nonumber;
filename app1 '1_6.dat';
title1 'Output 1.6: Generating a Multivariate Normal Data Set';
proc iml;
seed=30195;
z=normal(repeat(seed,50,4));
u={10,20,30,40};
s={3 1 0 0,
1 4 0 0,
0 0 1 4,
0 0 4 20};
a=root(s);
uu=repeat(u`,50,1);
y=(z*a) + uu;
print y;
file app1;
do i=1 to nrow(y);
do j=1 to ncol(y);
put (y[i,j]) 10.2 +2 @;
end;
put;
end;
closefile app1;
quit;
***************************************************
/* The following code appears on page 11, */
/* where it is used to produce output 1.7.1. */
***************************************************
/* Program 1_7_1.sas */
/* Program to create Q-Q plot of data */
options ls=80 ps=60 nodate nonumber;
filename app1 '1_6.dat';
title1 'Output 1.7.1: Q-Q plot of 1.6 Data (y1)';
data ex171;
infile app1;
input y1-y4;
proc sort;
by y1;
proc univariate noprint;
var y1;
output out=stats n=nobs mean=mean std=std;
data quantile;
set ex171;
if _n_=1 then set stats;
i+1;
p=(i - .5) / nobs;
z=probit(p);
normal = mean + z*std;
proc print;
proc corr;
var y1 z;
run;
filename out '1_7_1.cgm';
goptions device=cgmmwwc gsfname=out gsfmode=replace
colors=(black) hsize=5in vsize=4in;
proc gplot data=quantile;
plot y1*z normal*z /overlay frame;
symbol1 v=;
symbol2 i=join v=none l=1;
run;
***************************************************
/* The following code appears on page 13, */
/* where it is used to produce output 1.7.2. */
***************************************************
/* Program 1_7_2.sas */
/* Program to create Q-Q plot of 1/(y1**2) Data */
options ls=80 ps=60 nodate nonumber;
filename app1 '1_6.dat';
title1 'Output 1.7.2: Q-Q Plot of 1/(y1**2)';
data ex172;
infile app1;
input y1-y4;
ty1=1/(y1**2);
proc sort;
by ty1;
proc univariate noprint;
var ty1;
output out=stats n=nobs mean=mean std=std;
data quantile;
set ex172;
if _n_=1 then set stats;
i+1;
p=(i - .5) / nobs;
z=probit(p);
normal = mean + z*std;
proc print;
proc corr;
var ty1 z;
run;
filename out '1_7_2.cgm';
goptions device=cgmmwwc gsfname=out gsfmode=replace
colors=(black) hsize=5in vsize=4in;
proc gplot data=quantile;
plot ty1*z normal*z /overlay frame;
symbol1 v=;
symbol2 i=join v=none l=1;
run;
***************************************************
/* The following code appears on page 15, */
/* where it is used to produce output 1.7.3. */
***************************************************
/* Program 1_7_3.sas */
/* Program to create Q-Q plot of y1 data with Outlier */
options ls=80 ps=60 nodate nonumber;
filename app1 '1_6.dat';
title1 'Output 1.7.3: Q-Q plot of y1 data with an Outlier';
data ex173;
infile app1;
input y1-y4;
if y1 ge 13.7 then y1 = 17;
proc sort;
by y1;
proc univariate noprint;
var y1;
output out=stats n=nobs mean=mean std=std;
data quantile;
set ex173;
if _n_=1 then set stats;
i+1;
p=(i - .5) / nobs;
z=probit(p);
normal = mean + z*std;
proc print;
proc corr;
var y1 z;
run;
filename out '1_7_3.cgm';
goptions device=cgmmwwc gsfname=out gsfmode=replace
colors=(black) hsize=5in vsize=4in;
proc gplot data=quantile;
plot y1*z normal*z /overlay frame;
symbol1 v=;
symbol2 i=join v=none l=1;
run;
***************************************************
/* The following code appears on page 17, */
/* where it is used to produce output 1.7.4. */
***************************************************
/* Program 1_7_4.sas */
/* Program to create Q-Q plot of a dataset */
/* To run this program on your own dataset change */
/* the name of the file in the file=_____statement */
/* and the number of columns in the p=___ statement */
options ls=80 ps=60 nodate nonumber;
%let file = ycondx.dat;
%let p = 3;
/* macro to expand the string of variables that are processed */
%macro expand(cols);
%do j=1 %to &cols;
v&j
%end;
%mend expand;
/* macro to perform Q-Q plotting of the variables */
%macro qq(cols);
%do i=1 %to &cols;
proc sort data=ex174;
by v&i;
proc univariate noprint data=ex174;
var v&i;
output out=stats n=nobs mean=mean std=std;
data quantile;
set ex174;
if _n_=1 then set stats;
k+1;
pr=(k - .5) / nobs;
z=probit(pr);
normal = mean + z*std;
proc print data=quantile;
title "Output 1.7.4: Q-Q plot, variable V&i, &file";
proc corr data=quantile;
var v&i z;
filename out "1_7_4_&i'.cgm";
goptions device=cgmmwwc gsfname=out gsfmode=replace
colors=(black) hsize=5in vsize=4in;
proc gplot data=quantile;
title "Output 1.7.4: Q-Q plot, variable V&i, &file";
plot v&i*z normal*z /overlay frame;
symbol1 v=;
symbol2 i=join v=none l=1;
run;
%end;
%mend qq;
data ex174;
infile "&file";
input %expand(&p);
proc print data=ex174;
title "Output 1.7.4: &file";
%qq(&p);
***************************************************
/* The following code appears on page 22, */
/* where it is used to produce output 1.8.1. */
***************************************************
/* Program 1_8_1.sas */
/* Program to create Chi-Square Plot of Y Data */
/* Data set from 1_6.sas is used, with a column */
/* of observation numbers added to the file */
options ls=80 ps=60 nodate nonumber;
filename app1 '1_6.da2';
title1 'Output 1.8.1: Chi-Square Plot of the 1.6 Dataset';
data ex181;
infile app1;
input tag $ y1 - y4;
label tag = 'id'
y1 = 'var1'
y2 = 'var2'
y3 = 'var3'
y4 = 'var4';
%let id=tag;
%let var=y1 y2 y3 y4;
proc iml;
reset;
start dsquare;
use _last_;
read all var {&var} into y [colname=vars rowname=&id];
n=nrow(y);
p=ncol(y);
r1=&id;
print y;
m=y[ :,];
d=y - j(n,1) * m;
s=d` * d / (n-1);
dsq=vecdiag(d * inv(s) * d`);
r=rank(dsq);
val=dsq; dsq [r, ] = val;
val=r1; &id [r] = val;
z=((1:n)` - .5) / n;
chisq = 2 * gaminv(z,p/2);
result = dsq||chisq;
cl={'dsq' 'chisq'};
create dsquare from result [colname=cl rowname=&id];
append from result [rowname=&id];
finish;
run dsquare;
quit;
proc print data=dsquare;
var tag dsq chisq;
run;
filename out '1_8_1.cgm';
goptions device=cgmmwwc gsfname=out gsfmode=replace
colors=(black) hsize=5in vsize=4in;
proc gplot data=dsquare;
plot dsq*chisq /frame;
symbol1 v=;
run;
***************************************************
/* The following code appears on page 24, */
/* where it is used to produce output 1.8.2. */
***************************************************
/* Program 1_8_2.sas */
/* Program to create Chi-Square Plot */
/* of Residuals from Rhower data */
options ls=80 ps=60 nodate nonumber;
filename rhower 'ycondx.da2';
title1 'Output 1.8.2: Chi-Square Plot of Residuals';
data ex182;
infile rhower;
input tag $ yc1-yc3;
label tag = 'id'
yc1 = 'var1'
yc2 = 'var2'
yc3 = 'var3';
%let id=tag;
%let var=yc1 yc2 yc3;
proc iml;
reset;
start dsquare;
use _last_;
read all var {&var} into y [colname=vars rowname=&id];
n=nrow(y);
p=ncol(y);
r1=&id;
print y;
m=y[ :,];
d=y - j(n,1) * m;
s=d` * d / (n-1);
dsq=vecdiag(d * inv(s) * d`);
r=rank(dsq);
val=dsq; dsq [r, ] = val;
val=r1; &id [r] = val;
z=((1:n)` - .5) / n;
chisq = 2 * gaminv(z,p/2);
result = dsq||chisq;
cl={'dsq' 'chisq'};
create dsquare from result [colname=cl rowname=&id];
append from result [rowname=&id];
finish;
run dsquare;
quit;
proc print data=dsquare;
var tag dsq chisq;
run;
filename out '1_8_2.cgm';
goptions device=cgmmwwc gsfname=out gsfmode=replace
colors=(black) hsize=5in vsize=4in;
proc gplot data=dsquare;
plot dsq*chisq /frame;
symbol1 v=;
run;
***************************************************
/* The following code appears on page 26, */
/* where it is used to produce output 1.9.1. */
***************************************************
/* Program 1_9_1.sas */
/* Program to produce bivariate scatter plot of Rhower Data */
options ls=80 ps=60 nodate nonumber;
filename rhower '5_1.dat';
title 'Output 1.9.1: Bivariate Scatter Plots of Rhower Data';
data ex191;
infile rhower;
input y1-y3 x0-x5;
proc plot;
plot y1*x1;
plot y1*x2;
plot y1*x3;
plot y1*x4;
plot y1*x5;
run;
***************************************************
/* The following code appears on page 29, */
/* where it is used to produce output 1.9.2. */
***************************************************
/* Program 1_9_2.sas */
/* Program to create 3-D Plots of bivariate normal distributions*/
options ls=80 ps=60 nodate nonumber;
title 'Output 1.9.2: Bivariate Normal Distribution';
title2 'with u=(0, 0), var(y1)=3, var(y2)=4, cov(y1,y2)=1, r=.289';
data bivar;
vy1=3;
vy2=4;
r=.289;
keep y1 y2 z;
cons=1/(2*3.14159265*sqrt(vy1*vy2*(1-r*r)));
do y1=-10 to 10 by .2;
do y2=-10 to 10 by .2;
zy1=y1/sqrt(vy1);
zy2=y2/sqrt(vy2);
d=((zy1**2)+(zy2**2)-2*r*zy1*zy2)/(1-r**2);
z=cons*exp(-d/2);
if z > .001 then output;
end;
end;
run;
filename out1 '1_9_2_1.cgm';
goptions device=cgmmwwc gsfname=out1 gsfmode=replace
colors=(black) hsize=6in vsize=5in;
proc g3d data=bivar;
plot y1*y2=z;
run;
/* A Second Plot*/
title2 'with u=(0, 0), var(y1)=3, var(y2)=20, cov=0, r=0';
data bivar2;
vy1=3;
vy2=20;
r=0;
keep y1 y2 z;
cons=1/(2*3.14159265*sqrt(vy1*vy2*(1-r*r)));
do y1=-10 to 10 by .2;
do y2=-10 to 10 by .2;
zy1=y1/sqrt(vy1);
zy2=y2/sqrt(vy2);
d=((zy1**2)+(zy2**2)-2*r*zy1*zy2)/(1-r**2);
z=cons*exp(-d/2);
if z > .001 then output;
end;
end;
run;
filename out2 '1_9_2_2.cgm';
goptions device=cgmmwwc gsfname=out2 gsfmode=replace
colors=(black) hsize=6in vsize=5in;
proc g3d data=bivar2;
plot y1*y2=z;
run;
***************************************************
/* The following code appears on page 32, */
/* where it is used to produce output 1.10. */
***************************************************
/* Program 1_10.sas */
/* Program to calculate Mardia's measure of multivariate */
/* skewness and kurtosis */
options ls=80 ps=60 nodate nonumber;
title 'Output 1.10: Mardias Multivariate Skewness & Kurtosis';
data Rhower;
infile '5_1.dat';
input y1-y3 x0-x5;
proc print data=Rhower;
proc iml;
use Rhower;
v={y1 y2 y3};
w={x0 x1 x2 x3 x4 x5};
read all var v into y;
read all var w into x;
beta=inv(x`*x)*x`*y;
n=nrow(y);
p=ncol(y);
k=ncol(x);
s=(y`*y-y`*x*beta)/(n-k);
s_inv=inv(s);
q=i(n)-x*(inv(x`*x)*x`);
d=q*y*s_inv*y`*q;
b1=(sum(d#d))/(n*n);
b2=trace(d#d)/n;
kappa1= n*b1/6;
kappa2=(b2-p*(p+2))/sqrt(8*p*(p+2)/n);
dfchi=p*(p+1)*(p+2)/6;
pvalskew=1-probchi(kappa1,dfchi);
pvalkurt=2*(1-probnorm(abs(kappa2)));
print s; print s_inv;
print b1; print kappa1; print pvalskew;
print b2; print kappa2; print pvalkurt;
quit;
***************************************************
/* The following code appears on page 34, */
/* where it is used to produce output 1.11. */
***************************************************
/* Program 1_11.sas */
/* Program to compute Box Cox Transformations */
/* To run this program on your own dataset change */
/* the name of the file in the file=____ statement */
/* the number of rows in the n=____ statement and */
/* the number of columns (variables) in the p=___ statement */
options ls=80 ps=60 nodate nonumber mprint;
%let file=c:\exp1_6.dat;
%let n=50;
%let p=1;
/*macro to expand the string of variables that are processed */
%macro expand(cols);
%do j=1 %to &cols;
x&j
%end;
%mend expand;
/*macro to perform the Box-Cox transformation on the data matrix */
%macro loop(cols);
%do i=1 %to &cols;
proc iml;
use matrix;
read all var {x&i};
in=i(&n);
allh={-1.0, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1,
0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3};
one=j(&n,1,1);
c=in-(one*(inv(one`*one))*one`);
do k=1 to 23 by 1;
h=allh[k,1];
xh=x&i##h;
hinv=1/h;
vhinv=j(&n,1,hinv);
y=(xh-one)#vhinv;
my=(one`*y)/&n;
ycy=y`*c*y;
lnx=log(x&i);
slnx=one`*lnx;
ycyn=ycy/&n;
if ycyn > 0 then lhp1=-(&n/2)*log(ycyn);
else lhp1=.;
lhp2=(h-1)*slnx;
if ycyn > 0 then lh=lhp1+lhp2;
else lh=.;
lhs=lhs//lh;
end;
Lambda=allh||lhs;
print, "Lambda and corresponding likelihood for variables x&i",
lambda;
call pgraf(lambda, '*', 'lambda', 'likelihood',
"plot of lambda vs likelihood for variable x&i");
quit;
%end;
%mend loop;
/*input the data and process the macro */
data matrix;
infile "&file";
input (%expand(&p)) (&p*:25.) ;
title "Output 1.11: Box-Cox Transformation plots of &file";
proc print;
%loop(&p)
run;
***************************************************
/* The following code appears on page 51, */
/* where it is used to produce output 2.6. */
***************************************************
/* Program 2_6.sas */
/* Program to perform Multiple Linear Regression Analysis of homicide data */
/* Data from exercise 4, p 117, Kleinbaum, Kupper, Muller */
options ls=80 ps=60 nodate nonumber;
title 'Output 2.6: Multiple Linear Regression Analysis of Homicide data';
data reg;
infile 'c:\2_6.dat';
input city y x1 x2 x3;
label y='homicide rate'
x1='population size'
x2='percent low income'
x3='unemployment rate';
proc print;
proc univariate;
var y;
proc corr;
var y x1 x2 x3;
proc reg;
model y = x1 x2 x3 /vif;
proc reg;
model y = x1 x2 x3 /partial selection = backward;
model y = x2 x3 /vif r collin influence;
paint student. ge 2 or student. le -2 /symbol = 'o';
plot r.*x2 r.*x3 /hplots=2;
plot r.*p. student.*p.;
run;
***************************************************
/* The following code appears on page 59, */
/* where it is used to produce output 2.7. */
***************************************************
/* Program 2_7.sas */
/* Program to perform analysis of a One-Way Design with */
/* Unequal n, using Full Rank model */
options ls=80 ps=60 nodate nonumber;
filename treat '2_7.dat';
title 'Output 2.7: One-Way Design with Unequal n, Full Rank Model';
data treat;
infile treat;
input treat y;
proc print;
proc glm;
class treat;
model y=treat /noint e xpx;
contrast 'treatments' treat 1 0 0 -1,
treat 0 1 0 -1,
treat 0 0 1 -1;
estimate 'treat1-treat4' treat 1 0 0 -1;
estimate 'treat2-treat4' treat 0 1 0 -1;
estimate 'treat3-treat4' treat 0 0 1 -1;
run;
proc glm;
class treat;
model y=treat /noint e xpx;
contrast 'treats123 vs 4' treat -1 -1 -1 3;
estimate 'treats123 vs 4' treat -1 -1 -1 3;
run;
***************************************************
/* The following code appears on page 63, */
/* where it is used to produce output 2.8. */
***************************************************
/* Program 2_8.sas */
/* Program to perform analysis of a nested design with */
/* Unequal n, using Full Rank model */
options ls=80 ps=60 nodate nonumber;
filename nested 'c:\2_8.dat';
title 'Output 2.8: Nested Design with Unequal n, Full Rank Model';
data nest;
infile nested;
input a b y;
label a='course'
b='section';
proc print;
proc glm;
class a b;
model y = a b(a) /noint e;
contrast 'ha_1' a 1 -1;
estimate 'ha_1' a 1 -1 /e;
contrast 'hb_1' b(a) 1 -1 0 0 0;
estimate 'hb_1vs2' b(a) 1 -1 0 0 0 /e;
contrast 'hb_2' b(a) 0 0 1 0 -1,
b(a) 0 0 0 1 -1;
estimate 'hb_1vs3' b(a) 0 0 1 0 -1 /e;
estimate 'hb_2vs3' b(a) 0 0 0 1 -1 /e;
contrast 'hb_1and2' b(a) 1 -1 0 0 0,
b(a) 0 0 1 0 -1,
b(a) 0 0 0 1 -1;
proc glm;
class a b;
model y = a b(a);
run;
***************************************************
/* The following code appears on page 69, */
/* where it is used to produce output 2.9. */
***************************************************
/* Program 2_9.sas */
/* Program to perform analysis of covariance with different slopes and */
/* Unequal n, using Full Rank model */
/* the one-way Unbalanced Intraclass covariance model */
options ls=80 ps=60 nodate nonumber;
filename ex29 'c:\2_9.dat';
title 'Output 2.9: One-way Unbalanced Intraclass covariance model';
data cov;
infile ex29;
input y a z a1 a2 a3 z1 z2 z3;
proc print;
/* Using the full rank model */
proc reg;
model y = a1 a2 a3 z1 z2 z3 /noint;
parallel: mtest z1-z2=0,
z1-z3=0 /print details;
run;
/* Using the less than full rank model */
proc glm;
class a;
model y=a z a*z /e xpx solution;
lsmeans a /stderr pdiff cov;
run;
***************************************************
/* The following code appears on page 79, */
/* where it is used to produce output 3.3. */
***************************************************
/* Program 3_3.sas */
/* Two-Way Factorial Design, Unequal Cell Freq */
/* with and without interaction */
/* data from Timm and Carlson (1975, page 58) */
options ls=80 ps=60 nodate nonumber;
title 'Output 3.3: 2-Way Factorial Design';
data two;
infile 'c:\3_3.dat';
input a b y1 x11 x12 x13 x14 x21 x22 x23 x24;
proc print data=two;
run;
proc reg;
title2 'Unrestricted: 2-Way w/Interaction (univar)--Proc Reg';
model y1 = x11 x12 x13 x14 x21 x22 x23 x24 /noint;
AB: mtest x11-x12-x21+x22=0,
x12-x13-x22+x23=0,
x13-x14-x23+x24=0 /print details;
A: mtest x11+x12+x13+x14-x21-x22-x23-x24=0 /print details;
B: mtest x11+x21-x14-x24=0,
x12+x22-x14-x24=0,
x13+x23-x14-x24=0 /print details;
wtA: mtest .1875*x11+.3125*x12+.1875*x13+.3125*x14-
.2500*x21-.2500*x22-.2500*x23-.2500*x24=0 /print details;
wtB: mtest .4286*x11+.5714*x21-.5556*x14-.4444*x24=0,
.5556*x12+.4444*x22-.5556*x14-.4444*x24=0,
.4286*x13+.5714*x23-.5556*x14-.4444*x24=0 /print details;
run;
proc glm;
title2 'Unrestricted: 2-Way w/Interaction (univar)--Proc GLM';
class a b;
model y1 = a b a*b /ss1 ss2 ss3;
proc glm;
class a b;
model y1 = b a a*b /ss1 ss2 ss3;
run;
proc reg;
title2 'Restricted: 2-Way w/o Interaction (univar)--Proc Reg';
model y1 = x11 x12 x13 x14 x21 x22 x23 x24 /noint;
restrict x11 - x12 - x21 + x22 = 0,
x12 - x13 - x22 + x23 = 0,
x13 - x14 - x23 + x24 = 0;
A: mtest x11+x12+x13+x14-x21-x22-x23-x24=0 /print details;
B: mtest x11+x21-x14-x24=0,
x12+x22-x14-x24=0,
x13+x23-x14-x24=0 /print details;
wtA: mtest .1875*x11+.3125*x12+.1875*x13+.3125*x14-
.2500*x21-.2500*x22-.2500*x23-.2500*x24=0 /print details;
wtB: mtest .4286*x11+.5714*x21-.5556*x14-.4444*x24=0,
.5556*x12+.4444*x22-.5556*x14-.4444*x24=0,
.4286*x13+.5714*x23-.5556*x14-.4444*x24=0 /print details;
run;
proc glm;
title2 'Restricted: 2-Way w/o Interaction (univar)--Proc GLM';
class a b;
model y1 = a b /ss1;
proc glm;
class a b;
model y1 = b a /ss1;
run;
***************************************************
/* The following code appears on page 94, */
/* where it is used to produce output 3.4. */
***************************************************
/* Program 3_4.sas */
/* Latin Square Design, Full Rank Model */
options ls=80 ps=60 nodate nonumber;
title 'Output 3.4: Latin Square Design, Full Rank Model';
data Latin;
infile 'c:\3_4.dat';
input a b c y1 x112 x123 x131 x213 x221 x232 x311 x322 x333;
proc print data=latin;
run;
proc reg;
model y1 = x112 x123 x131 x213 x221 x232 x311 x322 x333 /noint;
restrict x112 - x131 - x213 + x221 - x322 + x333 = 0,
-x112 + x123 - x221 + x232 + x311 - x333 = 0;
A: mtest x112 + x123 + x131 - x311 - x322 - x333 = 0,
x213 + x221 + x232 - x311 - x322 - x333 = 0 /print details;
B: mtest x112 - x131 + x213 - x232 + x311 - x333 = 0,
x123 - x131 + x221 - x232 + x322 - x333 = 0 /print details;
C: mtest -x123 + x131 - x213 + x221 + x311 - x333 = 0,
x112 - x123 - x213 + x232 + x322 - x333 = 0 /print details;
run;
***************************************************
/* The following code appears on page 102 */
/* where it is used to produce output 3.5.1. */
***************************************************
/* Program 3_5_1.sas */
/* Split-Plot (Two-Group Profile) Design */
options ls=80 ps=60 nodate nonumber;
title 'Output 3.5.1: Split-Plot Repeated Measures Design';
data split;
infile 'c:\3_5_1.dat';
input y x111 x121 x131 x112 x122 x132
x211 x221 x231 x212 x222 x232
x311 x321 x331 x312 x322 x332;
proc print data=split;
run;
/* Using full rank model */
proc reg;
title2 'Full Rank Model';
model y = x111 x121 x131 x112 x122 x132 x211 x221 x231 x212
x222 x232 x311 x321 x331 x312 x322 x332 /noint;
restrict x111 - x121 - x112 + x122 = 0,
x121 - x131 - x122 + x132 = 0,
x211 - x221 - x212 + x222 = 0,
x221 - x231 - x222 + x232 = 0,
x311 - x321 - x312 + x322 = 0,
x321 - x331 - x322 + x332 = 0;
A: mtest x111+x121+x131+x112+x122+x132-x211-x221-x231-x212-x222-x232=0,
x211+x221+x231+x212+x222+x232-x311-x321-x331-x312-x322-x332=0
/print details;
B: mtest x111-x131+x112-x132+x211-x231+x212-x232+x311-x331+x312-x332=0,
x121-x131+x122-x132+x221-x231+x222-x232+x321-x331+x322-x332=0
/print details;
AB: mtest x111-x121+x112-x122-x221+x231-x222+x232=0,
x121-x131+x122-x132-x221+x231-x222+x232=0,
x211-x221+x212-x222-x311+x321-x312+x322=0,
x221-x231+x222-x232-x321+x331-x322+x332=0 /print details;
AS: mtest x111+x121+x131-x112-x122-x132=0,
x211+x221+x231-x212-x222-x232=0,
x311+x321+x331-x312-x322-x332=0 /print details;
run;
/* Using less than full rank model */
data split2;
input a b1 b2 b3;
cards;
1 3 4 3
1 2 2 1
2 3 7 7
2 5 4 6
3 3 4 6
3 2 3 5
;
run;
proc glm;
title2 'Less than Full Rank Model';
class a;
model b1 b2 b3 = a;
repeated b 3 profile /nom summary;
run;
***************************************************
/* The following code appears on page 109, */
/* where it is used to produce output 3_5_2. */
***************************************************
/* Program 3_5_2.sas */
/* Split-Plot (Two-Group Profile) Design, Full Rank Model */
/* Timm (1975, p244) data */
options ls=80 ps=60 nodate nonumber;
title 'Output 3.5.2: Two Group Profile Analysis';
data splitb;
infile 'c:\3_5_2.dat';
input grp p1 p2 p3 p4 p5;
proc print data=splitb;
run;
proc glm;
class grp;
model p1 p2 p3 p4 p5 = grp;
repeated position 5 profile /nom summary;
run;
***************************************************
/* The following code appears on page 111, */
/* where it is used to produce output 3.5.3. */
***************************************************
/* Program 3_5_3.sas */
/* Tests of Covariance Structures */
options ls=80 ps=60 nodate nonumber mprint;
title 'Output 3.5.3: Tests of Covariance Structures';
data struct;
infile 'c:\3_5_2.dat';
input grp p1 p2 p3 p4 p5;
proc print data=struct;
run;
proc iml;
use struct;
read all var {p1 p2 p3 p4 p5} where (grp=1) into y1;
read all var {p1 p2 p3 p4 p5} where (grp=2) into y2;
g=2;
n1=nrow(y1);
n2=nrow(y2);
n=n1+n2;
p=ncol(y1);
df1=n1-1;
df2=n2-1;
s1=(y1`*(i(n1)-(1/n1)*j(n1,n1))*y1)/df1;
s2=(y2`*(i(n2)-(1/n2)*j(n2,n2))*y2)/df2;
s=(1/(n-g))*(df1*s1+df2*s2);
/* the hypothesis test matrix a` */
ap={1 -1 0 0 0,
0 1 -1 0 0,
0 0 1 -1 0,
0 0 0 1 -1};
a=ap`;
q=nrow(ap);
call gsorth(as,t,lindep,a);
asp=as`;
as1a=as`*s1*as;
as2a=as`*s2*as;
asa=(1/(n-g))*(df1*as1a+df2*as2a);
print "A` and A*`", ap asp;
print s1 s2;
print "Reduced Covariance Matrices", as1a as2a;
print "Reduced Pooled Covaraince Matrix", asa;
/* Step 1 : Test of Equality of the Reduced Covariance Matrices */
m1=(n-g)*(log(det(asa)));
m2=df1*(log(det(as1a)))+df2*(log(det(as2a)));
m=m1-m2;
print m;
e1=(2*q*q+3*q-1)/(6*(q+1)*(g-1));
e2=(1/df1)+(1/df2)-(1/(n-g));
e=e1*e2;
print e;
xb=(1-e)*m;
v1=(q*(q+1)*(g-1))/2;
probxb=1-probchi(xb,v1);
print "Test of Equality of Reduced Covariance Matrices", xb v1;
print "Probability of Xb with df=v1", probxb;
print "The above test works well for ni>20 and q<6 and g<6";
eo1=(1/(df1*df1))+(1/(df2*df2))-(1/((n-g)*(n-g)));
eo2=(q-1)*(q+2)/(6*g-6);
eo=eo2*eo1;
vo=(v1+2)/(eo-(e*e));
fb=((1-e-(v1/vo))/v1)*m;
probfb=1-probf(fb,v1,vo);
print "Test of Equality of Reduced Covariance Matrices", fb v1 vo;