Português

Chi-square test of independence

Association and correlation · reference distribution: χ²((r−1)(c−1))

When to use it

Check whether two categorical variables are associated, in a contingency table.

Null hypothesis

The two variables are independent.

Assumptions

Test statistic

\chi^2 = \sum_{i,j} \dfrac{(O_{ij} - E_{ij})^2}{E_{ij}}, \qquad E_{ij} = \dfrac{R_i\, C_j}{N}

Effect size

Cramér’s V, from 0 to 1; in a 2 × 2 table, it is φ.

V = \sqrt{\dfrac{\chi^2}{N\,(\min(r, c) - 1)}}

How to report it

χ²(2, N = 200) = 7.4, p = .025, V = .19

In R and Python

R
chisq.test(table(df$a, df$b))
Python
stats.chi2_contingency(pd.crosstab(df["a"], df["b"]))

In Python, stats is scipy.stats and np is numpy.

Variants and alternatives

Where it sits in the catalog

Association and correlation. Instead of comparing groups, they measure whether two variables move together, and how strongly.

In the decision tree

  1. What do you want to do? Measure the relationship between two variables
  2. What kind of variables are they? Both categorical
  3. Are all expected counts at least 5? Yes

It also appears alongside other tests:

Open the decision tree