Derivation (independence in two-way contingency tables)

Continuing from the goodness-of-fit test, we need to solve for the maximum likelihood estimator under $H_0$. Log-likelihood $$ \begin{align*} l_N = \sum_{x,y} N_{xy}\log(p_{x+} p_{+y}) = \sum_{x=1}^{r} \underbrace{\left(\sum_{y=1}^{c} N_{xy}\right)}_{N_{x+}}\log p_{x+} + \sum_{y=1}^{c} \underbrace{\left(\sum_{x=1}^{r} N_{xy}\right)}_{N_{+y}} \log p_{+y}. \end{align*} $$ To maximise this, we can maximise the two terms separately to get $$ \begin{align*} \hat p_{x+} = \frac{N_{x+}}{n}, \qquad \hat p_{+y} = \frac{N_{+y}}{n}. \end{align*} $$

So $$ \begin{align*} 2\log Λ = 2\sum_{x,y} N_{xy} \log \frac{N_{xy}/n}{(N_{x+}/n)(N_{+y}/n)} = 2\sum_{x,y} N_{xy} \log \frac{N_{xy}}{N_{x+}N_{+y}/n}. \end{align*} $$ Therefore $O_{xy} = N_{xy}$ and $E_{xy} = N_{x+}N_{+y}/n$ in Pearson's statistic.

$H_1$ has $rc-1$ degrees of freedom as before, and $H_0$ has $r+c$ variables ($p_{1+}, \ldots, p_{r+}, p_{+1}, \ldots, p_{+c}$) and $2$ constraints ($\sum_{x} p_{x+} = 1 = \sum_{y} p_{+y} = 1$), so $r+c-2$ degrees of freedom. Thus $$ \begin{align*} d = (rc-1)-(r+c-2) = (r-1)(c-1). \end{align*} $$