Performing a one-sample t-test in R

One-sample t-test

A t-test is used to test hypotheses about the mean value of a population from which a sample is drawn. A t-test is suitable if the data is believed to be drawn from a normal distribution, or if the sample size is large.

A one-sample t-test is used to compare the mean value of a sample with a constant value denoted μ0. The test has the null hypothesis that the population mean is equal to μ0 and the alternative hypothesis that it is not equal to μ0.

The test can also be performed with a one-sided alternative hypothesis, which is known as a one-tailed test. The one-sided alternative hypothesis is either than the population mean is less than μ0 or that the population mean is greater than μ0.

You can perform a one-sample t-test with the t.test function. To compare a sample mean with a constant value mu0, use the command:

> t.test(dataset$sample1, mu=mu0)

The mu argument gives the value with which you want to compare the sample mean. It is optional and has a default value of zero.

By default, R performs a two-tailed test. To perform a one-tailed test, set the alternative argument to "greater" or "less", as shown below.

> t.test(dataset$sample1, mu=mu0, alternative="greater")

A 95% confidence interval for the population mean is included with the output. To adjust the size of the interval, use the conf.level argument.

> t.test(dataset$sample1, mu=mu0, conf.level=0.99)

Example 10.1. One-tailed, one-sample t-test using the bottles data

A bottle filling machine is set to fill bottles with soft drink to a volume of 500 ml. The actual volume is known to follow a normal distribution. The manufacturer believes the machine is under-filling bottles. A sample of 20 bottles is taken and the volume of liquid inside is measured. The results are given in the bottles dataset, which is available here.

> bottles
1  484.11
2  459.49
3  471.38
4  512.01
5  494.48
6  528.63
7  493.64
8  485.03
9  473.88
10 501.59
11 502.85
12 538.08
13 465.68
14 495.03
15 475.32
16 529.41
17 518.13
18 464.32
19 449.08
20 489.27

To calculate the sample mean, use the command:

> mean(bottles$Volume)
[1] 491.5705

Suppose you want to use a one-sample t-test to determine whether the bottles are being consistently under filled, or whether the low mean volume for the sample is purely the result of random variation. A one-sided test is suitable because the manufacturer is specifically interested in knowing whether the volume is less than 500 ml. The test has the null hypothesis that the mean filling volume is equal to 500 ml, and the alternative hypothesis that the mean filling volume is less than 500 ml. A significance level of 0.01 is to be used.

To perform the test, use the command:

> t.test(bottles$Volume, mu=500, alternative="less", conf.level=0.99)

This gives the following output:

One Sample t-test
data: bottles$Volume
t = -1.5205, df = 19, p-value = 0.07243
alternative hypothesis: true mean is less than 500
99 percent confidence interval:
     -Inf 505.6495
sample estimates:
mean of x

From the output, we can see that the mean bottle volume for the sample is 491.6 ml. The one-sided 99% confidence interval tells us that mean filling volume is likely to be less than 505.6 ml. The p-value of 0.07243 tells us that if the mean filling volume of the machine were 500 ml, the probability of selecting a sample with a mean volume less than or equal to this one would be approximately 7%.

Since the p-value is not less than the significance level of 0.01, we cannot reject the null hypothesis that the mean filling volume is equal to 500 ml. This means that there is no evidence that the bottles are being under-filled.

Social Widgets powered by