At What Level is a $\chi^2$ test Mathematically Identical to a $z$-test of Proportions? - Cross Validated most recent 30 from stats.stackexchange.com 2019-07-18T05:09:00Z https://stats.stackexchange.com/feeds/question/173415 http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://stats.stackexchange.com/q/173415 15 At What Level is a $\chi^2$ test Mathematically Identical to a $z$-test of Proportions? Antoni Parellada https://stats.stackexchange.com/users/67822 2015-09-21T03:00:15Z 2015-09-21T21:43:17Z <p><strong>BACKGROUND:</strong> Skip safely - it's here for reference, and to legitimize the question.</p> <p>The opening of this <a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;uact=8&amp;ved=0CB4QFjAAahUKEwiy8-rihYfIAhUGGz4KHY1PALw&amp;url=http%3A%2F%2Fwww.ucl.ac.uk%2Fenglish-usage%2Fstaff%2Fsean%2Fresources%2Fz-squared.pdf&amp;usg=AFQjCNHqfu33W1utxdJyKzYV9NZKR1z8XA&amp;sig2=H5Pm5OzMTyQeeAOl3dbmPg" rel="nofollow noreferrer">paper</a> reads:</p> <p><em>"Karl Pearson’s famous chi-square contingency test is derived from another statistic, called the z statistic, based on the Normal distribution. The simplest versions of $\chi^2$ can be shown to be mathematically identical to equivalent z tests. The tests produce the same result in all circumstances. For all intents and purposes “chi-squared” could be called “z-squared”. The critical values of $\chi^2$ for one degree of freedom are the square of the corresponding critical values of z."</em></p> <p>This has been asserted multiple times in CV (<a href="https://stats.stackexchange.com/a/2443/67822">here</a>, <a href="https://stats.stackexchange.com/a/108566/67822">here</a>, <a href="https://stats.stackexchange.com/a/158480/67822">here</a> and others).</p> <p>And indeed we can <a href="https://math.stackexchange.com/q/1384338/152225">prove</a> that $\chi^2_{1\,df}$ is equivalent to $X^2$ with $X\sim N(0,1)$:</p> <p>Let's say that $X \sim N(0,1)$ and that $Y=X^2$ and find the density of $Y$ by using the $cdf$ method:</p> <p>$p(Y \leq y) = p(X^2 \leq y)= p(-\sqrt{y} \leq x \leq \sqrt{y})$. The problem is that we cannot integrate in close form the density of the normal distribution. But we can express it:</p> <p>$$F_X(y) = F_X(\sqrt{y})- F_X(-\sqrt[]{y}).$$ Taking the derivative:</p> <p>$$f_X(y)= F_X'(\sqrt{y})\,\frac{1}{2\sqrt{y}}+ F_X'(\sqrt{-y})\,\frac{1}{2\sqrt{y}}.$$</p> <p>Since the values of the normal $pdf$ are symmetrical:</p> <p>$f_X(y)= F_X'(\sqrt{y})\,\frac{1}{\sqrt{y}}$. Equating this to the $pdf$ of the normal (now the $x$ in the $pdf$ will be $\sqrt{y}$ to be plugged into the $e^{-\frac{x^2}{2}}$ part of the normal $pdf$); and remembering to in include $\frac{1}{\sqrt{y}}$ at the end:</p> <p>$$f_X(y)= F_X'(\sqrt[]{y})\,\frac{1}{\sqrt[]{y}}= \frac{1}{\sqrt{2\pi}}\,e^{-\frac{y}{2}}\, \frac{1}{\sqrt[]{y}}=\frac{1}{\sqrt{2\pi}}\,e^{-\frac{y}{2}}\, y^{\frac{1}{2}- 1}$$</p> <p>Compare to the pdf of the chi square:</p> <p>$$f_X(x)= \frac{1}{2^{\nu/2}\Gamma(\frac{\nu}{2})}e^{\frac{-x}{2}}x^{\frac{\nu}{2}-1}$$</p> <p>Since $\Gamma(1/2)=\sqrt{\pi}$, for $1$ df, we have derived exactly the $pdf$ of the chi square.</p> <p>Further, if we call the function <code>prop.test()</code> in R we are invoking the same $\chi^2$ test as if we decide upon <code>chisq.test()</code>.</p> <p><strong>THE QUESTION:</strong></p> <p>So I get all these points, yet I still don't know how they apply to the actual implementation of these two tests for two reasons:</p> <ol> <li><p><em>A z-test is not squared.</em></p></li> <li><p><em>The actual test statistics are completely different:</em></p></li> </ol> <p>The value of the <a href="https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test" rel="nofollow noreferrer">test-statistic for a $\chi^2$</a> is:</p> <p>$\chi^2 = \sum_{i=1}^{n} \frac{(O_i - E_i)^2}{E_i} = N \sum_{i=1}^n p_i \left(\frac{O_i/N - p_i}{p_i}\right)^2$ where</p> <p>$\chi^2$ = Pearson's cumulative test statistic, which asymptotically approaches a $\chi^2$ distribution. $O_i$ = the number of observations of type $i$; $N$ = total number of observations; $E_i$ = $N p_i$ = the expected (theoretical) frequency of type $i$, asserted by the null hypothesis that the fraction of type $i$ in the population is $p_i$; $n$ = the number of cells in the table.</p> <p>On the other hand, the test statistic for a $z$-test is:</p> <p>$\displaystyle Z = \frac{\frac{x_1}{n_1}-\frac{x_2}{n_2}}{\sqrt{p\,(1-p)(1/n_1+1/n_2)}}$ with $\displaystyle p = \frac{x_1\,+\,x_2}{n_1\,+\,n_2}$, where $x_1$ and $x_2$ are the number of "successes", over the number of subjects in each one of the levels of the categorical variables, i.e. $n_1$ and $n_2$.</p> <p>This formula seems to rely on the binomial distribution.</p> <p>These two tests statistics are clearly different, and <a href="https://stats.stackexchange.com/a/167988/67822">result in different results for the actual test statistics, as well as for the <em>p</em>-values</a>: <code>5.8481</code> for the $\chi^2$ and <code>2.4183</code> for the z-test, where $\small 2.4183^2=5.84817$ (thank you, @mark999). The <em>p</em>-value for the $\chi^2$ test is <code>0.01559</code>, while for the z-test is <code>0.0077</code>. The difference explained by two-tailed versus one-tailed: $\small 0.01559/2=0.007795$ (thank you @amoeba).</p> <p><strong>So at what level do we say that they are one and the same?</strong></p> https://stats.stackexchange.com/questions/173415/at-what-level-is-a-chi2-test-mathematically-identical-to-a-z-test-of-propo/173483#173483 12 Answer by ttnphns for At What Level is a $\chi^2$ test Mathematically Identical to a $z$-test of Proportions? ttnphns https://stats.stackexchange.com/users/3277 2015-09-21T13:22:01Z 2015-09-21T21:43:17Z <p>Let us have a 2x2 frequency table where columns are two groups of respondents and rows are the two responses "Yes" and "No". And we've turned the frequencies into the <strong>proportions</strong> within group, i.e. into the vertical <em>profiles</em>:</p> <pre><code> Gr1 Gr2 Total Yes p1 p2 p No q1 q2 q -------------- 100% 100% 100% n1 n2 N </code></pre> <p>The usual (not Yates corrected) $\chi^2$ of this table, after you substitute proportions instead of frequencies in its formula, looks like this:</p> <p>$$n_1[\frac{(p_1-p)^2}{p}+\frac{(q_1-q)^2}{q}]+n_2[\frac{(p_2-p)^2}{p}+\frac{(q_2-q)^2}{q}]= \frac{n_1(p_1-p)^2+n_2(p_2-p)^2}{pq}.$$</p> <p>Remember that $p= \frac{n_1p_1+n_2p_2}{n_1+n_2}$, the element of the weighted average profile of the two profiles <code>(p1,q1)</code> and <code>(p2,q2)</code>, and plug it in the formula, to obtain</p> <p>$$...= \frac{(p_1-p_2)^2(n_1^2n_2+n_1n_2^2)}{pqN^2}$$</p> <p>Divide both numerator and denominator by the $(n_1^2n_2+n_1n_2^2)$ and get $$\frac{(p_1-p_2)^2}{pq(1/n_1+1/n_2)}=Z^2,$$</p> <p>the squared z-statistic of the z-test of proportions for "Yes" response.</p> <p>Thus, the <code>2x2</code> homogeneity Chi-square statistic (and test) is equivalent to the z-test of two proportions. The so called expected frequencies computed in the chi-square test in a given column is the weighted (by the group <code>n</code>) average vertical profile (i.e. the profile of the "average group") multiplied by that group's <code>n</code>. Thus, it comes out that chi-square tests the deviation of each of the two groups profiles from this average group profile, - which is equivalent to testing the groups' profiles difference from each other, which is the z-test of proportions.</p> <p>This is one demonstration of a link between a variables association measure (chi-square) and a group difference measure (z-test statistic). Attribute associations and group differences are (often) the two facets of the same thing.</p> <hr> <p>(Showing the expansion in the first line above, By @Antoni's request):</p> <p>$n_1[\frac{(p_1-p)^2}{p}+\frac{(q_1-q)^2}{q}]+n_2[\frac{(p_2-p)^2}{p}+\frac{(q_2-q)^2}{q}] = \frac{n_1(p_1-p)^2q}{pq}+\frac{n_1(q_1-q)^2p}{pq}+\frac{n_2(p_2-p)^2q}{pq}+\frac{n_2(q_2-q)^2p}{pq} = \frac{n_1(p_1-p)^2(1-p)+n_1(1-p_1-1+p)^2p+n_2(p_2-p)^2(1-p)+n_2(1-p_2-1+p)^2p}{pq} = \frac{n_1(p_1-p)^2(1-p)+n_1(p-p_1)^2p+n_2(p_2-p)^2(1-p)+n_2(p-p_2)^2p}{pq} = \frac{[n_1(p_1-p)^2][(1-p)+p]+[n_2(p_2-p)^2][(1-p)+p]}{pq} = \frac{n_1(p_1-p)^2+n_2(p_2-p)^2}{pq}.$</p>