# Unpaired two-sample t-test

By Data Tricks, 28 July 2020

### What is an unpaired two-samples t-test?

An unpaired t-test is used to compare the means of two unrelated groups of samples. It is sometimes called an independent t-test or independent samples t-test.

An unpaired two-samples t-test has a null hypothesis that the means of the two samples are the same. The alternative hypothesis is that the means are not the same, which is called a two-tailed test.

As with a paired t-test, we can perform a one-tailed test where the alternative hypothesis is that the mean of one sample is either higher or lower than the mean of the other.

### Example in R

Let’s say we have a some data on student performance based on gender:

set.seed(150)
data <- data.frame(male = rnorm(100, mean = 50, sd = 10),
female = rnorm(100, mean = 50, sd = 10))

We want to test whether there is a statistically significant difference between the performance of male and female students. The null hypothesis is that there is no difference, and the alternative hypothesis is that there is a difference.

To perform an unpaired samples t-test in R, use the following code:

test <- t.test(x = data$male, y = data$female, alternative = "two.sided", paired = FALSE)

Now analyse the output of the test:

> test

Welch Two Sample t-test

data: data$male and data$female
t = -0.22376, df = 194.96, p-value = 0.8232
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-3.030858 2.413206
sample estimates:
mean of x mean of y
50.16018  50.46900

You will notice that the t.test function has performed a Welch unpaired samples t-test. Welch’s t-test is an adaptation of student’s t-test and is useful when the two samples have unequal variances or unequal sample sizes. Neither of these scenarios is true in our example, but there is probably very little difference in the result of a Welch or student’s t-test.

#### p-value

The p-value is 0.82 which is above the 5% significance level, therefore the null hypothesis cannot be rejected. This indicates that the means of male and female students are equal.

#### 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.

#### Degrees of freedom

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

#### 95% confidence interval

The 95% confidence interval for our test is -3.0 to 2.4. This means that at the 5% significance level, the mean difference falls somewhere between -3.0 and 2.4.

## Is an unpaired two-sample t-test the right test?

Use our interactive tool to help you choose the right statistical test or read our article on how to choose the right statistical test.

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

## Free data science in R guide

Sign up to our newsletter and we will send you a series of guides containing tips and tricks on data science and machine learning in R.

No thanks