Goodness-of-fit vs. independence tests
A goodness-of-fit test compares observed frequencies to expected frequencies for a single categorical variable. It answers whether the observed distribution matches a theoretical one. Degrees of freedom equal the number of categories minus one.
An independence test determines whether two categorical variables are associated. It uses a contingency table of observed counts and computes expected counts from row and column totals. Degrees of freedom equal (rows - 1) times (columns - 1).
χ² = Σ (Oᵢ − Eᵢ)² / Eᵢ
Chi-square statistic: sum of squared differences between observed and expected, divided by expected.