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 and the alternative hypothesis
can be stated as follows:
where 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:
Step 3: Calculate the Test Statistic
The test statistic for a one-sample proportion test is calculated using the formula:
where is the null hypothesis proportion (0.05 in this case).
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 significance level is approximately
.
Step 5: Make a Decision
Since the calculated -statistic (1.48) is less than the critical value (1.96), we fail to reject the null hypothesis.
Conclusion
At the 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.