Chi-Square Test of Independence
Hypothesis Testing
Discovering Hidden Associations
The chi-square test of independence reveals whether two categorical variables are related, forming the foundation of association analysis. It answers the fundamental question: are these variables connected?
- Market Research — Identifying associations between demographics and purchasing behavior
- Epidemiology — Discovering links between risk factors and disease outcomes
- Social Science — Testing relationships between education, income, and other factors
The independence test uncovers the structure hidden in contingency tables.
Tests whether two categorical variables are independent (not associated) in a contingency table.
Worked Example: Gender vs Learning Style
Heatmap Visualization
# Heatmap
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
sns.heatmap(contingency, annot=True, fmt='d', cmap='Blues', ax=axes[0])
axes[0].set_title('Observed Frequencies')
sns.heatmap(resid_df, annot=True, fmt='.2f', cmap='RdBu_r', center=0,
vmin=-3, vmax=3, ax=axes[1])
axes[1].set_title('Standardized Residuals\n(Red = higher than expected, Blue = lower)')
plt.tight_layout()
plt.savefig('chi_square_independence.png', dpi=150)
plt.show()