Skip to content

Chi-Square Test (categorical association)

Use the chi-square test of independence when you have:

  • Two categorical variables
  • Counts in a contingency table

Example questions:

  • Is purchase (yes/no) associated with plan type (basic/pro)?
  • Is churn associated with region?
Chi-square test
import pandas as pd
from scipy.stats import chi2_contingency
 
# Example contingency table
# rows: plan, columns: churn
ct = pd.DataFrame(
    {
        "churn_no": [80, 120],
        "churn_yes": [20, 60],
    },
    index=["basic", "pro"],
)
 
chi2, p, dof, expected = chi2_contingency(ct)
print("chi2:", chi2)
print("p:", p)
print("dof:", dof)
print("expected:\n", expected)
  • Small p-value → evidence of association
  • Expected counts should not be too small (rule of thumb: mostly >= 5)

After significance, consider effect size like Cramér’s V.

diagram Chi-square test of independence mermaid
Observed counts are compared against the counts you'd expect if the two variables were unrelated.

Continue to Correlation vs Causation to measure how two numeric variables relate.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading