You are on page 1of 52

Eight

One Sample Tests and


Two Sample Tests
293
8.1 ONE SAMPLE TEST FOR THE LOCATION
Parametric test
One sample t-test (normal populations)
Nonparametric tests
Sign test
Sign rank test
294
Testing H
0
: =
0
The data set teabag.txt consists of the weight, in grams, of a ran-
dom sample of 50 tea bags.
We want to nd out if the mean amount of tea per bag is different
from 5.5 grams.
That is, to test
H
0
: =5.5 against H
1
: =5.5.
295
Tests for Location: SAS Proc Univariate
*
One sample t-test;
data teabags;
infile "C:/ST2137/data/ST2137-data/teabags.txt"
firstobs=2;
input weight;
*
Test H_0: mu = 5.5 against H_1: mu ^= 5.5;
proc univariate data=teabags mu0=5.5;
var weight;
run;
296
SAS Proc Univariate Output
The UNIVARIATE Procedure
Variable: weight
Tests for Location: Mu0=5.5
Test -Statistic- -----p Value------
Students t t 0.093541 Pr > |t| 0.9259
Sign M 2.5 Pr >= |M| 0.5601
Signed Rank S 16.5 Pr >= |S| 0.8635
297
One sample t-test: R
# Parametric method: One sample t-test
> teabags=read.table("C:/ST2137/data/teabags.txt", header=T)
> attach(teabags)
> t.test(weight, mu=5.5)
One Sample t-test
data: weight
t = 0.0935, df = 49, p-value = 0.9259
alternative hypothesis: true mean is not equal to 5.5
95 percent confidence interval:
5.471323 5.531477
sample estimates:
mean of x
5.5014
298
Sign Test: R
# Nonparametric method: Sign test
> weightno0 = (weight[weight!=5.5])
> binom.test(sum(weight>5.5), length(weightno0))
Exact binomial test
data: sum(weight > 5.5) and length(weightno0)
number of successes = 26, number of trials = 47, p-value = 0.5601
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
0.4011572 0.6982785
sample estimates:
probability of success
0.5531915
299
Signed Rank Test: R
# Nonparametric method: Signed rank test
> wilcox.test(weightno0, mu=5.5, alternative="two.sided")
Wilcoxon signed rank test with continuity correction
data: weightno0
V = 580.5, p-value = 0.8655
alternative hypothesis: true location is not equal to 5.5
Warning message:
In wilcox.test.default(weightno0, mu = 5.5, alternative = "two.sided") :
cannot compute exact p-value with ties
300
One Sample t-test: SPSS
Import the data set teabags.txt.
Analyze" Compare Means" One-Sample t tests ...".
Move weight" to the Test Variable(s) panel and input
0
(i.e. 5.5) in
the Test Value panel.
OK".
301
One sample t-test: SPSS Output
302
Sign and Signed Rank Tests: SPSS
Create a new variable mu with 50 values of
0
(i.e. 5.5).
Analyze" Nonparametric Tests" Legacy Dialogs ..." 2 Re-
lated Samples ...".
Move weight and mu to the Test Pair(s) List.
Choose Test Type: Wilcoxon and Sign OK".
303
Sign Test: SPSS Output
304
Signed Rank Tests: SPSS Output
305
8.2 TESTS COMPARING TWO GROUPS
Two independent samples
Two-sample t-test (normal populations)
Wilcoxon rank-sum test (nonparametric)
Two related samples
Paired t-test (normal population)
Sign Test or Wilcoxon signed rank test (nonparametric)
306
EXAMPLE 8.1 Consider the two independent samples, where subjects are
randomly assigned to a control (where a placebo is administrated) or treat-
ment group (where a drug is administered). Their response times to a
stimulus are then measured.
The data is presented in the following two ways:
Response time in millisecond
Control Treatment
80 100
93 103
83 104
89 99
98 102
As a table.
group time
c 80
c 93
c 83
c 89
c 98
t 100
t 103
t 104
t 99
t 102
As a text le: ex8_1.txt
307
Two sample t-test: SAS
Test H
0
:
T

C
=0 against H
1
:
T

