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 as the two variables being independent, and the alternative hypothesis as the two variables being dependent. Organize your raw frequency data into an contingency table, where is the number of rows (categories of the first variable) and is the number of columns (categories of the second variable). Let represent the observed count in row and column .
The steps
- Calculate the sum of each row, the sum of each column, and the grand total . 2. Calculate the expected count for each cell using the formula . 3. Compute the chi-square test statistic using summing over all cells. 4. Determine the degrees of freedom using . 5. Compare the calculated value to the critical value from the chi-square distribution table for the chosen significance level and calculated , or find the p-value. Reject if 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 ). The test statistic must always be non-negative. If , 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 .
Hypotheses: : Exercise type and shift are independent. : 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 = 100. Expected counts: . . . . Test statistic: . Degrees of freedom: . Critical value for with is 3.841. Since 24.242 > 3.841, reject . 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