Input proportions for two groups to calculate the z-score, p-value, and visualize the z-distribution.
Z-Score:
P-Value:
Understanding the Two-Proportion Z-Test
The two-proportion z-test is used to determine whether there’s a significant difference between two population proportions based on sample data. This test is commonly applied in situations where we want to compare the effectiveness of two groups, such as two treatments or interventions.
Key Components of the Two-Proportion Z-Test
- Sample Proportions (\( p_1 \) and \( p_2 \)): The observed proportions in each sample group.
- Pooled Proportion (\( p \)): The combined proportion of successes across both groups, used to calculate the standard error.
- Z-Score: A standardized measure of the difference between the two sample proportions, in terms of standard deviations.
Formula for the Two-Proportion Z-Test
The z-score for a two-proportion z-test is calculated as:
where:
- \( p_1 \): Sample proportion of group 1
- \( p_2 \): Sample proportion of group 2
- \( p \): Pooled proportion, calculated as \(\frac{x_1 + x_2}{n_1 + n_2}\), where \( x_1 \) and \( x_2 \) are the number of successes in each group.
- \( n_1 \): Sample size of group 1
- \( n_2 \): Sample size of group 2
Programmatically Calculating the Two-Proportion Z-Test
Below are examples for calculating the two-proportion z-test in JavaScript, Python, and R.
1. Using JavaScript (with jStat)
In JavaScript, the jStat library can be used to calculate the two-proportion z-test:
// Define inputs
const p1 = 0.5; // Proportion of successes in group 1
const n1 = 100; // Sample size of group 1
const p2 = 0.4; // Proportion of successes in group 2
const n2 = 100; // Sample size of group 2
// Calculate pooled proportion
const pooledP = (p1 * n1 + p2 * n2) / (n1 + n2);
// Calculate z-score
const zScore = (p1 - p2) / Math.sqrt(pooledP * (1 - pooledP) * (1 / n1 + 1 / n2));
// Calculate p-values based on z-score
const pValueTwoTailed = 2 * (1 - jStat.normal.cdf(Math.abs(zScore), 0, 1));
const pValueRightTailed = 1 - jStat.normal.cdf(zScore, 0, 1);
const pValueLeftTailed = jStat.normal.cdf(zScore, 0, 1);
console.log(`Two-tailed p-value: ${pValueTwoTailed.toFixed(5)}`);
console.log(`Right-tailed p-value: ${pValueRightTailed.toFixed(5)}`);
console.log(`Left-tailed p-value: ${pValueLeftTailed.toFixed(5)}`);
2. Using Python (with SciPy)
In Python, the SciPy library can be used to calculate the two-proportion z-test:
from scipy.stats import norm
# Define inputs
p1 = 0.5 # Proportion of successes in group 1
n1 = 100 # Sample size of group 1
p2 = 0.4 # Proportion of successes in group 2
n2 = 100 # Sample size of group 2
# Calculate pooled proportion
pooled_p = (p1 * n1 + p2 * n2) / (n1 + n2)
# Calculate z-score
z_score = (p1 - p2) / ((pooled_p * (1 - pooled_p) * (1 / n1 + 1 / n2)) ** 0.5)
# Calculate p-values
p_value_two_tailed = 2 * (1 - norm.cdf(abs(z_score)))
p_value_right_tailed = 1 - norm.cdf(z_score)
p_value_left_tailed = norm.cdf(z_score)
print(f"Two-tailed p-value: {p_value_two_tailed:.5f}")
print(f"Right-tailed p-value: {p_value_right_tailed:.5f}")
print(f"Left-tailed p-value: {p_value_left_tailed:.5f}")
3. Using R
In R, you can calculate the two-proportion z-test as follows:
# Define inputs
p1 <- 0.5 # Proportion of successes in group 1
n1 <- 100 # Sample size of group 1
p2 <- 0.4 # Proportion of successes in group 2
n2 <- 100 # Sample size of group 2
# Calculate pooled proportion
pooled_p <- (p1 * n1 + p2 * n2) / (n1 + n2)
# Calculate z-score
z_score <- (p1 - p2) / sqrt(pooled_p * (1 - pooled_p) * (1 / n1 + 1 / n2))
# Calculate p-values
p_value_two_tailed <- 2 * (1 - pnorm(abs(z_score)))
p_value_right_tailed <- 1 - pnorm(z_score)
p_value_left_tailed <- pnorm(z_score)
cat("Two-tailed p-value:", round(p_value_two_tailed, 5), "\n")
cat("Right-tailed p-value:", round(p_value_right_tailed, 5), "\n")
cat("Left-tailed p-value:", round(p_value_left_tailed, 5), "\n")
Further Reading
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.