The paired samples t-test is used to compare the means between two related groups of samples. In this case, you have two values (i.e., pair of values) for the same samples.
# 1. Data in two numeric vectors
# 1.1 Weight of the mice before treatment
before <-c(200.1, 190.9, 192.7, 213, 241.4, 196.9, 172.2, 185.5, 205.2, 193.7)
# 1.2 Weight of the mice after treatment
after <-c(392.9, 393.2, 345.1, 393, 434, 427.9, 422, 383.9, 392.3, 352.2)
beforeAfter = cbind(before, after)
beforeAfter## before after
## [1,] 200.1 392.9
## [2,] 190.9 393.2
## [3,] 192.7 345.1
## [4,] 213.0 393.0
## [5,] 241.4 434.0
## [6,] 196.9 427.9
## [7,] 172.2 422.0
## [8,] 185.5 383.9
## [9,] 205.2 392.3
## [10,] 193.7 352.2
# 2. Create a data frame
beforeAfter2 <- data.frame(
group = rep(c("before", "after"), each = 10),
weight = c(before, after)
)
beforeAfter2## group weight
## 1 before 200.1
## 2 before 190.9
## 3 before 192.7
## 4 before 213.0
## 5 before 241.4
## 6 before 196.9
## 7 before 172.2
## 8 before 185.5
## 9 before 205.2
## 10 before 193.7
## 11 after 392.9
## 12 after 393.2
## 13 after 345.1
## 14 after 393.0
## 15 after 434.0
## 16 after 427.9
## 17 after 422.0
## 18 after 383.9
## 19 after 392.3
## 20 after 352.2
**H0: The mean weight is same before and after the treatment* H1: The mean weight is not same before and after the treatment
##
## Paired t-test
##
## data: weight by group
## t = 20.883, df = 9, p-value = 6.2e-09
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 173.4219 215.5581
## sample estimates:
## mean of the differences
## 194.49
##
## Paired t-test
##
## data: after and before
## t = 20.883, df = 9, p-value = 6.2e-09
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 173.4219 215.5581
## sample estimates:
## mean of the differences
## 194.49
The p-value is 6.2e-09, which is less than 0.05, hence we reject the null hypothesis. The mean weight is not same before and after the treatment.
The paired samples Wilcoxon test (also known as Wilcoxon signed-rank test) is a non-parametric alternative to paired t-test used to compare paired data. Itβs used when your data are not normally distributed.
**H0: The median weight is same before and after the treatment* H1: The median weight is not same before and after the treatment
# non-parametric Wilcoxon Test
res <- wilcox.test(weight ~ group, data = beforeAfter2, paired = TRUE)
res##
## Wilcoxon signed rank exact test
##
## data: weight by group
## V = 55, p-value = 0.001953
## alternative hypothesis: true location shift is not equal to 0
The p-value is 0.001953, which is less than 0.05, hence we reject the null hypothesis. The mean weight is not same before and after the treatment.