Comparing the means of paired samples
There are mainly two techniques are used to compare the means of paired samples. These two techniques are:
- Paired sample T-test
- Paired Samples Wilcoxon Test
Paired sample T-test
This is a statistical procedure that is used to determine whether the mean difference between two sets of observations is zero. In a paired sample t-test, each subject is measured two times, resulting in pairs of observations.
Implementation in R:
For performing a one-sample t-test in R, use the function t.test(). The syntax for the function is given below.
Syntax: t.test(x, y, paired =TRUE)
Parameters:
- x, y: numeric vectors
- paired: a logical value specifying that we want to compute a paired t-test
Example:
R
# R program to illustrate # Paired sample t-test set.seed (0) # Taking two numeric vectors shopOne <- rnorm (50, mean = 140, sd = 4.5) shopTwo <- rnorm (50, mean = 150, sd = 4) # Using t.tset() result = t.test (shopOne, shopTwo, var.equal = TRUE ) # Print the result print (result) |
Output:
Two Sample t-test
data: shopOne and shopTwo
t = -13.158, df = 98, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-11.482807 -8.473061
sample estimates:
mean of x mean of y
140.1077 150.0856
Paired Samples Wilcoxon Test
The paired samples Wilcoxon test is a non-parametric alternative to paired t-test used to compare paired data. It’s used when data are not normally distributed.
Implementation in R:
To perform Paired Samples Wilcoxon-test, the R provides a function wilcox.test() that can be used as follows:
Syntax: wilcox.test(x, y, paired = TRUE, alternative = “two.sided”)
Parameters:
- x, y: numeric vectors
- paired: a logical value specifying that we want to compute a paired Wilcoxon test
- alternative: the alternative hypothesis. Allowed value is one of “two.sided” (default), “greater” or “less”.
Example: Here, let’s use an example data set, which contains the weight of 10 rabbits before and after the treatment. We want to know, if there is any significant difference in the median weights before and after treatment?
R
# R program to illustrate # Paired Samples Wilcoxon Test # The data set # Weight of the rabbit before treatment before <- c (190.1, 190.9, 172.7, 213, 231.4, 196.9, 172.2, 285.5, 225.2, 113.7) # Weight of the rabbit after treatment after <- c (392.9, 313.2, 345.1, 393, 434, 227.9, 422, 383.9, 392.3, 352.2) # Create a data frame myData <- data.frame ( group = rep ( c ( "before" , "after" ), each = 10), weight = c (before, after) ) # Print all data print (myData) # Paired Samples Wilcoxon Test result = wilcox.test (before, after, paired = TRUE ) # Printing the results print (result) |
Output:
group weight 1 before 190.1 2 before 190.9 3 before 172.7 4 before 213.0 5 before 231.4 6 before 196.9 7 before 172.2 8 before 285.5 9 before 225.2 10 before 113.7 11 after 392.9 12 after 313.2 13 after 345.1 14 after 393.0 15 after 434.0 16 after 227.9 17 after 422.0 18 after 383.9 19 after 392.3 20 after 352.2 Wilcoxon signed rank test data: before and after V = 0, p-value = 0.001953 alternative hypothesis: true location shift is not equal to 0
In the above output, the p-value of the test is 0.001953, which is less than the significance level alpha = 0.05. We can conclude that the median weight of the mice before treatment is significantly different from the median weight after treatment with a p-value = 0.001953.
Comparing Means in R Programming
There are many cases in data analysis where you’ll want to compare means for two populations or samples and which technique you should use depends on what type of data you have and how that data is grouped together. The comparison of means tests helps to determine if your groups have similar means. So this article contains statistical tests to use for comparing means in R programming. These tests include:
Contact Us