# How to do a hypothesis test for the difference between means when both population variances are known (in R)

See all solutions.

Assume we have two samples, $x_1, x_2, \ldots, x_n$ and $x’_1, x’_2, \ldots, x’_n$, that come from normally distributed populations with known variances, and the two sample means are $\bar{x}$ and $\bar{x}’$, respectively. We might want to ask whether the difference $\bar{x}-\bar{x}’$ is significantly different from, greater than, or less than zero.

## Solution

We’re going to use fake data here, but you can replace our fake data with your real data below. You will need not only the samples but also the known population standard deviations.

1
2
3
4
sample1 <- c(5, 8, 10, 3, 6, 2)
sample2 <- c(13, 20, 16, 12, 18, 15)
population1_sd = 2.4
population2_sd = 3


We must compute the sizes and means of the two samples.

1
2
3
4
n1 <- length(sample1)
n2 <- length(sample2)
sample1_mean <- mean(sample1)
sample2_mean <- mean(sample2)


We choose a value $0 \le \alpha \le 1$ as the probability of a Type I error (a false positive, finding we should reject $H_0$ when it’s actually true). We will use $\alpha=0.05$ in this example.

### Two-tailed test

In a two-tailed test, the null hypothesis is that the difference is zero, $H_0: \bar{x} - \bar{x}’ = 0$. We compute a test statistic and $p$-value as follows.

1
2
3
test_statistic <- (sample1_mean - sample2_mean) /
sqrt(population1_sd^2/n1 + population2_sd^2/n2)
2*pnorm(abs(test_statistic), lower.tail = FALSE)  # two-tailed p-value

1
[1] 1.820494e-10


Our p-value is less than $\alpha$, so we have sufficient evidence to reject the null hypothesis. The difference between the means is significantly different from zero.

### Right-tailed test

In the right-tailed test, the null hypothesis is $H_0: \bar{x} - \bar{x}’ \le 0$. That is, we are testing whether the difference is greater than zero.

The code is very similar to the previous, except only in computing the $p$-value. We repeat the code that’s in common, to make it easier to copy and paste the examples.

1
2
3
test_statistic <- (sample1_mean - sample2_mean) /
sqrt(population1_sd^2/n1 + population2_sd^2/n2)
pnorm(test_statistic, lower.tail = FALSE)  # right-tailed p-value

1
[1] 1


Our $p$-value is greater than $\alpha$, so we do not have sufficient evidence to reject the null hypothesis. We would continue to assume that the difference in means is less than or equal to zero.

### Left-tailed test

In a left-tailed test, the null hypothesis is $H_0: \bar{x} - \bar{x}’ \ge 0$. That is, we are testing whether the difference is less than zero.

The code is very similar to the previous, except only in computing the $p$-value. We repeat the code that’s in common, to make it easier to copy and paste the examples.

1
2
3
test_statistic <- (sample1_mean - sample2_mean) /
sqrt(population1_sd^2/n1 + population2_sd^2/n2)
pnorm(test_statistic, lower.tail = TRUE)  # left-tailed p-value

1
[1] 9.102468e-11


Our $p$-value is less than $\alpha$, so we have sufficient evidence to reject the null hypothesis. The difference between the means is significantly less than zero.