Comparing the means of one-sample data
There are mainly two techniques used to compare the one-sample mean to a standard known mean. These two techniques are:
- One Sample T-test
- One-Sample Wilcoxon Test
One Sample T-test
The One-Sample T-Test is used to test the statistical difference between a sample mean and a known or assumed/hypothesized value of the mean in the population.
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, mu = 0)
Parameters:
- x: the name of the variable of interest
- mu: set equal to the mean specified by the null hypothesis
Example:
R
# R program to illustrate # One sample t-test set.seed (0) sweetSold <- c ( rnorm (50, mean = 140, sd = 5)) # Ho: mu = 150 # Using the t.test() result = t.test (sweetSold, mu = 150) # Print the result print (result) |
Output:
One Sample t-test data: sweetSold t = -15.249, df = 49, p-value < 2.2e-16 alternative hypothesis: true mean is not equal to 150 95 percent confidence interval: 138.8176 141.4217 sample estimates: mean of x 140.1197
One-Sample Wilcoxon Test
The one-sample Wilcoxon signed-rank test is a non-parametric alternative to a one-sample t-test when the data cannot be assumed to be normally distributed. It’s used to determine whether the median of the sample is equal to a known standard value i.e. a theoretical value.
Implementation in R:
To perform a one-sample Wilcoxon-test, R provides a function wilcox.test() that can be used as follows:
Syntax: wilcox.test(x, mu = 0, alternative = “two.sided”)
Parameters:
- x: a numeric vector containing your data values
- mu: the theoretical mean/median value. Default is 0 but you can change it.
- alternative: the alternative hypothesis. Allowed value is one of “two.sided” (default), “greater” or “less”.
Example: Here, let’s use an example data set containing the weight of 10 rabbits. Let’s know if the median weight of the rabbit differs from 25g?
R
# R program to illustrate # one-sample Wilcoxon signed-rank test # The data set set.seed (1234) myData = data.frame ( name = paste0 ( rep ( "R_" , 10), 1:10), weight = round ( rnorm (10, 30, 2), 1) ) # Print the data print (myData) # One-sample wilcoxon test result = wilcox.test (myData$weight, mu = 25) # Printing the results print (result) |
Output:
name weight 1 R_1 27.6 2 R_2 30.6 3 R_3 32.2 4 R_4 25.3 5 R_5 30.9 6 R_6 31.0 7 R_7 28.9 8 R_8 28.9 9 R_9 28.9 10 R_10 28.2 Wilcoxon signed rank test with continuity correction data: myData$weight V = 55, p-value = 0.005793 alternative hypothesis: true location is not equal to 25
In the above output, the p-value of the test is 0.005793, which is less than the significance level alpha = 0.05. So we can reject the null hypothesis and conclude that the average weight of the rabbit is significantly different from 25g with a p-value = 0.005793.
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: