data(sleep)
print(sleep)
## extra group ID
## 1 0.7 1 1
## 2 -1.6 1 2
## 3 -0.2 1 3
## 4 -1.2 1 4
## 5 -0.1 1 5
## 6 3.4 1 6
## 7 3.7 1 7
## 8 0.8 1 8
## 9 0.0 1 9
## 10 2.0 1 10
## 11 1.9 2 1
## 12 0.8 2 2
## 13 1.1 2 3
## 14 0.1 2 4
## 15 -0.1 2 5
## 16 4.4 2 6
## 17 5.5 2 7
## 18 1.6 2 8
## 19 4.6 2 9
## 20 3.4 2 10
str(sleep)
## 'data.frame': 20 obs. of 3 variables:
## $ extra: num 0.7 -1.6 -0.2 -1.2 -0.1 3.4 3.7 0.8 0 2 ...
## $ group: Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ ID : Factor w/ 10 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
d <- with(sleep, extra[group == 2] - extra[group == 1])
shapiro.test(d)
##
## Shapiro-Wilk normality test
##
## data: d
## W = 0.82987, p-value = 0.03334
a <- with(sleep, extra[group == 1])
shapiro.test(a)
##
## Shapiro-Wilk normality test
##
## data: a
## W = 0.92581, p-value = 0.4079
b <- with(sleep, extra[group == 2])
shapiro.test(b)
##
## Shapiro-Wilk normality test
##
## data: b
## W = 0.9193, p-value = 0.3511
t.test(b, a, data = sleep, paired = TRUE)
##
## Paired t-test
##
## data: b and a
## t = 4.0621, df = 9, p-value = 0.002833
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## 0.7001142 2.4598858
## sample estimates:
## mean difference
## 1.58
t.test(b, a, data = sleep, paired = TRUE, alternative = "greater")
##
## Paired t-test
##
## data: b and a
## t = 4.0621, df = 9, p-value = 0.001416
## alternative hypothesis: true mean difference is greater than 0
## 95 percent confidence interval:
## 0.8669947 Inf
## sample estimates:
## mean difference
## 1.58
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.