You are on page 1of 8

T-test

YIK LUN, KEI

Equal Variance, population variance unknown


y1 y0
t0 = q
Sp n1 + n1
1 2

(n1 1)S12 +(n2 1)S22


Sp 2 = n1 +n2 2
r r
1 1 1 1
y1 y0 t/2,n1 +n2 2 Sp n1 + n2 1 2 y1 y0 + t/2,n1 +n2 2 Sp n1 + n2

Inequal Variance, population variance unknown

t0 = ry1 y0
S2 S2
1 2
n1 + n2

(S12 /n1 +S22 /n2 )2


df = (S 2 /n1 )2 (S 2 /n2 )2
1 2
n1 1 + n2 1
r r
S12 S22 S12 S22
y1 y0 t/2,df n1 + n2 1 2 y1 y0 + t/2,df n1 + n2

Example

(a)

H0 : 1 = 2
H1 : 1 6= 2

(b)

Z0 = ry1 y0
2 2
1 2
n1 + n2

Since Z0.025 = 1.96, which is greater than 1.35, do not reject the null.
set.seed(1112) # random order
M1 <- sample(c(16.03,16.04,16.05,16.05,16.02,16.01,15.96,15.98,16.02,15.99))
set.seed(123)
M2 <- sample(c(16.02,15.97,15.96,16.01,15.99,16.03,16.04,16.02,16.01,16))
# given population variance, even if we draw 10 sample from each group
mu1<-mean(M1)
mu2<-mean(M2)
(mu1-mu2) / sqrt(0.015^2/10 + 0.018^2/10)

1
## [1] 1.349627

(c)

p-value is 0.1771
2*pnorm(-abs(1.349627))

## [1] 0.1771357

(d)

Since this interval contains 0. There is no difference in machines


r r
12 22 2 22
y1 y0 Z/2 n1 + n2 1 2 y1 y0 + Z/2 n11 + n2
r
2 2
(16.015 16.005) (1.96) 0.015 10 r + 0.018
10 ) 1 2
2 0.0182
(16.015 16.005) + (1.96) 0.015 10 + 10 )
0.0045 1 2 0.0245
(16.015-16.005) - (1.96)*sqrt((0.015^2/10) + (0.018^2/10) ) # lower bound

## [1] -0.004522529
(16.015-16.005) + (1.96)*sqrt((0.015^2/10) + (0.018^2/10) ) # upper bound

## [1] 0.02452253

Example

(a)

The data is assumed to be normal since all the data points are very close to qqline.
B1 <- c(6.08,6.22,7.99,7.44,6.48,7.99,6.32,7.6,6.03,7.52)
B2 <- c(5.73,5.8,8.42,6.84,6.43,8.76,6.32,7.62,6.59,7.67)
diff <- B1-B2
qqnorm(diff);qqline(diff,col=2)

2
Normal QQ Plot

0.4
Sample Quantiles

0.0
0.4
0.8

1.5 1.0 0.5 0.0 0.5 1.0 1.5

Theoretical Quantiles

(b)

The 95% confidence interval on the difference in mean score is (-0.3664148, 0.2644148) contains the value of
zero. There is no difference in birth order.
var.test(B1,B2)

##
## F test to compare two variances
##
## data: B1 and B2
## F = 0.59148, num df = 9, denom df = 9, p-value = 0.4461
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.1469164 2.3813137
## sample estimates:
## ratio of variances
## 0.5914846
t.test(diff,alternative=c("two.sided"),cond.level=0.95,var.equal = TRUE)

##
## One Sample t-test
##
## data: diff
## t = -0.36577, df = 9, p-value = 0.723
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
## -0.3664148 0.2644148
## sample estimates:

3
## mean of x
## -0.051

Example

(a)

For both F1 and F2, most of the data points are closely to the qqline. Hence we can assume that the
assumption of normality is fulfilled. In addition, since the slopes for both plot are almost the same, we can
say that the assumption of equal variance is fulfilled for both samples. In addition, p-value for equal variance
is 0.9419, hence we do not reject the null that their variances are different.
set.seed(100) # random order
F1<-sample(c(206,188,205,187,193,207,185,189,192,210,194,178))
set.seed(1111)
F2<-sample(c(177,197,206,201,176,185,200,197,198,188,189,203))
qqnorm(F1);qqline(F1,col=2)

Normal QQ Plot
210
Sample Quantiles

200
190
180

1.5 1.0 0.5 0.0 0.5 1.0 1.5

Theoretical Quantiles
qqnorm(F2);qqline(F2,col=2)

4
Normal QQ Plot

205
Sample Quantiles

195
185
175

1.5 1.0 0.5 0.0 0.5 1.0 1.5

