Hypothesis testing is a statistical procedure used to make decisions about population parameters based on sample information.
Two commonly used tests for population means are:
We shall use a significance level of 5% throughout this document.
A one-sample Z-test is used to test a hypothesis about a population mean when the population standard deviation is known.
\[ Z = \frac{\bar X-\mu_0}{\sigma/\sqrt{n}} \]
Where:
Suppose the average height of a particular plant species is believed to be 50 cm. The population standard deviation is known to be 8 cm.
A random sample of 36 plants has an average height of 53 cm.
Test whether the population mean differs significantly from 50 cm.
\[ H_0:\mu=50 \]
\[ H_1:\mu\neq 50 \]
# Given information
xbar <- 53
mu0 <- 50
sigma <- 8
n <- 36
# Calculate Z statistic
z <- (xbar - mu0)/(sigma/sqrt(n))
# Calculate two-sided p-value
p_value <- 2 * pnorm(-abs(z))
# Display results
z
## [1] 2.25
p_value
## [1] 0.02444895
alpha <- 0.05
if(p_value < alpha){
print("Reject the null hypothesis")
} else {
print("Do not reject the null hypothesis")
}
## [1] "Reject the null hypothesis"
If the p-value is less than 0.05, we reject the null hypothesis.
We conclude that the population mean plant height differs significantly from 50 cm.
A two-sample Z-test is used to compare the means of two independent populations when their population standard deviations are known.
\[ Z = \frac{\bar X_1-\bar X_2} {\sqrt{\frac{\sigma_1^2}{n_1}+ \frac{\sigma_2^2}{n_2}}} \]
Two varieties of plants are grown under different conditions.
| Information | Variety A | Variety B |
|---|---|---|
| Sample mean | 52 | 48 |
| Population SD | 6 | 5 |
| Sample size | 40 | 35 |
Test whether the average heights differ significantly.
# Sample information
xbar1 <- 52
xbar2 <- 48
sigma1 <- 6
sigma2 <- 5
n1 <- 40
n2 <- 35
# Calculate Z statistic
z <- (xbar1 - xbar2) /
sqrt(sigma1^2/n1 + sigma2^2/n2)
# Calculate p-value
p_value <- 2 * pnorm(-abs(z))
z
## [1] 3.148254
p_value
## [1] 0.001642488
if(p_value < 0.05){
print("Reject the null hypothesis")
} else {
print("Do not reject the null hypothesis")
}
## [1] "Reject the null hypothesis"
A one-sample t-test is used to test whether a population mean is equal to a specified value when the population standard deviation is unknown.
\[ t = \frac{\bar X-\mu_0}{s/\sqrt n} \]
Degrees of freedom:
\[ df=n-1 \]
The following observations represent the heights (in cm) of 10 randomly selected plants.
height <- c(48, 52, 51, 49, 55,
53, 50, 54, 52, 56)
# Descriptive statistics
mean(height)
## [1] 52
sd(height)
## [1] 2.581989
length(height)
## [1] 10
Test whether the average height differs significantly from 50 cm.
\[ H_0:\mu=50 \]
\[ H_1:\mu\neq 50 \]
result <- t.test(height,
mu = 50,
alternative = "two.sided",
conf.level = 0.95)
result
##
## One Sample t-test
##
## data: height
## t = 2.4495, df = 9, p-value = 0.03679
## alternative hypothesis: true mean is not equal to 50
## 95 percent confidence interval:
## 50.15296 53.84704
## sample estimates:
## mean of x
## 52
result$statistic
## t
## 2.44949
result$parameter
## df
## 9
result$p.value
## [1] 0.0367875
result$conf.int
## [1] 50.15296 53.84704
## attr(,"conf.level")
## [1] 0.95
result$estimate
## mean of x
## 52
if(result$p.value < 0.05){
print("Reject the null hypothesis")
} else {
print("Do not reject the null hypothesis")
}
## [1] "Reject the null hypothesis"
An independent two-sample t-test is used to compare the means of two independent groups.
Suppose we want to compare plant heights under two different fertilizers.
fertilizer_A <- c(45, 48, 50, 52, 49,
51, 47, 53, 50, 48)
fertilizer_B <- c(55, 57, 54, 58, 56,
59, 53, 55, 57, 56)
\[ H_0:\mu_1=\mu_2 \]
\[ H_1:\mu_1\neq\mu_2 \]
mean(fertilizer_A)
## [1] 49.3
mean(fertilizer_B)
## [1] 56
sd(fertilizer_A)
## [1] 2.406011
sd(fertilizer_B)
## [1] 1.825742
result <- t.test(fertilizer_A,
fertilizer_B,
alternative = "two.sided",
var.equal = FALSE)
result
##
## Welch Two Sample t-test
##
## data: fertilizer_A and fertilizer_B
## t = -7.0149, df = 16.784, p-value = 2.235e-06
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -8.71707 -4.68293
## sample estimates:
## mean of x mean of y
## 49.3 56.0
If the population variances can reasonably be assumed equal, we can use the pooled-variance t-test.
result_pooled <- t.test(fertilizer_A,
fertilizer_B,
var.equal = TRUE)
result_pooled
##
## Two Sample t-test
##
## data: fertilizer_A and fertilizer_B
## t = -7.0149, df = 18, p-value = 1.51e-06
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -8.706597 -4.693403
## sample estimates:
## mean of x mean of y
## 49.3 56.0
if(result$p.value < 0.05){
print("Reject the null hypothesis")
} else {
print("Do not reject the null hypothesis")
}
## [1] "Reject the null hypothesis"
A paired t-test is used when two observations are related, such as measurements taken from the same experimental units before and after treatment.
\[ t = \frac{\bar d}{s_d/\sqrt n} \]
Where:
The following data represent the heights of five plants before and after applying fertilizer.
before <- c(20, 22, 19, 21, 23)
after <- c(23, 25, 21, 24, 26)
\[ H_0:\mu_d=0 \]
\[ H_1:\mu_d\neq 0 \]
result <- t.test(after,
before,
paired = TRUE,
alternative = "two.sided")
result
##
## Paired t-test
##
## data: after and before
## t = 14, df = 4, p-value = 0.000151
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## 2.244711 3.355289
## sample estimates:
## mean difference
## 2.8
difference <- after - before
difference
## [1] 3 3 2 3 3
mean(difference)
## [1] 2.8
sd(difference)
## [1] 0.4472136
if(result$p.value < 0.05){
print("Reject the null hypothesis")
} else {
print("Do not reject the null hypothesis")
}
## [1] "Reject the null hypothesis"
| Basis | Z-test | t-test |
|---|---|---|
| Population SD | Known | Unknown |
| Distribution | Normal | Student’s t |
| Test statistic | Z | t |
| Degrees of freedom | Not required | Required |
Note: When the population standard deviation is unknown, the t-test is generally appropriate, even for large samples.
| Function | Purpose |
|---|---|
pnorm() |
Normal distribution probability |
qnorm() |
Normal distribution quantile |
pt() |
t-distribution probability |
qt() |
t-distribution quantile |
mean() |
Sample mean |
sd() |
Sample standard deviation |
t.test() |
Performs t-tests |
For the Z-test, we calculated the test statistic and p-value using the normal distribution functions.
A machine is designed to produce seeds with an average weight of 10 grams. The population standard deviation is known to be 2 grams.
A random sample of 64 seeds has an average weight of 10.5 grams.
Test whether the average weight differs significantly from 10 grams at the 5% significance level.
The following observations represent the yields (kg) of 10 plants.
yield <- c(18, 20, 22, 19, 21,
23, 20, 19, 22, 21)
Test whether the population mean yield differs significantly from 20 kg.
Two groups of plants receive different treatments.
group1 <- c(12, 14, 15, 13, 16, 14)
group2 <- c(17, 18, 16, 19, 17, 20)
Use an independent two-sample t-test to compare their average growth.
The following observations represent plant growth before and after treatment.
before <- c(10, 12, 11, 13, 12, 14)
after <- c(12, 14, 13, 15, 14, 16)
Apply a paired t-test to examine whether the treatment produces a significant change in plant growth.
Z-tests and t-tests are important tools for testing hypotheses about population means.
R makes these procedures easier by providing functions for calculating test statistics, p-values and confidence intervals.