# Chi-squared test degrees of freedom and experiment design

In the chi-squared test of independence for a $$r\times c$$ contingency table, the degrees of freedom are set as $$(r-1)(c-1)$$, which can be seen as a goodness-of-fit test with $$n - 1 - s$$ degrees of freedom where $$n = rc$$ is the number of categories and $$s = (r-1) + (c-1)$$ is due to fitting the row and column marginal probabilities to the data.

If the experiment is such that the row sums are pre-determined, I think the test becomes a test of homogeneity, which according to Wikipedia has the same degrees of freedom. However, as only the column marginal probabilities need to be fitted from the data, it seems like one should use $$s = c - 1$$. Why is this not the case? Maybe some other intuition than the analogy to the goodness-of-fit test is more adequate.

• The principles and assumptions of the chi-squared test are presented in my post at stats.stackexchange.com/a/17148/919. – whuber Mar 20 at 12:54
• @whuber Thank you for the reference, I posted a tentative answer and would be grateful if you could see if it makes sense. – udscbt Mar 21 at 10:36

As mentioned in the answer linked by whuber, this way of computing degrees of freedom for the goodness-of-fit test is but a heuristic.

Whatever experiment design, the hypothesized independence of the row and column variables enforces the following $$1 + (r-1) + (c-1)$$ constraints:

• $$\sum_{i=1}^r\sum_{j=1}^c O_{i,j} / N = 1$$,
• $$O_{i,c} / N \approx (\sum_{j=1}^c O_{i,j})(\sum_{k=1}^r O_{k,c}) / N^2$$ for $$i < r$$,
• $$O_{r,j} / N \approx (\sum_{k=1}^c O_{r,k})(\sum_{i=1}^r O_{i,j}) / N^2$$ for $$j < c$$.

with $$O_{i,j}$$ the count in cell $$(i,j)$$, $$N$$ the total number of observations and $$\approx$$ becoming an equality asymptotically.

This leaves $$(r-1)(c-1)$$ degrees of freedom for the counts in the table and the goodness-of-fit test checks if the cell probabilities match some particular values. These values may come from the experiment design or from the data itself but it does not change the degrees of freedom for the test statistic.

However, it may change the p-value. For example, say the experiment was designed with 1/2 probability for each row but the empirical probabilities are 48/100 and 52/100. If 1/2 is used for the test, the p-value would be different from that for which the empirical ones are used.