C
=0
data ex8_1;
infile "C:/ST2137/data/ex8_1.txt" firstobs=2;
input group $ time;
proc ttest data=ex8_1;
title "t-test Example";
class group;
var time;
run;
Remark: Proc ttest uses a class statement to identify the two groups
of subjects.
308
Two sample t-test: SAS Output
The TTEST Procedure
Variable: time
group N Mean Std Dev Std Err Minimum Maximum
c 5 88.6000 7.3007 3.2650 80.0000 98.0000
t 5 101.6 2.0736 0.9274 99.0000 104.0
Diff (1-2) -13.0000 5.3666 3.3941
group Method Mean 95% CL Mean Std Dev 95% CL Std Dev
c 88.6000 79.5350 97.6650 7.3007 4.3741 20.9789
t 101.6 99.0252 104.2 2.0736 1.2424 5.9587
Diff (1-2) Pooled -13.0000 -20.8268 -5.1732 5.3666 3.6249 10.2811
Diff (1-2) Satterthwaite -13.0000 -21.9317 -4.0683
Method Variances DF t Value Pr > |t|
Pooled Equal 8 -3.83 0.0050
Satterthwaite Unequal 4.6412 -3.83 0.0141
Equality of Variances
Method Num DF Den DF F Value Pr > F
Folded F 4 4 12.40 0.0318
309
Two sample t-test: R
> # Two sample t-test Example 8.1
> ex8.1 <- read.table("C:/ST2137/data/ex8_1.txt", header=TRUE)
> attach(ex8.1)
> cont <- time[group=="c"]
> treat <- time[group=="t"]
> var.test(cont, treat) #Test if the variances are equal
F test to compare two variances
data: cont and treat
F = 12.3953, num df = 4, denom df = 4, p-value = 0.03177
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
1.290573 119.051498
sample estimates:
ratio of variances
12.39535
> # t.test(cont, treat, mu=0, var.equal=TRUE)
> t.test(cont, treat, mu=0, var.equal=FALSE)
310
Welch Two Sample t-test
data: cont and treat
t = -3.8302, df = 4.641, p-value = 0.0141
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-21.931655 -4.068345
sample estimates:
mean of x mean of y
88.6 101.6
> #Alternatively, we can use the following command:
> t.test(time~group, mu=0, var.equal=FALSE)
311
Two sample t-test: SPSS
Import the data set ex8_1.txt.
Analyze" Compare Means" Independent Sample T tests ...".
Move time to the Test Variable(s) panel and group to the "Grouping
Variable" panel.
Click "Dene Groups ...".
312
Input the values for the two groups.
Continue" OK".
313
Two sample t-test: SPSS Output
314
8.3 TWO INDEPENDENT SAMPLES: NONPARAMETRIC TESTS
Suppose the assumptions of two-sample t-test not met:
The data are not normally distributed and the sample size is small.
E.g. The following are numbers in a psychology experiment that
measured the response to a stimulus:
0 6 0 5 7 6 9 4 8 0 7 0 5 6 6 0 0
It may be due to a threshold effect.
The response is either 0 (the stimulus is not detected), or, once
the stimulus is detected, the average response is about 6.
315
316
The data values may only represent ordered categories.
E.g. Scales such as 1 = very mild, 2 = mild, 3 = moderate, 4 =
strong, 5 = severe reect the strength of a response.
We cannot say that a score of 4 (strong) is worth twice the score
of 2 (mild).
We need a nonparametric test to analyze differences in central ten-
dencies for ordinal data.
For very small samples, nonparametric tests are often more appro-
priate since assumptions concerning distribution are difcult to de-
termine.
317
Wilcoxon rank sum test (Mann-Whitney U-test)
Consider the following experiment
Group A: No treatment.
Group B: Treated with a drug to prevent tumor formation
Both groups are exposed to a chemical that encourages tumor growth. The
masses (in grams) of tumors in Groups A and B are
A: 3.1 2.2 1.7 2.7 2.5
B: 0.0 0.0 1.0 2.3
We can order the data by the size of the tumor to get
Mass: 0.0 0.0 1.0 1.7 2.2 2.3 2.5 2.7 3.1
Group: B B B A A B A A A
Rank: 1.5 1.5 3 4 5 6 7 8 9
318
Sum ranks of Group A =4+5+7+8+9 =33
Sum ranks of Group B =1.5+1.5+3+6 =12
If there were smaller tumors in Group B, we would expect the Bs to be at
the lower rank ordering and therefore have a smaller sum of ranks then
the As.
319
Wilcoxon rank sum test: SAS
data ex8_2;
infile "C:/ST2137/data/ex8_2.txt" firstobs=2;
input group $ mass;
proc npar1way data=ex8_2 wilcoxon;
title "Nonparametric Test to Compare Tumor Masses";
class group;
var mass;
exact wilcoxon;
run;
320
Wilcoxon rank sum test: SAS Output
The NPAR1WAY Procedure
Wilcoxon Scores (Rank Sums) for Variable mass
Classified by Variable group
Sum of Expected Std Dev Mean
group N Scores Under H0 Under H0 Score
_____________________________________________________________________
A 5 33.0 25.0 4.065437 6.60
B 4 12.0 20.0 4.065437 3.00
Average scores were used for ties.
Wilcoxon Two-Sample Test
Statistic (S) 12.0000
Normal Approximation
Z -1.8448
One-Sided Pr < Z 0.0325
321
Two-Sided Pr > |Z| 0.0651
t Approximation
One-Sided Pr < Z 0.0511
Two-Sided Pr > |Z| 0.1023
Exact Test
One-Sided Pr <= S 0.0317
Two-Sided Pr >= |S - Mean| 0.0635
Z includes a continuity correction of 0.5.
Kruskal-Wallis Test
Chi-Square 3.8723
DF 1
Pr > Chi-Square 0.0491
322
Wilcoxon rank sum test: R
> # Example 8.2 Wilcoxon rank sum test
> ex8.2 <-read.table("C:/ST2137/data/ex8_2.txt", header=TRUE)
> attach(ex8.2)
> gp.a <-mass[group=="A"]
> gp.b <-mass[group=="B"]
> wilcox.test(gp.a, gp.b) #Alternatively, wilcox.test(mass ~ group)
Wilcoxon rank sum test with continuity correction
data: gp.a and gp.b
W = 18, p-value = 0.06506
alternative hypothesis: true location shift is not equal to 0
Warning message:
In wilcox.test.default(gp.a, gp.b) : cannot compute exact p-value with ties
323
Wilcoxon rank sum test: SPSS
We create a numeric variable Groupno to represent the groups.
Transform" Recode" Into different variable ...".
Move group to String variable" panel.
Complete Output variable" window Change".
324
Click Old and New Values" and input the values.
Continue" OK".
Analyze" Legacy Dialogs" Nonparametric Tests" 2 Inde-
pendent Samples ...".
325
Move mass to the Test Variable List" and Groupno to the Grouping
Variable:
Click "Dene Groups ...".
Input the values for the two groups.
Choose Mann-Whitney U" in the Test Type".
326
Wilcoxon rank sum test: SPSS Output
327
8.4 PAIRED t -TEST (RELATED SAMPLES)
There are many situations where each subject receives both treat-
ments.
Each subject could have been measured in the absence of drug and
after receiving the drug.
The response times for the control and treatment groups would no
longer be independent.
2-sample t-test cannot be used since the groups are no longer inde-
pendent.
Apaired t-test can be used if the differences between before and after
treatments follow a normal distribution.
328
Paired t-test: SAS
/
*
Example 8.3 Paired t-test
*
/
data ex8_3;
infile "C:/ST2137/data/ex8_3.txt" firstobs=2;
input subject ctime ttime;
proc ttest data=ex8_3;
title "A Paired t-test";
paired ctime
*
ttime;
run;
329
Paired t-test: SAS Output
A Paired t-test
The TTEST Procedure
Difference: ctime - ttime
N Mean Std Dev Std Err Minimum Maximum
6 -7.3333 4.1312 1.6865 -15.0000 -4.0000
Mean 95% CL Mean Std Dev 95% CL Std Dev
-7.3333 -11.6687 -2.9979 4.1312 2.5787 10.1322
DF t Value Pr > |t|
5 -4.35 0.0074
330
Paired t-test: R
> ex8.3 <-read.table("C:/ST2137/data/ex8_3.txt", header=TRUE)
> attach(ex8.3)
> t.test(control, treatment, mu=0, paired=TRUE)
Paired t-test
data: control and treatment
t = -4.3481, df = 5, p-value = 0.007372
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-11.668743 -2.997923
sample estimates:
mean of the differences
-7.333333
331
Paired t-test: SPSS
Import ex8_3.txt.
Analyze" Compare Means" Paired Sample T test...".
Highlight the two variables control" and treatment" simultaneously.
Move these 2 variables to Paired Variables"panel OK".
332
Paired t-test: SPSS Output
333
8.5 TWO RELATED SAMPLES: NONPARAMETRIC TESTS
We apply the one sample nonparametric tests to the difference of the
paired observations.
One sample nonparametric tests:
Sign test
Wilcoxon signed rank test
334
EXAMPLE 8.2 Consider an experiment that each subject tries each of the
two drugs. The time span to pain relief is measured:
Subject 1 2 3 4 5 6 7 8
Drug A 20 40 30 45 19 27 32 26
Drug B 18 36 32 46 15 22 29 25
335
Sign test
Consider the previous example. The differences in time span between
Drug A and Drug B are
Subject: 1 2 3 4 5 6 7 8
Difference: 2 4 -2 -1 4 5 3 1
Sign: + + - - + + + +
Number of positive signs = 6 and number of negative signs = 2.
If there was no difference in the two drugs, we expect the number of pos-
itive signs (i.e. A < B) should be more or less the same as the number of
negative signs (i.e. B <A).
336
Wilcoxon signed rank test
The differences in time span between Drug A and Drug B are
Subject: 1 2 3 4 5 6 7 8
Difference: 2 4 -2 -1 4 5 3 1
Rank of
absolute 3.5 6.5 3.5 1.5 6.5 8 5 1.5
Sign: + + - - + + + +
Sum of positive ranks: 3.5 + 6.5 + 6.5 + 8 + 5 + 1.5 = 31
Sum of negative ranks: 3.5 + 1.5 = 5
If there was no difference in the two drugs, we would expect the sum of
the ranks of positive signs should be more or less the same as the sum of
the ranks of negative signs.
337
Two Related Samples - Nonparametric tests: SAS
/
*
Example 8.4 Nonparametric test for related samples
*
/
data ex8_4;
infile "C:/ST2137/data/ex8_4.txt" firstobs=2;
input subject drug_A drug_B;
diff = drug_A -drug_B;
proc univariate data=ex8_4;
title "Nonparametric Test for 2 related samples";
var diff;
run;
338
Two Related Samples - Nonparametric tests: SAS output
Nonparametric Test for 2 related samples
The UNIVARIATE Procedure
Variable: diff
Moments
N 8 Sum Weights 8
Mean 2 Sum Observations 16
Std Deviation 2.50713268 Variance 6.28571429
Skewness -0.5801629 Kurtosis -0.977686
Uncorrected SS 76 Corrected SS 44
Coeff Variation 125.356634 Std Error Mean 0.88640526
Tests for Location: Mu0=0
Test -Statistic- -----p Value------
Students t t 2.256304 Pr > |t| 0.0587
Sign M 2 Pr >= |M| 0.2891
Signed Rank S 13 Pr >= |S| 0.0859
339
Two Related Samples - Nonparametric tests: R
> ex8.4 <-read.table("C:/ST2137/data/ex8_4.txt", header=TRUE)
> attach(ex8.4)
> diff <-drug_A -drug_B
> ncount <-sum(sign(diff[diff>0]))
# Get the number of positive signs
> binom.test(ncount, length(diff),0.5)
# binom.test(obs_x, n, H_0: p = 0.5)
Exact binomial test
data: ncount and length(diff)
number of successes = 6, number of trials = 8, p-value = 0.2891
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
0.3491442 0.9681460
sample estimates:
probability of success
0.75
340
> wilcox.test(diff)
Wilcoxon signed rank test with continuity correction
data: diff
V = 31, p-value = 0.07895
alternative hypothesis: true location is not equal to 0
Warning message:
In wilcox.test.default(diff) : cannot compute exact p-value with ties
341
Two Related Samples Nonparametric tests: SPSS
Analyze" Nonparametric Tests" Legacy Dialogs" 2 Re-
lated Samples ...".
Highlight the two variables drug_A and drug_B simultaneously.
Move these 2 variables to Test Pairs: " panel OK".
Check Wilcoxon" and Sign" OK".
342
Two Related Samples Nonparametric tests: SPSS Output
Sign Test
343
Wilcoxon Signed Rank Test
344

You might also like