1 Introduction

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:

  1. Z-test
  2. t-test

1.1 General steps of hypothesis testing

  1. Formulate the null and alternative hypotheses.
  2. Specify the level of significance.
  3. Calculate the test statistic.
  4. Calculate the p-value.
  5. Make a statistical decision.
  6. Interpret the result.

We shall use a significance level of 5% throughout this document.

2 Part A: Z-test

2.1 1. One-sample Z-test

A one-sample Z-test is used to test a hypothesis about a population mean when the population standard deviation is known.

2.1.1 Test statistic

\[ Z = \frac{\bar X-\mu_0}{\sigma/\sqrt{n}} \]

Where:

  • \(\bar X\) = Sample mean
  • \(\mu_0\) = Hypothesized population mean
  • \(\sigma\) = Population standard deviation
  • \(n\) = Sample size

2.1.2 Example

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.

2.1.3 Hypotheses

\[ H_0:\mu=50 \]

\[ H_1:\mu\neq 50 \]

2.1.4 R implementation

# 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

2.1.5 Decision

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"

2.1.6 Interpretation

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.

2.2 2. Two-sample Z-test

A two-sample Z-test is used to compare the means of two independent populations when their population standard deviations are known.

2.2.1 Test statistic

\[ Z = \frac{\bar X_1-\bar X_2} {\sqrt{\frac{\sigma_1^2}{n_1}+ \frac{\sigma_2^2}{n_2}}} \]

2.2.2 Example

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.

2.2.3 R implementation

# 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

2.2.4 Decision

if(p_value < 0.05){
  print("Reject the null hypothesis")
} else {
  print("Do not reject the null hypothesis")
}
## [1] "Reject the null hypothesis"

3 Part B: t-test

3.1 3. One-sample t-test

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.

3.1.1 Test statistic

\[ t = \frac{\bar X-\mu_0}{s/\sqrt n} \]

Degrees of freedom:

\[ df=n-1 \]

3.1.2 Example

The following observations represent the heights (in cm) of 10 randomly selected plants.

3.1.3 R implementation

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.

3.1.4 Hypotheses

\[ H_0:\mu=50 \]

\[ H_1:\mu\neq 50 \]

3.1.5 Performing the t-test

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

3.1.6 Extracting important results

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

3.1.7 Decision

if(result$p.value < 0.05){
  print("Reject the null hypothesis")
} else {
  print("Do not reject the null hypothesis")
}
## [1] "Reject the null hypothesis"

3.2 4. Independent two-sample t-test

An independent two-sample t-test is used to compare the means of two independent groups.

3.2.1 Example

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)

3.2.2 Hypotheses

\[ H_0:\mu_1=\mu_2 \]

\[ H_1:\mu_1\neq\mu_2 \]

3.2.3 Descriptive statistics

mean(fertilizer_A)
## [1] 49.3
mean(fertilizer_B)
## [1] 56
sd(fertilizer_A)
## [1] 2.406011
sd(fertilizer_B)
## [1] 1.825742

3.2.4 Welch’s two-sample t-test

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

3.2.5 Pooled two-sample t-test

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

3.2.6 Decision

if(result$p.value < 0.05){
  print("Reject the null hypothesis")
} else {
  print("Do not reject the null hypothesis")
}
## [1] "Reject the null hypothesis"

3.3 5. Paired t-test

A paired t-test is used when two observations are related, such as measurements taken from the same experimental units before and after treatment.

3.3.1 Test statistic

\[ t = \frac{\bar d}{s_d/\sqrt n} \]

Where:

  • \(\bar d\) = Mean of paired differences
  • \(s_d\) = Standard deviation of paired differences
  • \(n\) = Number of pairs

3.3.2 Example

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)

3.3.3 Hypotheses

\[ H_0:\mu_d=0 \]

\[ H_1:\mu_d\neq 0 \]

3.3.4 Performing the paired t-test

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

3.3.5 Examining paired differences

difference <- after - before

difference
## [1] 3 3 2 3 3
mean(difference)
## [1] 2.8
sd(difference)
## [1] 0.4472136

3.3.6 Decision

if(result$p.value < 0.05){
  print("Reject the null hypothesis")
} else {
  print("Do not reject the null hypothesis")
}
## [1] "Reject the null hypothesis"

4 Part C: Comparison and Important R Functions

4.1 6. Comparison between Z-test and t-test

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.

4.2 7. Important R functions

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.

5 Part D: Practice Problems

5.1 Problem 1: One-sample Z-test

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.

5.2 Problem 2: One-sample t-test

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.

5.3 Problem 3: Independent two-sample t-test

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.

5.4 Problem 4: Paired t-test

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.

6 Conclusion

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.