Perhaps the most widely used statistical analysis for better or worse is the t-test. Here’s a quick summary of how to call the t-test for one sample using R. The function name is t.test and the main parameters are the data, the test type (alternative=), the mean (mu=), and the confidence level (conf.level=).
The hardest part about t-tests in R is knowing how to set up the problem. In these examples the null hypothesis has a mean of 4 (H0: μ = 4) and I have tested the three different alternative hypothesis options: H1: μ ≠ 3, H1: μ < 3, and H1: μ > 3. In each test I have used a 95% confidence interval (alpha = 0.05). Note: A t-test would not be performed in this manner using all three alternatives, this is merely for example purposes.
Here’s an example of a one-sided t test using the vector x.
x = c(1,2,4,7,4,3,7,8,3,9)
t.test(x, alternative="two.sided", mu = 3, conf.level = 0.95)
##
## One Sample t-test
##
## data: x
## t = 2.0769, df = 9, p-value = 0.0676
## alternative hypothesis: true mean is not equal to 3
## 95 percent confidence interval:
## 2.839464 6.760536
## sample estimates:
## mean of x
## 4.8
t.test(x, alternative="less", mu = 3, conf.level = 0.95)
##
## One Sample t-test
##
## data: x
## t = 2.0769, df = 9, p-value = 0.9662
## alternative hypothesis: true mean is less than 3
## 95 percent confidence interval:
## -Inf 6.388698
## sample estimates:
## mean of x
## 4.8
t.test(x, alternative="greater", mu = 3, conf.level = 0.95)
##
## One Sample t-test
##
## data: x
## t = 2.0769, df = 9, p-value = 0.0338
## alternative hypothesis: true mean is greater than 3
## 95 percent confidence interval:
## 3.211302 Inf
## sample estimates:
## mean of x
## 4.8
Let’s analyze the results above starting with the alternative hypothesis that the true mean is not equal to 3 (H1: μ ≠ 3). The results show that the 95% confidence interval for the true mean is 2.84 to 6.76. Since the null hypothesis states that the true mean is 3 (H0: μ = 3), and 3 is within the 95% confidence interval, the null hypothesis is unlikely to be rejected. The p-value is 0.076, which is greater than the alpha value of 0.05 (or 1-confidenc interval 0.95). Since the p-value is not less than 0.05, the alternative hypothesis that the true mean is not equal to 3 is rejected in favor of the null hypothesis. Some other useful information the t-test provides is the degrees of freedom (9) and the t-statistic 2.08.
Let’s look at the results from the alternative hypothesis that the true mean is less than 3 (H1: μ < 3). The 95% confidence interval is less than 6.39 and the p-value is 0.966. Once again the p-value is greater than the alpha value of 0.05 (or 1-0.95), so the alternative hypothesis that the sample mean is less than 3 (H1: μ < 3) is rejected in favor of the null hypothesis that the true mean is 3 (H0: μ = 3).
Finally let’s look at the results from the t-test using the alternative hypothesis that the true mean is greater than 3 (H1: μ > 3). The 95% confidence interval is 3.21 and greater, which does not include the value of the null hypothesis. In this case the p-value is 0.0338, which IS less than the alpha value of 0.05 (or 1-0.95) and the null hypothesis (H0: μ = 3) is rejected in favor of the alternative hypothesis (H1: μ > 3) that the true mean is greater than 3.