3
$\begingroup$

If I run a chi square in R with the Yates' correction, I get slightly different results from doing it by hand. What is the exact formula R is using for the Yates' correction? I use the simple code:

chisq.test(table)

(for a 2x2 table, so df = 1 and R does Yates' correction automatically)

By hand (well, in excel), I'm subtracting 0.5 from each Observed-Expected value (which I then square and subsequently divide by the expected): = the sum of all ((O-E)-0.5)^2/E.

This is clearly not the formula R is using to get the chi square with the Yates correction, but I can't seem to find what they do use. Does anyone know?

Adding data from SO post: Some sample data (in table saved as .csv):

Pre; Post

32; 3

512; 179

With R and Yates' correction chi square = 4.4 and p=0.035

With my own formula subtracting 0.5 chi square = 5.78 and p=0.0209

$\endgroup$
  • 1
    $\begingroup$ You should be subtracting one half from the absolute difference between observed & expected values. $\endgroup$ – Scortchi - Reinstate Monica Jul 2 '15 at 14:21
  • 3
    $\begingroup$ check the documentation of 'chisq.test'. $\endgroup$ – Zhanxiong Jul 2 '15 at 14:21
  • 1
    $\begingroup$ Or simply type chisq.test to see the code. Search for YATES to see the relevant parts. $\endgroup$ – whuber Jul 2 '15 at 14:33
  • $\begingroup$ Thanks! That was exactly what I was looking for. Sorry, I'm new to R and statistics. $\endgroup$ – Marieke Jul 2 '15 at 14:39
  • $\begingroup$ This question will remain forever open if someone does not write up the solution and accept it. $\endgroup$ – Mike Wise Jul 3 '15 at 11:14
2
$\begingroup$

The problem was the absolute value, as @Scortchi noted.


Yates' correction modifies the $\chi^2$ statistic for a $2\times 2$ contingency table in an effort to correct the error made by using a (continuous) $\chi^2$ distribution to approximate the (discrete) sampling distribution of the statistic.

Recall that the $\chi^2$ statistic is based on the residuals in a contingency table: the differences between the observed counts $O$ and the expectations $E$ in each cell. (The expectations do not have to be whole numbers). In fact, only the sizes of the residuals really matter, because the residuals are always squared. Yates' correction subtracts $1/2$ from the size of each residual. Thus, the original formula

$$\chi^2 = \sum_{\text{cells}} \frac{(O_\text{cell} - E_\text{cell})^2}{E_\text{cell}}$$

becomes

$$\chi^2_\text{corrected} = \sum_{\text{cells}} \frac{(|O_\text{cell} - E_\text{cell}| - 1/2)^2}{E_\text{cell}}.$$


The R code for chisq.test appears to be a little subtler. Here is the relevant section. (It is buried within some nested conditionals which are not relevant here.)

        if (correct && nrow(x) == 2L && ncol(x) == 2L) {
            YATES <- min(0.5, abs(x - E))
            if (YATES > 0) 
              METHOD <- paste(METHOD, "with Yates' continuity correction")
        }
        else YATES <- 0
        STATISTIC <- sum((abs(x - E) - YATES)^2/E)

In this code, x stores the cell counts (thus playing the role of $O$) and E is a parallel array of expected values. The outer conditional (if) assures the correction is applied only when (a) it is requested, as indicated by the logical value of correct, and (b) these counts are for a $2\times 2$ table.

The use of min replaces $1/2$ in the correction by the smallest of the absolute residuals (should any of them be smaller than $1/2$). This assures that none of the corrected absolute residuals is made any less than zero. This little nicety is not noted in the Wikipedia article. Although not the same as Yates' original proposal, it can be construed as a variation of it in which no corrected value is ever made negative:

... group the $\chi$ distribution, taking the half units of deviation from expectation as the group boundaries ... . This is equivalent to computing the values of $\chi^2$ for deviations half a unit less than the true deviations, $8$ successes, for example, being reckoned as $7\frac{1}{2}$... . This correction may be styled the correction for continuity... .

Reference

The quotation is at p. 222 of

Yates, F (1934). "Contingency table involving small numbers and the χ2 test". Supplement to the Journal of the Royal Statistical Society 1(2): 217–235.

$\endgroup$
2
$\begingroup$

Yates' correction for continuity is:

$\chi_\text{corrected}^2 = \sum_{i=1}^{N} {(|O_i - E_i| - \frac12)^2 \over E_i}$

R's chi-square uses:

 STATISTIC <- sum((abs(x - E) - YATES)^2/E)

Where YATES might be 0 (e.g. if no correction is applied) or

            YATES <- min(0.5, abs(x - E))

which is by default used in the 2x2 case; which is to say, if $|O_i-E_i|<\frac12$ (as it will be if $O_i=E_i$), then the correction will be smaller than $\frac12$ (0 if they're equal).

This adjustment (sometimes) to the usual "$\frac12$" makes it equivalent to

$\sum_{i=1}^{N} {\max(0,|O_i - E_i| - \frac12)^2 \over E_i}$

which corresponds to the final 2x2 form given in the Wikipedia article:

$ \frac{N( \max(0, |ad - bc| - N/2) )^2}{N_S N_F N_A N_B}\,,$

which is described as better "in some cases". [Yates' actual advice in his 1934 article was more complicated than either formula.]

$\endgroup$
  • $\begingroup$ +1 Thank you for making the connection to that last form of the statistic. $\endgroup$ – whuber Jul 3 '15 at 15:05
  • $\begingroup$ @whuber I had the information sitting in an edit session a couple of hours back, but unfortunately got interrupted before I finished typing the last few words. $\endgroup$ – Glen_b -Reinstate Monica Jul 3 '15 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.