How to do a hypothesis test for the difference between means when both population variances are known (in R)
Task
Assume we have two samples,
Related tasks:
- How to compute a confidence interval for the difference between two means when both population variances are known
- How to do a hypothesis test for a mean difference (matched pairs)
- How to do a hypothesis test for a population proportion
- How to do a hypothesis test for population variance
- How to do a hypothesis test for the difference between two proportions
- How to do a hypothesis test for the mean with known standard deviation
- How to do a hypothesis test for the ratio of two population variances
- How to do a hypothesis test of a coefficient’s significance
- How to do a one-sided hypothesis test for two sample means
- How to do a two-sided hypothesis test for a sample mean
- How to do a two-sided hypothesis test for two sample means
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
Two-tailed test
In a two-tailed test, the null hypothesis is that the difference is zero,
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.820494e-10
Our p-value is less than
Right-tailed test
In the right-tailed test, the null hypothesis is
The code is very similar to the previous, except only in computing the
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
Our
Left-tailed test
In a left-tailed test, the null hypothesis is
The code is very similar to the previous, except only in computing the
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] 9.102468e-11
Our
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Contributed by Elizabeth Czarniak (CZARNIA_ELIZ@bentley.edu)