Worms in Apples (One Sample Proportion Test)

Suppose we want to test if the proportion of apples with worms in an orchard is different from the commonly accepted proportion of 5%. We will use a one-sample proportion test to determine if there is a significant difference.

Step 1: State the Hypotheses

The null hypothesis H_0 and the alternative hypothesis H_A can be stated as follows:
H_0: p = 0.05 \quad \text{(the proportion of apples with worms is 5\%)}
H_A: p \neq 0.05 \quad \text{(the proportion of apples with worms is not 5\%)}
where p is the true proportion of apples with worms.

Step 2: Collect Data

Suppose we randomly sample 150 apples from the orchard and find that 12 of them have worms. We can summarize this information as follows:
n = 150 \quad \text{(total number of apples sampled)}
x = 12 \quad \text{(number of apples with worms)}
\hat{p} = \frac{x}{n} = \frac{12}{150} = 0.08 \quad \text{(sample proportion)}

Step 3: Calculate the Test Statistic

The test statistic for a one-sample proportion test is calculated using the formula:
z = \frac{\hat{p} - p_0}{\sqrt{\frac{p_0 (1 - p_0)}{n}}}
where p_0 is the null hypothesis proportion (0.05 in this case).

z = \frac{0.08 - 0.05}{\sqrt{\frac{0.05 \times (1 - 0.05)}{150}}} = \frac{0.03}{\sqrt{\frac{0.05 \times 0.95}{150}}} \approx 1.48

Step 4: Determine the Critical Value

Using a standard normal distribution (z-distribution), we find the critical value for a two-tailed test at the \alpha = 0.05 significance level is approximately \pm 1.96.

Step 5: Make a Decision

Since the calculated z-statistic (1.48) is less than the critical value (1.96), we fail to reject the null hypothesis.

Conclusion

At the \alpha = 0.05 significance level, we do not have enough evidence to conclude that the proportion of apples with worms is different from 5%. Therefore, we fail to reject the null hypothesis that the proportion of apples with worms in the orchard is 5%.

implementation

Python

import numpy as np
from statsmodels.stats.proportion import proportions_ztest

# Data
n = 150  # total number of apples sampled
x = 12   # number of apples with worms
p0 = 0.05  # null hypothesis proportion

# Sample proportion
p_hat = x / n

# Perform one-sample proportion z-test
z_statistic, p_value = proportions_ztest(count=x, nobs=n, value=p0)

# Print results
print(f"Sample Proportion: {p_hat}")
print(f"Z-Statistic: {z_statistic}")
print(f"P-Value: {p_value}")

# Conclusion based on alpha = 0.05
alpha = 0.05
if p_value < alpha:
    print("Reject the null hypothesis.")
else:
    print("Fail to reject the null hypothesis.")

R

# Data
n <- 150  # total number of apples sampled
x <- 12   # number of apples with worms
p0 <- 0.05  # null hypothesis proportion

# Sample proportion
p_hat <- x / n

# Perform one-sample proportion z-test
z_statistic <- (p_hat - p0) / sqrt(p0 * (1 - p0) / n)
p_value <- 2 * (1 - pnorm(abs(z_statistic)))  # two-tailed test

# Print results
cat("Sample Proportion:", p_hat, "\n")
cat("Z-Statistic:", z_statistic, "\n")
cat("P-Value:", p_value, "\n")

# Conclusion based on alpha = 0.05
alpha <- 0.05
if (p_value < alpha) {
    cat("Reject the null hypothesis.\n")
} else {
    cat("Fail to reject the null hypothesis.\n")
}


Discover more from Science Comics

Subscribe to get the latest posts sent to your email.

Leave a Reply

error: Content is protected !!