Theoretical Quantiles
qqnorm(F1-F2);qqline(F1-F2,col=2)

Normal QQ Plot
30
20
Sample Quantiles

10
0
10
20

1.5 1.0 0.5 0.0 0.5 1.0 1.5

Theoretical Quantiles
var.test(F1,F2)

##
## F test to compare two variances

5
##
## data: F1 and F2
## F = 1.046, num df = 11, denom df = 11, p-value = 0.9419
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.3011181 3.6334674
## sample estimates:
## ratio of variances
## 1.045994

(b)

Since p-value is 0.3667, which is greater than 0.05, do not reject the null. Hence the mean deflection
temperature under load for formulation 1 does not exceed that of formulation 2.
t.test(F1,F2,alternative=c("greater"),cond.level=0.95,var.equal = T)

##
## Two Sample t-test
##
## data: F1 and F2
## t = 0.34483, df = 22, p-value = 0.3667
## alternative hypothesis: true difference in means is greater than 0
## 95 percent confidence interval:
## -5.637883 Inf
## sample estimates:
## mean of x mean of y
## 194.5000 193.0833

(c)

p-value for equal variance is 0.9419, hence we do not reject the null that their variances are different. The
p-value for normal test on F1 is 0.3438 and the p-value for normal test on F2 is 0.2453. Since both p-values are
greater than 0.05, we do not reject the null hypothesis that they follow an approximately normal distribution.
shapiro.test(F1)

##
## Shapiro-Wilk normality test
##
## data: F1
## W = 0.92643, p-value = 0.3438
shapiro.test(F2)

##
## Shapiro-Wilk normality test
##
## data: F2
## W = 0.91474, p-value = 0.2453
shapiro.test(F1-F2)

##
## Shapiro-Wilk normality test

6
##
## data: F1 - F2
## W = 0.98109, p-value = 0.9876

Example

(a)

H0 : 1 = 2
H1 : 1 6= 2

(b)

Since t0.975,18 = 2.1, which is greater than 0.798, do not reject the null.
set.seed(1112) # random order
M1 <- sample(c(16.03,16.04,16.05,16.05,16.02,16.01,15.96,15.98,16.02,15.99))
set.seed(123)
M2 <- sample(c(16.02,15.97,15.96,16.01,15.99,16.03,16.04,16.02,16.01,16))
mu1<-mean(M1)
mu2<-mean(M2)
sd1<-sd(M1)
sd2<-sd(M2)
spool<-sqrt((9*sd1^2+9*sd2^2) / (18))
t0 = (mu1-mu2) / (spool * sqrt(1/10 + 1/10))
t0

## [1] 0.7989355
qt(0.975,18) # df = n1+n2-2

## [1] 2.100922

(c)

p-value is 0.43474
(1-pt(t0,18))*2

## [1] 0.4347438
t.test(M1,M2,alternative=c("two.sided"),cond.level=0.95,var.equal = T)

##
## Two Sample t-test
##
## data: M1 and M2
## t = 0.79894, df = 18, p-value = 0.4347
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.01629652 0.03629652
## sample estimates:
## mean of x mean of y

7
## 16.015 16.005

(d)

Since this interval contains 0. There is no difference in machines


r r
y1 y0 t/2,n1 +n2 2 Sp n1 + n2 1 2 y1 y0 + t/2,n1 +n2 2 Sp n11
1 1
+ 1
n2
q
1 1
(16.015 16.005) (2.1) 0.028 10 10 ) 1 2
+q
1 1
(16.015 16.005) + (2.1) 0.028 10 + 10 )
0.0163 1 2 0.0363
(16.015-16.005) - (2.1) * 0.028 * sqrt((1/10 + 1/10))

## [1] -0.01629616
(16.015-16.005) + (2.1) * 0.028 * sqrt((1/10 + 1/10))

## [1] 0.03629616

Example inequal variance

N1 <- sample(c(6625,6000,5450,5200,5175,4900,4750,4500,3985,900,450,2800))
N2 <- sample(c(3900,3500,3450,3200,2980,2800,2500,2400,2200,1200,1150,1130))
var.test(N1,N2)

##
## F test to compare two variances
##
## data: N1 and N2
## F = 3.9874, num df = 11, denom df = 11, p-value = 0.03052
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 1.147892 13.851139
## sample estimates:
## ratio of variances
## 3.987432
t.test(F1,F2,alternative=c("two.sided"),cond.level=0.95,var.equal = F)

##
## Welch Two Sample t-test
##
## data: F1 and F2
## t = 0.34483, df = 21.989, p-value = 0.7335
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -7.103683 9.937016
## sample estimates:
## mean of x mean of y
## 194.5000 193.0833

You might also like