You are on page 1of 24

Topic 13

T-tests and Nonparameteric


Comparisons Using SAS

T-tests and Nonparameteric


Topic 13 1
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS

• Assigning to Different Treatments


• T-Test: Testing Differences between Two Independent Sample
means
• Wilcoxon Rank-Sum Test
• Paired t-Test

T-tests and Nonparameteric


Topic 13 2
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS
Assigning to Different Treatments
Example 1: We have 25 milking cows to which we assign treatments A or B
using random number generator RANUNI(0) and PROC Rank
DATA ASSIGN; /*To verify what we have, we print.
INPUT NAME $ 5.; Every cow is assigned a random
/* GROUP will be assigned unif(0,1) random number:*/
numbers*/
GROUP=RANUNI(0); PROC PRINT DATA=ASSIGN;
/* Cows names, C1, C2, etc. */ VAR NAME GROUP;
DATALINES; RUN;
C1 Obs NAME GROUP
Output:
C2
C3 1 C1 0.88838
C4 2 C2 0.67017
C5 If run again get different
C6 3 C3 0.95299
random numbers since we
C7 used the seed 4 C4 0.09468
; "0"!!! It depends on the time
5 C5 0.91697
6 C6 0.50620
T-tests and Nonparameteric
Comparisons Using SAS
Topic 13 7 C7 0.42678 3
T-tests and Nonparameteric Comparisons Using
SAS
Assigning to Different Treatments
Example 1: We have 25 milking cows to which we assign treatments A or B
using random number generator RANUNI(0) and PROC Rank
/*To see the content of TREAT:
/*Now use PROC RANK with option Without FORMAT get 0-1
GROUPS=2 to divide the subjects and assignments instead of A,B
assign to ‘0’ or ‘1’ treatments, and create a assignments.*/
new data set containing the treatments PROC PRINT DATA=TREAT;
TREAT.*/ ID NAME;
/*PROC RANK clips the random numbers in VAR GROUP;
some fation when making the assignment "0’ RUN;
or ’1’!!!*/
Output: NAME GROUP
PROC RANK DATA=ASSIGN GROUPS=2 C1 1
OUT=TREAT;
VAR GROUP; C2 1
If run again get different
RUN; random numbers since we C3 1
used the seed C4 0
"0"!!! It depends on the time
C5 1
C6 0
T-tests and Nonparameteric
Comparisons Using SAS
Topic 13 C7 0 4
T-tests and Nonparameteric Comparisons Using
SAS
Assigning to Different Treatments
Example 1: We have 25 milking cows to which we assign treatments A or B
using random number generator RANUNI(0) and PROC Rank
/*To see the content of TREAT: With
/*Now use PROC FORMAT to change from FORMAT get A,B assignments.*/
0-1 to A-B:*/
PROC PRINT DATA=TREAT;
PROC FORMAT; ID NAME;
VALUE ZERONE 0='A' 1='B'; VAR GROUP;
RUN; FORMAT GROUP ZERONE.;
PROC SORT DATA=TREAT; RUN;
/*You could also use DATA=ASSIGN */
BY NAME; Output: NAME GROUP
RUN;
C1 B
If run again get different C2 B
random numbers since we C3 B
used the seed
"0"!!! It depends on the time C4 A
C5 B
C6 A
T-tests and Nonparameteric
Topic 13 5
Comparisons Using SAS C7 A
T-tests and Nonparameteric Comparisons Using
SAS
T-Test: Testing Differences between Two Independent Sample means:
Example 2: Milking cows were assigned randomly to treatment A or B, and thenthe
average daily milk production over a 3 week period was recorded. Test equality of
means using a t-test.
NOTE: The category or class or group are the independent variable
DATA MILK; B 35 The TTEST Procedure
INPUT DIET $ YIELD; B 47 Output
DATALINES; B 55 Variable: YIELD
A 44 B 29
A 44 B 40 DIET N Mean Std Dev Std Err Minimum Maximum
A 56 B 39
A 46 A 13 45.1538 7.9984 2.2184 30.0000 58.0000
B 32
A 47 B 41 B 12 42.2500 8.7399 2.5230 29.0000 57.0000
A 38 B 42 Diff (1-2) 2.9038 8.3613 3.3472
A 58 B 57
A 53 B 51
A 49 B 39
A 35 ;
A 46 PROC TTEST DATA=MILK;
A 30 CLASS DIET; Output continued on next slide
A 41 VAR YIELD;
RUN;
T-tests and Nonparameteric
Topic 13 6
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS
T-Test: Testing Differences between Two Independent Sample means:
Example 2: Milking cows were assigned randomly to treatment A or B, and
thenthe average daily milk production over a 3 week period was recorded.
Test equality of means using a t-test. Output continued
DIET Method Mean 95% CL Mean Std Dev 95% CL Std Dev
A 45.1538 40.3205 49.9872 7.9984 5.7355 13.2032
B 42.2500 36.6969 47.8031 8.7399 6.1913 14.8393
Diff (1-2) Pooled 2.9038 -4.0203 9.8280 8.3613 6.4985 11.7288
Diff (1-2) Satterthwaite 2.9038 -4.0573 9.8650

