One-sample t-test in R

By Data Tricks, 28 July 2020

What is a one-sample t-test?

A one-sample t-test is used to test whether the mean of a population, from which a sample is drawn, is statistically different from a hypothesised value.

A t-test has a null hypothesis that the population mean is equal to the hypothesised value, and an alternative hypothesis that it is not equal. This is called a two-tailed t-test.

We can also perform a one-tailed t-test if we have a prior belief that the population mean is either larger or smaller than the hypothesised value. In a one-tailed test, the null hypothesis is the same but the alternative hypothesis is that the population mean is larger (or smaller, as appropriate) than the hypothesised value.

Example in R

Let’s say we have a sample of values as follows:

set.seed(150)
data <- data.frame(Value = rnorm(30, mean = 50, sd = 10))

We want to test whether the mean of those values is statistically different to 50. The null hypothesis is that the mean of the population is equal to the hypothesised mean (50), and the alternative hypothesis is that the mean is different (either above or below) the hypothesised mean.

To perform the one-sample t-test in R, use the following code:

test <- t.test(data$Value, mu = 50)

Now let’s analyse the output of the test:

> test

       One Sample t-test

data: data$Value
t = 0.57321, df = 29, p-value = 0.5709
alternative hypothesis: true mean is not equal to 50
95 percent confidence interval:
 47.02585 55.29045
sample estimates:
mean of x
 51.15815

p-value

The p-value is 0.5709, which is above the 5% significance level, therefore the null hypothesis cannot be rejected.

t-value

The t-value measures the size of the difference relative to the variation in the sample data. The greater the value of the t-value, the more likely it is that the null hypothesis should be rejected. In our example, the t-value is 0.57 which is small.

Degrees of freedom

In a t-test, one degree of freedom is “spent” estimating the mean, so the degrees of freedom will be n-1 – the number of values in the sample minus 1 – which in this case is 29.

95% confidence interval

The 95% confidence interval for our test is 47.03 to 55.29. This means that at the 5% significance level, the null hypothesis cannot be rejected for hypothesised means between 47.03 and 55.29.

Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Please note that your first comment on this site will be moderated, after which you will be able to comment freely.