forked from friendly/SAS-macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnifyPow.sas
4159 lines (3579 loc) · 123 KB
/
UnifyPow.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
/*
------------------------------------------------------------------------
FOR INFORMATION AND LATEST VERSION: http://www.bio.ri.ccf.org/power.html
------------------------------------------------------------------------
UnifyPow.sas, version 97.10.beta
1997 Copyright (c) by Ralph G. O'Brien, PhD
Department of Biostatistics and Epidemiology
Cleveland Clinic Foundation
Cleveland, OH 44195
Voice: 216-445-9451
Fax: 216-444-8023
This single file contains all components of UnifyPow. You simply put
it in an appropriate directory on your system and %include it in your
SAS run. It is distributed so that it runs in the way I have been
demonstrating in my workshops. Technically, it is not a true macro
in this form, but there are instructions soon below explaining how
to easily convert it to one. (There is usually no compelling reason
to do this.)
-------------------------------------------
RUNNING UnifyPow AS DISTRIBUTED (NON-MACRO)
-------------------------------------------
Regardless of the platform, to run this "non-macro" version of
UnifyPow and get the standard tables, simply follow this template:
----------------------------------------------------------------------
options ls=78 nosource2;
%let UnifyPow = file_specification;
%include "&UnifyPow";
title1 "A Title for Problem 1.";
datalines;
{set of UnifyPow statements here}
%tables;
%include "&UnifyPow";
title1 "A Title for Problem 2.";
datalines;
{second set of UnifyPow statements here}
%tables;
----------------------------------------------------------------------
--------------------------------
THE FILE SPECIFICATION STATEMENT
--------------------------------
UNIX is my everyday platform); I use something like:
----------------------------------------------------------------------
%let UnifyPow = /home/robrien/SASmacros/UnifyPow9710.sas;
----------------------------------------------------------------------
Windows 95 is pretty mysterious to me, but I put UnifyPow9710.sas
in the SAS directory and use something like this:
----------------------------------------------------------------------
%let UnifyPow = UnifyPow9710.sas;
----------------------------------------------------------------------
-----------------------
MAKING UnifyPow A MACRO
-----------------------
To make UnifyPow a macro, search for "MAKE MACRO" in the
code below to find instructions for changing the few lines needed.
-----------------------------------------------
RUNNING UnifyPow AFTER YOU HAVE MADE IT A MACRO
-----------------------------------------------
Regardless of the platform, to run the macro version of UnifyPow
and get the standard tables, simply follow this template:
----------------------------------------------------------------------
options ls=78 nosource2;
%include file_specification; {described above}
title1 "A Title for Problem 1.";
%readspec; datalines;
{set of UnifyPow statements here}
%UnifyPow;
title1 "A Title for Problem 2.";
%readspec; datalines;
{second set of UnifyPow statements here}
%UnifyPow;
----------------------------------------------------------------------
---------------------------------------------------
EXPERIENCED SAS USERS MAY CUSTOMIZE UnifyPow OUTPUT
---------------------------------------------------
All results from each UnifyPow problem are stored in a temporary SAS
data set called PowData. Knowing this, experienced SAS users may
easily customize their output by merging results from two or more
problems and by using their own PROC TABULATE or SAS/GRAPH code. The
examples.sas file I distribute should have one or two examples of
this. I recommend that you first examine the structure of PowData
by just seeing what it holds.
Non-macro version:
----------------------------------------------------------------------
options ls=78 nosource2;
%let UnifyPow = file_specification;
%include "&UnifyPow";
datalines;
{set of UnifyPow statements here}
proc print data=PowData;
----------------------------------------------------------------------
Macro version:
----------------------------------------------------------------------
options ls=78 nosource2;
%include file_specification;
%readspec; datalines;
{set of UnifyPow statements here}
%UnifyPow;
proc print data=PowData;
----------------------------------------------------------------------
----------------------
UnifyPow LEGAL NOTICES
----------------------
THIS SOFTWARE IS MADE AVAILABLE "AS IS".
UnifyPow is a trademark of Ralph G. O'Brien. No commercial use
of this trademark may be made without prior written permission of
Ralph O'Brien.
All UnifyPow software and its included text and accompanying
documentation are Copyright 1997 by Ralph G. O'Brien.
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee to Ralph
O'Brien is hereby granted, provided that these legal notices appear in
all copies and supporting documentation, that the name "UnifyPow" is
retained, and that the names of Ralph O'Brien and the Cleveland Clinic
Foundation are not used in advertising or publicity pertaining to
distribution of the software without the specific, written prior
permission of Ralph O'Brien.
Although the above trademark and copyright restrictions do not convey
the right to redistribute derivative works, Ralph O'Brien encourages
unrestricted distribution of patches or ancillary code which can be
applied to or used in conjunction with Ralph O'Brien's distribution.
If this software is modified for local use, please denote this on all
modified versions of the software by appending the letter "L" to the
current version number and by noting the changes in the code itself
and in the associated documentation.
RALPH G. O'BRIEN AND THE CLEVELAND CLINIC FOUNDATION DISCLAIM
ALL WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS SOFTWARE,
INCLUDING WITHOUT LIMITATION ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN NO
EVENT SHALL RALPH G. O'BRIEN OR THE CLEVELAND CLINIC FOUNDATION
BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT (INCLUDING
NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-----------------------------------------
REPORTING PROBLEMS AND MAKING SUGGESTIONS
-----------------------------------------
UnifyPow's computations are checked thoroughly using multiple
methods: comparing its results to those obtained by using other
software, to those in published tables and examples, and to
results obtained from Monte Carlo simulation. No true bugs have
yet been reported to me about UnifyPow's numerical accuracy, but
no software can be said to be totally free of problems. I really
do appreciate knowing if you encounter difficulties or have
suggestions for improvements. Most of the additions to UnifyPow
come about this way. On the other hand, I cannot promise to
address everyone's queries, especially those that are mostly
related to consulting advice. I correspond best via email.
Do not modify this code without obtaining written permission from Ralph
O'Brien. If changes are needed for some purpose, have the wisdom and
courtesy to consult him.
---------------
CITING THE WORK
---------------
If you use this work, please cite it--something like
... using UnifyPow, a macro for the SAS System (O'Brien, 1997).
This reference is
O'Brien, RG (1997), "UnifyPow: A SAS Macro for Sample-Size Analysis,"
Proceedings of the 22nd SAS Users Group International Conference,
Cary NC: SAS Institute, 1353-1358.
Another key reference for this work is:
O'Brien, RG and Muller, KE (1993), "Unified Power Analysis
for t Tests through Multivariate Hypotheses," in Edwards, EK.
(Ed.), Applied Analysis of Variance in the Behavioral Sciences,
New York: Marcel Dekker, pp. 297-344.
Actually, that chapter covers freeware called OneWyPow.sas and other
%include modules that are now unified in this macro. UnifyPow offers a
simpler interface and a far larger set of methods.
---------------------------------------
OBTAINING FREEWARE SAS CODE AND UPDATES
---------------------------------------
UnifyPow is freeware distributed primarily via the anonymous ftp
site at the Department of Biostatistics and Epidemiology at the
Cleveland Clinic Foundation. Downloading new versions periodically
ensures that you are getting the latest version I feel is safe for
public distribution.
You can use your web browser (e.g., Netscape Navigator/Communicator)
to get the files. Just go to
http://www.bio.ri.ccf.org/power.html
and follow the instructions.
You can also download the files directly from the relevant ftp address,
ftp.bio.ri.ccf.org
and directory,
UnifyPow.all
in the root directory (not in /pub). If you know how to obtain
documents via anonymous ftp, this is all the information you need.
------------------------
UnifyPow.sas SOURCE CODE
------------------------
*/
%macro tables;
/*
All kinds of tables for displaying results.
Easy to customize.
*/
/*
First do the trimming of results for one- or two-tailed tests.
Both types are always computed when appropriate, so when I
decided to add the TAILS/SIDES option it was just much easier to strip
the results, rather than prevent their computation to begin
with. This is wasteful of computing cycles, so someday I should redo
this.
*/
%TrimTail;
/*
The tables are constructed so that they will handle either "Power" or
"NTotal" (TotalN or TotalPairs or PairsTotal) statments.
*/
%if &ResltVar = power %then %do;
%let SpecVar = NTotal;
%let result = %str(Power="Power"*mean=" ");
%let spec = %str(Ntotal="Total N");
%if &ProbType = McNemar %then
%let spec = %str(Ntotal="Pairs");
%if &ProbType = PairedMu %then
%let spec = %str(Ntotal="Total Pairs");
%let format = 4.3;
%end;
%if &ResltVar = NTotal %then %do;
%let SpecVar = NomPower;
%let result = %str(Ntotal="Total N"*mean=" ");
%if &ProbType = McNemar %then
%let result = %str(Ntotal="Pairs"*mean=" ");
%if &ProbType = PairedMu %then
%let result = %str(Ntotal="Total Pairs"*mean=" ");
%let spec = %str(NomPower="Minimum Power");
%let format = 6.0;
%end;
%if %index(&TablType, WlcxnPow) > 0 %then
%let AddRow2 = %str(* parent = "Parent");
%if &TablType = GnrlPow %then %do;
/* all alphas in one table, no SD or parent */
proc tabulate format = &format order=data;
class alpha effctitl testtype &SpecVar ProbStmt;
var &ResltVar;
table
ProbStmt="Scenario:",
effctitl="Method" * testtype="Type",
alpha * &spec * &result
/rtspace=28;
%if &fnote=2pi %then %do;
footnote1
"*The Approximate Unconditional corresponds to the Ordinary Pearson";
footnote2
"chi-square test for a 2 x 2 table. Technically, the method here ";
footnote3
"uses a regular t test with Y = 0 (no) or 1 (yes), which is known ";
footnote4
"to offer more accurate p-levels and can be done with any standard ";
footnote5
"t-test routine. See D'Agostino, Chase, and Belanger (1988), ";
footnote6
"American Statistician, 1988, 42:198-202. ";
footnote7 " ";
footnote8
"**The Exact Unconditional corresponds to the test proposed by ";
footnote9
"Suissa and Shuster (1985), J Royal Stat Soc A, 148:317-327). ";
run;
%end; /* 2pi */
run;
footnote1; footnote2; footnote3; footnote4; footnote5;
footnote6; footnote7; footnote8; footnote9;
%end; /* GnrlPow */
%if &TablTyp3 = Pi1Specl %then %do;
/* tables of TruAlpha and critical values for 1-group binomial problem */
proc tabulate format=6.0 order=data;
class SpcTitl3 effctitl alpha testtype &SpecVar;
var TruAlpha LoCrit HiCrit;
table SpcTitl3 = " ",
effctitl="Method" * &spec * testtype="Type",
alpha*(TruAlpha = "Actual Alpha"*mean=" "*F=6.3
LoCrit="Lower Crit Value"*mean=" "
HiCrit="Upper Crit Value"*mean=" ")
/rtspace = 28;
footnote1
"These critical values are part of the rejection region.";
footnote2
"The note above describes how they are set.";
run;
footnote1; footnote2;
%end; /* pi1Specl */
%if &TablType = tPow %then %do;
/* all alphas in one table, does not give parent */
proc tabulate format = &format order=data;
class alpha effctitl testtype SD &SpecVar ProbStmt;
var &ResltVar;
table
ProbStmt="Scenario:"* effctitl=" ",
alpha= "Alpha" * testtype="Type",
SD="Standard Deviation" * &spec * &result
/rtspace=28;
%end; /* tpow */
%if &TablType = WlcxnPow %then %do;
proc tabulate format = 4.3 order=data;
class parent SD ProbStmt;
var Wp1 Wp2 W&p3or4;
table ProbStmt="Scenario:", parent="Parent",
mean="Nonparametric Moments"*(Wp1="p1" Wp2="p2" W&p3or4="&p3or4")*
SD="&SDType" / rtspace = 11;
/* separate tables for each alpha, gives parent */
proc tabulate format= &format order=data;
class alpha effctitl testtype parent SD &SpecVar ProbStmt;
var &ResltVar;
table
ProbStmt="Scenario:"*alpha="Alpha:",
effctitl="Method"* testtype="Type" *parent="Parent",
SD="Standard Deviation" * &spec * &result
/rtspace=28;
%end; /* TablType = WlcxnPow */
%if &TablType = 1or2WlcxnPow %then %do;
%if &TblMmnts = yes %then %do;
%if &SDtype ne none %then %do;
proc tabulate format=4.3 order=data;
class parent SD ProbStmt;
var Wp1 Wp2 W&p3or4;
table ProbStmt="Scenario:", parent="Parent",
mean="Nonparametric Moments"*(Wp1="p1" Wp2="p2" W&p3or4="&p3or4")*
SD="&SDType" / rtspace = 11;
%end; /* SDtype ne none (1) */
%if &SDType = none %then %do;
proc tabulate format=4.3 order=data;
class parent ProbStmt;
var Wp1 Wp2 W&p3or4;
table ProbStmt="Scenario:", parent = "Parent",
mean="Nonparametic Moments" * (Wp1="p1" Wp2="p2" W&p3or4="&p3or4")
/rtspace=11;
%end; /* SDType = none (1) */
%end; /* TblMmnts = yes */
%if &SDtype ne none %then %do;
/* separate tables for each alpha, gives parent */
proc tabulate format= &format order=data;
class alpha effctitl testtype parent SD &SpecVar ProbStmt;
var &ResltVar;
table ProbStmt="Scenario:"*alpha="Alpha:",
effctitl="Method"* testtype="Type" *parent="Parent",
SD="&SDtype" * &spec * &result
/rtspace=28;
%end; /* SDtype ne none (2) */
%if &SDtype = none %then %do;
proc tabulate format=&format order=data;
class alpha effctitl &DistList testtype parent &SpecVar ProbStmt;
var &ResltVar;
table ProbStmt="Scenario:" &OthScen,
effctitl=" Method"* testtype="Type",
alpha="Alpha" * &spec * &result
/rtspace=28;
%end; /* SDType = none (2) */
%end; /* TablType = 1or2WlcxnPow */
%if &TablType = WlcxMuPpow %then %do;
data PowDatBB; set PowData; if Wp1 ne .;
proc tabulate data=PowDatBB format=6.3 order=data;
class parent SDP Trials ProbStmt;
var Wp1 Wp2 W&p3or4;
table ProbStmt="Scenario:", parent="Parent",
mean="Nonparametric Moments"*(Wp1="p1" Wp2="p2" W&p3or4="&p3or4")*
SDP="SD(P)" * Trials="&TrlName" / rtspace = 11;
/* separate tables for each alpha, gives parent */
proc tabulate data=PowData format=&format order=data;
class alpha effctitl testtype parent SDP Trials &SpecVar ProbStmt;
var &ResltVar;
table ProbStmt="Scenario:"*alpha="Alpha:",
effctitl="Method"* testtype="Type" *parent="Parent",
SDP="SD(P)" * Trials="&TrlName" * &spec * &result
/rtspace=28;
%end; /* TablType = WlcxMuPpow */
%if &TablType = WlcxPaMuPow %then %do;
proc tabulate format=4.3 order=data;
class parent parent SDMult Corr ProbStmt;
var Wp1 Wp2 W&p3or4;
table ProbStmt="Scenario:",
parent="Parent",
SDMult="x SD Multiplier" * Corr="Corr(Y1, Y2)" *
mean="Nonparametric Moments"*(Wp1="p1" Wp2="p2" W&p3or4="&p3or4")
/ rtspace = 11;
proc tabulate format=&format order=data;
class alpha effctitl testtype parent SDMult Corr &SpecVar ProbStmt;
var &ResltVar;
table ProbStmt="Scenario:" * alpha= "Alpha",
effctitl="Method" * testtype="Type" * parent="Parent",
SDMult="x SD (SD Multiplier)" * Corr="Corr(Y1, Y2)" * &spec *
&result
/rtspace=28;
%end; /* TablType = WlcxPaMuPow */
%if &TablType = tPaMuPow %then %do;
proc tabulate format=&format order=data;
class alpha effctitl testtype SDMult Corr &SpecVar ProbStmt;
var &ResltVar;
table ProbStmt="Scenario:"* effctitl=" ",
alpha= "Alpha" * testtype="Type",
SDMult="x SD (SD Multiplier)" * Corr="Corr(Y1, Y2)" * &spec * &result
/rtspace=28;
%end; /* TablType = tPaMuPow */
%if &TablType = FPaMuPow %then %do;
proc tabulate format=&format order=data;
class alpha effctitl testtype SDMult Corr &SpecVar ProbStmt;
var &ResltVar;
table ProbStmt="Scenario:"* effctitl=" ",
alpha= "Alpha" * testtype="Type",
SDMult="x SD (SD Multiplier)" * Corr="Corr(Y1, Y2)" * &spec * &result
/rtspace=28;
%end; /* TablType = FPaMuPow */
%if &TablType = tmuP_Pow %then %do;
proc tabulate format=&format order=data;
class alpha effctitl testtype SDP Trials &SpecVar ProbStmt;
var &ResltVar;
table ProbStmt="Scenario:"* effctitl=" ",
alpha= "Alpha" * testtype="Test Type",
SDP="SD(P)" * Trials="&TrlName" * &spec * &result
/rtspace=28;
%end; /* TablType = tmuP_Pow */
%if &TablType = FMuP_Pow %then %do;
proc tabulate format=&format order=data;
class alpha effctitl testtype SDP Trials &SpecVar ProbStmt;
var &ResltVar;
table ProbStmt="Scenario:",
effctitl=" " * alpha= "Alpha" * testtype="Test Type",
SDP="SD(P)" * Trials="&TrlName" * &spec * &result
/rtspace=28;
%end; /* TablType = FMuP_Pow */
%if &TablType = FPow %then %do;
/* condensed tables for F tests */
proc tabulate format = &format order=data;
class alpha effctitl testtype SD &SpecVar ProbStmt;
var &ResltVar;
table ProbStmt="Scenario:",
effctitl="Test" * alpha="Alpha" * testtype="Type",
SD="Standard Deviation" * &spec * &result
/rtspace=28;
%end; /* TablType = FPow */
%if &TablType = 1betaOLS %then %do;
proc tabulate format=&format order=data;
class alpha BetaWt tolernce SDx testtype SD &SpecVar;
var &ResltVar;
table alpha="Alpha:"*BetaWt="Beta Coefficient:",
tolernce="Tol(X)" *SDx="SD(X)" * testtype="Type",
SD="SD(Resid)" * &spec * &result
/rtspace=28;
%end; /* TablType = 1betaOLS */
%if ((&TablTyp2 = FindNPow) and
(&TablType ne none)) %then %do;
/* Tables the powers after doing FindN problem */
proc tabulate format = 4.3 order=data;
class TitlDlta alpha effctitl testtype NomPower parent ProbStmt;
var power;
table TitlDlta="",
effctitl= "Test" * testtype="Type" &AddRow2,
alpha * NomPower="Minimum Power" * Power="Power"*mean=" "
/rtspace=28;
%end; /* FindNPow */
%if &TablType = none %then %do;
%put ..............................................;
%put The user specified that no tables be produced.;
%put ..............................................;
%end; /* TablType = none */
run;
%mend tables;
%macro readspec (fname=temp);
/* Simple utility for input used when UnifyPow is a macro. */
data _null_; file &fname; input; put _infile_;
%mend readspec;
%macro FindN (GetPow, Cntinu);
/*
Secant method to find Ntotal such that power(Ntotal) = NomPower.
Starting values are carefully found to ensure convergence.
GetPow is the statement label to begin power computation.
Cntinu is the statement label to go to after convergence.
*/
if Step = 1 then do;
FindingN = 1;
MinPossN = NumGrps + 1;
if rhoProb then MinPossN = NumGrps*4;
Ntotal = MinPossN;
Step = 2;
go to &GetPow;
end;
if Step = 2 then do;
if (Ntotal = MinPossN) and (power > NomPower) then do;
put //
"WARNING: Power exceeds " NomPower "using smallest possible NTotal.";
put
" Are your specifications correct?";
go to &Cntinu;
end;
Diff = power - NomPower;
if Diff < 0 then do;
Ntotal = 2*Ntotal;
OldDifLo = Diff;
go to &GetPow;
end;
NtotHi = Ntotal;
DiffHi = Diff;
NtotLo = Ntotal/2;
DiffLo = OldDifLo;
Ntotal = NtotLo + (NtotHi - NtotLo)/(1 - DiffHi/DiffLo);
if not(Ntotal > 0) then do; %TroubMsg; end;
Step = 3;
go to &GetPow;
end;
if Step = 3 then do;
Diff = power - NomPower;
if abs(Diff) < .00049 then do;
FindingN = 0;
go to &Cntinu;
end;
if Diff < 0 then do;
NtotLo = Ntotal;
DiffLo = Diff;
end;
else do;
NtotHi = Ntotal;
DiffHi = Diff;
end;
Ntotal = NtotLo + (NtotHi - NtotLo)/(1 - DiffHi/DiffLo);
if not(Ntotal > 0) then do; %TroubMsg; end;
go to &GetPow;
end;
%mend FindN;
%macro TroubMsg;
put // "WARNING: Despite extensive testing, the iterative method";
put " to find NTtotal for a specified power has failed";
put " to converge. NTotal was set to missing.";
put / " Please report this to";
put " Ralph O'Brien";
put " [email protected]";
Ntotal = .;
power = .;
go to &Cntinu;
%mend TroubMsg;
%macro FindNBnl (GetPow, Cntinu);
/*
Finds minimum N = Ntotal for binomial such that power(N) > NomPower.
GetPow is the statement label to begin power computation.
Cntinu is the statement label to go to after convergence.
*/
if Step = 1 then do;
FindingN = 1;
N_try = 1;
Step = 2;
go to &GetPow;
end;
if Step = 2 then do;
if (N_try = 1) and (power > NomPower) then do;
put //
"WARNING: Power of " NomPower "is reached with NTotal = 1.";
put
" Are your specifications correct?";
FindingN = 0;
go to &Cntinu;
end;
if power < NomPower then do;
N_try = N_try*2;
go to &GetPow;
end;
N_tryHi = N_try;
N_tryLo = N_try/2;
if (N_tryHi - N_tryLo) < 5 then Step = 4;
else do;
Step = 3;
N_try = N_tryHi - .5*(N_tryHi - N_tryLo);
go to &GetPow;
end;
end;
if Step = 3 then do;
if power > NomPower then N_tryHi = N_try;
else N_tryLo = N_try;
if (N_tryHi - N_tryLo) < 5 then do;
Step = 4;
N_try = N_tryLo + 1;
go to &GetPow;
end;
else do;
N_try = N_tryHi - .5*(N_tryHi - N_tryLo);
go to &GetPow;
end;
end;
if Step = 4 then do;
if power > NomPower then do;
FindingN = 0;
go to &Cntinu;
end;
N_try = N_try + 1;
go to &GetPow;
end;
%mend FindNBnl;
%macro TrimTail;
/* Keeps only results specified by TAILS/SIDES statement */
data PowData; set PowData;
if &KeepTail = 3 then go to outTrimT;
if tails = &KeepTail;
outTrimT:
%mend TrimTail;
%macro CheckWp (Wp1, Wp2, Wp3or4);
if ((NumGrps = 1) and ((&Wp2 < &Wp1) or (&Wp2 < &Wp3or4))) or
((NumGrps = 2) and ((&Wp2 ge &Wp1) or (&Wp3or4 ge &Wp1))) then do;
DoLH = 0;
DoNoe = 1;
if NumGrps = 1 then put
// "WARNING: A situation makes p2 < p1 or p2 < p4.";
if NumGrps = 2 then put
// "WARNING: A situation fails to make p2 < p1 and p3 < p1.";
put " Only Noether's method will be used."
/ " Use results with caution. Perhaps try different"
/ " numbers of items.";
end;
%mend CheckWp;
%macro SDBetBnl (muP, SDP, Trials, p_beta, q_beta, SDBB);
/*
SDBB is std dev of Beta-Binomial outcome, Y/Trials, defined as follows.
Subject's "true score" success rate, P, is distributed as standard
beta random variable with mean MuP and std dev SDP. A check is made
that this beta density is unimodal.
Given P, Y is dist'd as binomial(Trials, P). Y is called a beta-binomial
random variable, also known as the negative hypergeometric.
The subject's success probability, Y/Trials, is the random variable of interest.
*/
/*
Theory in Johnson, Kotz, Balakrishnan, [Continuous Univariate
Distributions, Vol. 2, 2nd Ed., 1995], Equations 25.28 & 25.29.
Note: JKB's "p" = p_beta and "q" = q_beta.
*/
pPLUSq = &muP*(1 - &muP)/&SDP**2 - 1;
&p_beta = (&muP**2)*(1 - &muP)/(&SDP**2) - &muP;
&q_beta = pPLUSq - &p_beta;
if (&p_beta lt 1) or (&q_beta lt 1) then do;
put // "ERROR: With mu(P) = " &muP "and SD(P) = " &SDP;
put " the resulting beta distribution for P is not unimodal.";
stop; end;
/*
Theory in Johnson, Kotz, and Kemp [Univariate Discrete
Distributions, 2nd Ed., 1992], Equation 6.48.
Note: JKK's "alpha" = p_beta and "beta" = q_beta.
*/
&SDBB = &Trials*&p_beta*&q_beta*(&p_beta+&q_beta+&Trials);
&SDBB = &SDBB/(((&p_beta+&q_beta)**2)*(&p_beta+&q_beta+1));
&SDBB = sqrt(&SDBB)/&Trials;
%mend SDBetBnl;
%macro ProbBBnl(X, p_beta, q_beta, N, PrX);
/*
Computes probabilities for beta-binomial random variable X:
PrX = Prob[X = x | P, N] where N fixed, P distd as beta(p_beta, q_beta).
Uses (6.18) in Johnson, Kotz, and Kemp [Univariate Discrete
Distributions, 2nd Ed., 1992].
*/
%NBnlCoef(&p_beta, &X, result1);
%NBnlCoef(&q_beta, &N-&X, result2);
%NBnlCoef(&p_beta+&q_beta, &N, result3);
&PrX = (result1/result3)*result2;
%mend ProbBBnl;
%macro NBnlCoef (NBn, NBr, NBresult);
/*
Finds /-n\
\ r/, using Eq. 1.10 in Johnson, Kotz, Kemp, 1992.
*/
%BnmlCoef(&NBn+&NBr-1, &NBr, &NBresult);
&NBresult = ((-1)**(&NBr))*&NBresult;
%mend NBnlCoef;
%macro BnmlCoef(BCn, BCr, n_over_r);
/*
Finds /n\
\r/, using gamma function.
*/
&n_over_r = log(gamma(&BCn+1)) - log(gamma(&BCn-(&BCr)+1))
- log(gamma(&BCr+1));
&n_over_r = exp(&n_over_r);
%mend BnmlCoef;
%macro SetCumBB(p, q, Trials, CumDist);
/*
Sets the cumulative distribution function of a beta-binomial with
parameters P ~ beta(p, q) and Y ~ binomial(Trials, P).
*/
%let i = i_CumDst;
do &i = 0 to &Trials;
%ProbBBnl(&i, &p, &q, &Trials, prob_i);
if &i = 0 then &CumDist{0} = prob_i;
else &CumDist{&i} = &CumDist{&i-1} + prob_i;
end;
%mend SetCumBB;
%macro StWlBB2G(p_betaX, q_betaX, p_betaY, q_betaY, Trials, Wp1, Wp2, Wp3);
/*
Set p1, p2, p3 parameters for 2-group Wilcoxon problem for beta-binomial
parent. Lots of ties require adjustments:
p1 = Pr[Y > X] + .5*P[Y = X],
p2 = Pr[{Yi > Xk} and {Yi' > Xk}] + 0.50*Pr[{Yi = Xk} and {Yi' > Xk}]
+ 0.25*Pr[{Yi = Xk} and {Yi' = Xk}],
p3 = Pr[{Yi > Xk} and {Yi > Xk'}] + 0.50*Pr[{Yi = Xk} and {Yi > Xk'}]
+ 0.25*Pr[{Yi = Xk} and {Yi = Xk'}].
*/
%let pX = pX_BB2G; %let qX = qX_BB2G;
%let pY = pY_BB2G; %let qY = qY_BB2G;
%let muX = muX_BB2G; %let muY = muY_BB2G;
&pX = &p_betaX; &qX = &q_betaX; &pY = &p_betaY; &qY = &q_betaY;
redoBB2G:
&muX = &pX/(&pX+&qX); &muY = &pY/(&pY+&qY);
%SetCumBB(&pX, &qX, &Trials, cumX);
%SetCumBB(&pY, &qY, &Trials, cumY);
%let X = X_SetCBB; %let Y = Y_SetCBB;
do &Y = 0 to &Trials;
if &Y = 0 then &Wp1 = .5*cumY{0}*cumX{0};
else &Wp1 = &Wp1 + (cumY{&Y} - cumY{&Y-1})*(0.5*(cumX{&Y} - cumX{&Y-1})
+ cumX{&Y-1});
end;
/* check if Wp1 makes sense */
&Wp1 = round(&Wp1, .001);
if (&muX le &muY) and (&Wp1 < 0.500) or
(&muX ge &muY) and (&Wp1 > 0.500) then do;
put // "WARNING: The specified beta-binomial distributions";
put " are such that power computations for the";
put " Wilcoxon test would be misleading. Try";
put " increasing the effect size.";
&Wp1 = .; &Wp2 = .; &Wp3 = .;
go to outBB2G;
end;
/* UnifyPow is set only for Wp1 > 0.50 */
else if (&muX ge &muY) and (&Wp1 < 0.500) then do;
/*"Reflect" problem by switching X and Y */
temp = &pX; &pX = &pY; &pY = temp;
temp = &qX; &qX = &qY; &qY = temp;
go to redoBB2G;
end;
do &X = 0 to &Trials;
if &X = 0 then
&Wp2 = cumX{0}*(.25*cumY{0}**2 + .5*2*cumY{0}*(1-cumY{0})
+ (1-cumY{0})**2);
else &Wp2 = &Wp2 + (cumX{&X} - cumX{&X-1})*(.25*(cumY{&X} - cumY{&X-1})**2
+ .5*2*(cumY{&X} - cumY{&X-1})*(1-cumY{&X}) + (1-cumY{&X})**2);
end;
do &Y = 0 to &Trials;
if &Y = 0 then
&Wp3 = .25*cumY{0}*cumX{0}**2;
else &Wp3 = &Wp3 + (cumY{&Y} - cumY{&Y-1})*(.25*(cumX{&Y} - cumX{&Y-1})**2
+ .5*2*(cumX{&Y} - cumX{&Y-1})*cumX{&Y-1} + cumX{&Y-1}**2);
end;
outBB2G: %mend StWlBB2G;
%macro StWlBB1G(p_beta, q_beta, Trials, NullVal, Wp1, Wp2, Wp4);
/*
Set p1, p2, p4 parameters for 1-group Wilcoxon problem for beta-binomial
parent. Lots of ties require adjustments:
p1 = Pr[Y > muYnull] + .5*P[Y = muYnull] > .50
p2 = Pr[(Yi + Yi')/2 > muYnull] + 0.50*Pr[(Yi + Yi')/2 = muYnull],
p4 = Pr[{(Yi + Yi')/2 > muYnull)} and {(Yi + Yi'')/2 > muYnull)}]
+ 0.50*Pr[{(Yi + Yi')/2 > muYnull)} and {(Yi + Yi'')/2 = muYnull)}]
+ 0.25*Pr[{(Yi + Yi')/2 = muYnull)} and {(Yi + Yi'')/2 = muYnull)}].
*/
%let pY = pY_BB1G; %let qY = qY_BB1G; %let NullValu = NV_BB1G;
%let muY = muY_BB1G;
&pY = &p_beta; &qY = &q_beta; &NullValu = &NullVal;
if &NullValu = 0 then do;
put // "ERROR: NULL of 0 is not allowed."; stop; end;
if &NullValu = 1 then do;
put // "ERROR: NULL of 1 is not allowed."; stop; end;
SetCumY: %SetCumBB(&pY, &qY, &Trials, cumY);
&muY = &pY/(&pY+&qY);
%let muYnull = muYnull_;
&muYnull = &Trials*&NullValu;
if floor(&muYnull) = &muYnull then
&Wp1 = .5*(cumY{&muYnull} - cumY{&muYnull - 1}) + (1 - cumY{&muYnull});
else &Wp1 = 1 - cumY{floor(&muYnull)};
/* check if Wp1 makes sense */
&Wp1 = round(&Wp1, .001);
if ((&muY ge &NullValu) and (&Wp1 < 0.500)) or
((&muY le &NullValu) and (&Wp1 > 0.500)) then do;
put // "WARNING: The specified beta-binomial distribution";
put " is such that power computations for the";
put " Wilcoxon test would be misleading. Try";
put " increasing the effect size.";
&Wp1 = .; &Wp2 = .; &Wp4 = .;
goto outBB1G;
end;
if ((&muY < &NullValu) and (&Wp1 < 0.500)) then do;
/* UnifyPow is set only for Wp1 > 0.50, so "reflect" the problem. */
temp = &pY; &pY = &qY; &qY = temp; &NullValu = 1 - &NullValu;
go to SetCumY;
end;
%let Y1 = Y1_SetBB;
%let MGvY1 = MGvY1_;
%let Wp2GvY1 = Wp2GvY1_;
do &Y1 = 0 to &Trials;
&MGvY1 = 2*&muYnull - &Y1;
if &MGvY1 < 0 then &Wp2GvY1 = 1;
else if &MGvY1 > &Trials then &Wp2GvY1 = 0;
else if &MGvY1 = 0 then &Wp2GvY1 = .5*cumY{0} + (1 - cumY{&MGvY1});
else if floor(&MGvY1) = &MGvY1 then
&Wp2GvY1 = .5*(cumY{&MGvY1} - cumY{&MGvY1 - 1})
+ (1 - cumY{&MGvY1});
else &Wp2GvY1 = (1 - cumY{floor(&MGvY1)});
if &Y1 = 0 then &Wp2 = &Wp2GvY1*(cumY{0});
else &Wp2 = &Wp2 + &Wp2GvY1*(cumY{&Y1} - cumY{&Y1-1});
end;
%let Wp4GvY1 = Wp4GvY1_;
do &Y1 = 0 to &Trials;
&MGvY1 = 2*&muYnull - &Y1;
if &MGvY1 < 0 then &Wp4GvY1 = 1;
else if &MGvY1 > &Trials then &Wp4GvY1 = 0;
else if &MGvY1 = 0 then &Wp4GvY1 = .25*cumY{0}**2 + (1 - cumY{0})**2;
else if floor(&MGvY1) = &MGvY1 then
&Wp4GvY1 = .25*(cumY{&MGvY1} - cumY{&MGvY1 - 1})**2 +
+ 2*.5*(cumY{&MGvY1} - cumY{&MGvY1 - 1})*(1 - cumY{&MGvY1})
+ (1 - cumY{&MGvY1})**2;
else &Wp4GvY1 = (1 - cumY{floor(&MGvY1)})**2;
if &Y1 = 0 then &Wp4 = &Wp4GvY1*(cumY{0});
else &Wp4 = &Wp4 + &Wp4GvY1*(cumY{&Y1} - cumY{&Y1-1});
end;
outBB1G: %mend StWlBB1G;
%macro StWlCat1 (NullVal, Wp1, Wp2, Wp4);
/*
Set p1, p2, p3 parameters for 1-group Wilcoxon problem for general
discrete parent specified by user. Lots of ties require adjustments:
p1 = Pr[Y > NullVal] + .5*P[Y = NullVal] > .50
p2 = Pr[(Yi + Yi')/2 > NullVal] + 0.50*Pr[(Yi + Yi')/2 = NullVal],
p4 = Pr[{(Yi + Yi')/2 > NullVal)} and {(Yi + Yi'')/2 > NullVal)}]
+ 0.50*Pr[{(Yi + Yi')/2 > NullVal)} and {(Yi + Yi'')/2 = NullVal)}]
+ 0.25*Pr[{(Yi + Yi')/2 = NullVal)} and {(Yi + Yi'')/2 = NullVal)}].
*/
%let NullValu = NV_Cat1;
&NullValu = &NullVal;
if not(0 < &NullValu < NumCat) then do;
put // "ERROR: NULL not between 0 and number of categories."; stop; end;
do iCat = 1 to NumCat;
if iCat = 1 then cumY{iCat} = ProbY{iCat};
else cumY{iCat} = cumY{iCat-1} + ProbY{iCat};
end;
if abs(cumY{NumCat} - 1) > .001 then do;
put // "ERROR: DISTRIBUTION probabilities do not sum to 1.000";
stop; end;
if floor(&NullValu) = &NullValu then
&Wp1 = .5*(cumY{&NullValu} - cumY{&NullValu - 1}) + (1 - cumY{&NullValu});
else &Wp1 = 1 - cumY{floor(&NullValu)};