Method Variances DF t Value Pr > |t|


Pooled Equal 23 0.87 0.3946
Satterthwaite Unequal 22.342 0.86 0.3966

Equality of Variances
Method Num DF Den DF F Value Pr > F
Folded F 11 12 1.19 0.7621

T-tests and Nonparameteric Output continued on next slide


Topic 13 7
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS
T-Test: Testing Differences between Two Independent Sample means
Example 2: Milking cows were assigned randomly to treatment A or B, and
thenthe average daily milk production over a 3 week period was recorded.
Test equality of means using a t-test.
Output continued

T-tests and Nonparameteric


Topic 13 8
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS
Wilcoxon Rank-Sum Test (quick example without SAS):
H0: The two distributions are identical
H1: Distributions are not identical (that is, Dist A is SHIFTED to the right of the dist
B; Dist A is SHIFTED to the left of the dist B; or Dists A,B, are different.)
Example 3: The mineral content of specimens from two locations A, B, are
Recorded:
A: 7.6, 11.1, 6.8, 9.8, 4.9, 6.1, 15.1 (n1=7=n_A)
B: 4.7, 6.4, 4.1, 3.7, 3.9 (n2=5=n_B)
Does location A have a higher mineral content?
Rank the combined data:
B B B B A A B A A A A A
3.7 3.9 4.1 4.7 4.9 6.1 6.4 6.8 7.6 9.8 11.1 15.1
1 2 3 4 5 6 7 8 9 10 11 12 <---- RANKS
USE SMALLER SAMPLE, B, which had a size of 5:
W=WB=1+2+3+4+7=17 <---- Sum of the ranks of B
T-tests and Nonparameteric
Topic 13 9
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS

P-value

Note: E[WB]= nB*(nA + nB +1)/2=5*13/2=32.5


T-tests and Nonparameteric E[WA]= nA*(nA + nB +1)/2=7*13/2=45.5
Topic 13 10
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS
Wilcoxon Rank-Sum Test (using SAS):
H0: The two distributions are identical
H1: Distributions are not identical (that is, Dist A is SHIFTED to the right of the dist
B; Dist A is SHIFTED to the left of the dist B; or Dists A,B, are different.)
Example 3: Output
The NPAR1WAY Procedure
DATA MINERAL;
INPUT LOCATION $ CONT @@;
Wilcoxon Scores (Rank Sums) for Variable CONT
/* Use "@@" to input data horizontally*/
Classified by Variable LOCATION
DATALINES;
A 7.6 A 11.1 A 6.8 A 9.8 A 4.9 A 6.1 A 15.1 LOCATION N Sum of Expected Std Dev Mean Score
B 4.7 B 6.4 B 4.1 B 3.7 B 3.9 Scores Under H0 Under H0
; A 7 61.0 45.50 6.157651 8.714286
PROC NPAR1WAY DATA=MINERAL WILCOXON;
B 5 17.0 32.50 6.157651 3.400000
CLASS LOCATION;
VAR CONT;
EXACT WILCOXON;
RUN; Output continued on next slide

