How to run a chi-square test of independence

The chi-square test of independence evaluates whether two categorical variables are associated in a population. It applies when you have counts of independent observations organized in a contingency table, provided that the expected cell counts are sufficiently large.

The setup

Define the null hypothesis H0H_0 as the two variables being independent, and the alternative hypothesis HaH_a as the two variables being dependent. Organize your raw frequency data into an rimescr imes c contingency table, where rr is the number of rows (categories of the first variable) and cc is the number of columns (categories of the second variable). Let OijO_{ij} represent the observed count in row ii and column jj.

The steps

  1. Calculate the sum of each row, the sum of each column, and the grand total nn. 2. Calculate the expected count for each cell using the formula Eij=(extRowiextTotal)imes(extColumnjextTotal)nE_{ij} = \frac{( ext{Row } i ext{ Total}) imes ( ext{Column } j ext{ Total})}{n}. 3. Compute the chi-square test statistic using χ2=(OijEij)2Eij\chi^2 = \sum \frac{(O_{ij} - E_{ij})^2}{E_{ij}} summing over all cells. 4. Determine the degrees of freedom using df=(r1)(c1)df = (r - 1)(c - 1). 5. Compare the calculated χ2\chi^2 value to the critical value from the chi-square distribution table for the chosen significance level α\alpha and calculated dfdf, or find the p-value. Reject H0H_0 if χ2\chi^2 is greater than the critical value.

Checking the result

Verify that the sum of all expected counts exactly equals the sum of all observed counts (the grand total nn). The test statistic χ2\chi^2 must always be non-negative. If χ2=0\chi^2 = 0, the observed counts perfectly match the expected counts.

Common errors

A frequent error is using percentages, proportions, or fractions instead of raw frequency counts; the chi-square formula strictly requires raw counts. Another common error is proceeding with the test when one or more expected cell counts are less than 5, which invalidates the chi-square distribution approximation.

Worked example

A researcher wants to know if preferred exercise type (Cardio vs. Weights) is independent of shift worked (Day vs. Night). The observed counts are: Day/Cardio = 30, Day/Weights = 10, Night/Cardio = 15, Night/Weights = 45. Test at α=0.05\alpha = 0.05.

Hypotheses: H0H_0: Exercise type and shift are independent. HaH_a: Exercise type and shift are dependent. Row totals: Day = 30 + 10 = 40. Night = 15 + 45 = 60. Column totals: Cardio = 30 + 15 = 45. Weights = 10 + 45 = 55. Grand total nn = 100. Expected counts: EextDay,Cardio=(40imes45)/100=18E_{ ext{Day,Cardio}} = (40 imes 45) / 100 = 18. EextDay,Weights=(40imes55)/100=22E_{ ext{Day,Weights}} = (40 imes 55) / 100 = 22. EextNight,Cardio=(60imes45)/100=27E_{ ext{Night,Cardio}} = (60 imes 45) / 100 = 27. EextNight,Weights=(60imes55)/100=33E_{ ext{Night,Weights}} = (60 imes 55) / 100 = 33. Test statistic: χ2=(3018)218+(1022)222+(1527)227+(4533)233=14418+14422+14427+14433=8.000+6.545+5.333+4.364=24.242\chi^2 = \frac{(30-18)^2}{18} + \frac{(10-22)^2}{22} + \frac{(15-27)^2}{27} + \frac{(45-33)^2}{33} = \frac{144}{18} + \frac{144}{22} + \frac{144}{27} + \frac{144}{33} = 8.000 + 6.545 + 5.333 + 4.364 = 24.242. Degrees of freedom: df=(21)(21)=1df = (2 - 1)(2 - 1) = 1. Critical value for α=0.05\alpha = 0.05 with df=1df = 1 is 3.841. Since 24.242 > 3.841, reject H0H_0. Exercise type and shift are dependent.

FAQ

Run your own problem

References: OpenStax Introductory Statistics, Chapter 11 · Khan Academy: Chi-square tests for categorical data

See also