# I don't understand Chi squared

I think of the contingency table test. In all textbooks I've seen, the test statistic is calculated as the sum of $(O-E)^2/E$ over all cells. But the degree of freedom is not the number of all cells. E.g. in an $n \times m$ table, it's $(n-1)(m-1)$.

So far so good. But what's the rationale behind this approach? I think that the definition of the $\chi^2$ distribution is the sum of $M$ variables, each a square of a standard normal variable. I think that each $(O-E)^2/E$ is approximately standard normal, and that's why we use $\chi^2$ distribution. But the degree of freedom is not the number of summands!

• There are some linear restrictions between the summNDS ... – kjetil b halvorsen Apr 13 '15 at 10:22

The degrees of freedom is not the number of cells because there are relationships among the cells.

I think that each $(O−E)^2/E$ is approximately standard normal,

No, for two reasons. First, it's the square of something that's normal, and second, the thing it's the square of is not actually standard normal*. The easiest case to consider is the multinomial (chi-square goodness of fit). To work out the expectations, we condition on the total observed ($\sum_i O_i=\sum_i E_i$). Given the total number of observations the variance of $(O_i-E_i)$ is not $\sqrt{E_i}$ but $\sqrt{E_i(1-p_i)}$.

* whether we're dealing with a multinomial goodness of fit or a test of independence in a contingency table.

However, the counts are negatively dependent in such a way that $\sum_{i=1}^k (O_i−E_i)^2/E_i$ is equivalent to a sum of $k-1$ independent standard normals.

In the $n\times m$ table, the relationships involve a linear restriction on every row and column, but one of those restrictions is implied by the others, so it's $nm-n-m+1$ df.

• Meant to say square of something that is normal. But the point about the variance is very good. In fact the variance of a binomial trial is np(1-p), which is in this case E - pE; so not E as I erratically assumed. It seems that the derivation of the chi square test (for contingency tables) is a bit more involved, than it seems on the surface. Given the definition of the chi squared distribution, it's easy to assume that the terms are standard normal, but in this case it does not seem to be true. So the terms are something else, but their sum follows a chi squared distribution, coincidentally. – user40541 Apr 14 '15 at 11:39