T-tests and Nonparameteric


Topic 13 11
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS
Wilcoxon Rank-Sum Test (using SAS):
Example 3: Kruskal-Wallis Test
Output Continued Chi-Square 6.3363
Wilcoxon Two-Sample Test
DF 1
Statistic (S) 17.0000 Pr > Chi-Square 0.0118
Good approximation
Normal Approximation
Z -2.4360
One-Sided Pr < Z 0.0074
Two-Sided Pr > |Z| 0.0149

t Approximation
One-Sided Pr < Z 0.0165
Two-Sided Pr > |Z| 0.0331
What we calculated!!
Exact Test
One-Sided Pr <= S 0.0051
Two-Sided Pr >= |S - Mean| 0.0101
Z includes a continuity correction of 0.5.
T-tests and Nonparameteric
Topic 13 12
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS

T-tests and Nonparameteric


Topic 13 13
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS
Wilcoxon Rank-Sum Test (using SAS):
H0: The two distributions are identical
H1: Distributions are not identical (that is, Dist A is SHIFTED to the right of the dist
B; Dist A is SHIFTED to the left of the dist B; or Dists A,B, are different.)
Example 4: The SAS System
The NPAR1WAY Procedure
DATA FLAME;
INPUT FABRIC $ DAMAGE @@; Wilcoxon Scores (Rank Sums) for Variable DAMAGE
DATALINES; Classified by Variable FABRIC
A 5.7 A 7.3 A 7.6 A 6.0 A 6.5 FABRIC N Sum of Expected Std Dev Mean
B 4.9 B 7.4 B 5.3 B 4.6 B 6.2 Scores Under H0 Under H0 Score
;
A 5 34.0 27.50 4.787136 6.80
PROC NPAR1WAY DATA=FLAME WILCOXON;
CLASS FABRIC; B 5 21.0 27.50 4.787136 4.20
VAR DAMAGE;
EXACT WILCOXON;
RUN;
Output continued on next slide

T-tests and Nonparameteric


Topic 13 14
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS
Wilcoxon Rank-Sum Test (using SAS):
Wilcoxon Two-Sample Test
Example 4: Statistic (S) 34.0000
Output Continued

Kruskal-Wallis Test Normal


Approximation
Chi-Square 1.8436
Z 1.2534 Good approximation
DF 1
One-Sided Pr > 0.1050
Pr > Chi-Square 0.1745 Z
Two-Sided Pr > 0.2101
|Z|

t Approximation
One-Sided Pr > 0.1208
Z
Two-Sided Pr > 0.2417
|Z|

Exact Test
One-Sided Pr >= 0.1111
S
So we do not reject the null hypothesis. Two-Sided Pr >= 0.2222
|S - Mean|
T-tests and Nonparameteric
Topic 13 Z includes a continuity correction of 0.5. 15
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS

T-tests and Nonparameteric


Topic 13 16
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS
Paired T-Test: Testing Differences between Two Dependent Sample means:
Simply use PROC MEANS on the difference with options: T and PRT (p-val).
Example 5a: A pill is used to reduce blood preasure. Have 15 subjects.
DATA BP;
INPUT BEFORE AFTER; PROC MEANS DATA=BP N MEAN STDERR T PRT;
DIFF=BEFORE-AFTER; VAR DIFF;
DATALINES; RUN;
70 68
80 72 Note: df=n-1=15-1=14
72 62 Note: We get a two sided test. To get one sided, divide
76 70 p-value by 2!!!
76 58
Output:
76 66
The SAS System
72 68
78 52 The MEANS Procedure
82 64
64 72 Analysis Variable : DIFF
74 74 N Mean Std Error t Value Pr > |t|
92 60
74 74 15 8.8000000 2.8338095 3.11 0.0077
68 72
84 74 P-value = 0.00385, We reject the null hypothesis
;
T-tests and Nonparameteric
Topic 13 17
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS
Paired T-Test: Testing Differences between Two Dependent Sample means:
Using PROC TTEST on the difference with options: T and PRT (p-val).
Example 5b: A pill is used to reduce blood preasure. Have 15 subjects.
DATA PAIRED;
INPUT BEFORE AFTER; PROC TTEST DATA = PAIRED;
DIFF=BEFORE-AFTER; VAR DIFF;
DATALINES; RUN;
70 68
80 72 Note: df=n-1=15-1=14
72 62 Note: We get a two sided test. To get one sided, divide
76 70 p-value by 2!!!
Output:
76 58
The TTEST Procedure
76 66
72 68 Variable: DIFF
78 52 N Mean Std Dev Std Err Minimum Maximum
82 64
64 72 15 8.8000 10.9753 2.8338 -8.0000 32.0000
74 74
Mean 95% CL Mean Std Dev 95% CL Std Dev
92 60
74 74 8.8000 2.7221 14.8779 10.9753 8.0353 17.3091
68 72 DF t Value Pr > |t|
84 74
14 3.11 0.0077
;
T-tests and Nonparameteric Output continued on next slide
Topic 13 18
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS
Paired T-Test: Testing Differences between Two Dependent Sample means:
Using PROC TTEST on the difference with options: T and PRT (p-val).
Example 5b: A pill is used to reduce blood preasure. Have 15 subjects.
Output continued
Remember:
We get a two sided test. To get one sided, divide p-
value by 2!!!

P-value = 0.00385, We reject the null hypothesis (of


course we should have gotten the same answer)

T-tests and Nonparameteric


Topic 13 19
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS
Paired T-Test: Testing Differences between Two Dependent Sample means:
Using PROC TTEST on the difference with options: T and PRT (p-val).
Example 5c: A pill is used to reduce blood preasure. Have 15 subjects.
DATA PAIRED;
INPUT BEFORE AFTER; PROC TTEST DATA = PAIRED;
DIFF=BEFORE-AFTER; PAIRED BEFORE*AFTER;
DATALINES; RUN;
70 68
80 72 Note: df=n-1=15-1=14
72 62 Note: We get a two sided test. To get one sided, divide
76 70 p-value by 2!!!
Output:
76 58
The TTEST Procedure
76 66
72 68 Variable: BEFORE-AFTER
78 52 N Mean Std Dev Std Err Minimum Maximum
82 64
64 72 15 8.8000 10.9753 2.8338 -8.0000 32.0000
74 74
Mean 95% CL Mean Std Dev 95% CL Std Dev
92 60
74 74 8.8000 2.7221 14.8779 10.9753 8.0353 17.3091
68 72 DF t Value Pr > |t|
84 74
14 3.11 0.0077
;
T-tests and Nonparameteric Output continued on next slide
Topic 13 20
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS
Paired T-Test: Testing Differences between Two Dependent Sample means:
Using PROC TTEST on the difference with options: T and PRT (p-val).
Example 5c: A pill is used to reduce blood preasure. Have 15 subjects.
Output continued
Remember:
We get a two sided test. To get one sided, divide p-
value by 2!!!

P-value = 0.00385, We reject the null hypothesis (of


course we should have gotten the same answer)

T-tests and Nonparameteric


Topic 13 21
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS
Paired T-Test: Testing Differences between Two Dependent Sample means:
Using PROC TTEST on the difference with options: T and PRT (p-val).
Example 5c: A pill is used to reduce blood preasure. Have 15 subjects.
Output continued
Remember:
We get a two sided test. To get one sided, divide p-
value by 2!!!

P-value = 0.00385, We reject the null hypothesis (of


course we should have gotten the same answer)

T-tests and Nonparameteric


Topic 13 22
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS

If you want a mean other than the default 0 and a different alpha
(for confidence interval):

PROC TTEST H0=1 ALPHA=0.01 DATA = BP ;


paired before*after;
RUN;

T-tests and Nonparameteric


Topic 13 23
Comparisons Using SAS
T-tests and Nonparameteric Comparisons Using
SAS

Wrap-Up
• Assigning to Different Treatments
• T-Test: Testing Differences between Two Independent Sample
means
• Wilcoxon Rank-Sum Test
• Paired t-Test

T-tests and Nonparameteric


Topic 13 24
Comparisons Using SAS

You might also like