# Discrepancy between chi-square with Yates correction calculated by Excel and R

I am comparing observed counts with expected counts generated by assuming equal probability. My data, in R, are as follows:

All <- matrix(c(51, 51, 76, 26), nrow=2, ncol=2)

All

[,1] [,2]

[1,]   51   76

[2,]   51   26


When I run the chi-square, these are my results:

chisq.test(All)

Pearson's Chi-squared test with Yates' continuity correction

data:  All
X-squared = 12.016, df = 1, p-value = 0.0005275


This makes sense, but when I do the calculations by hand in Excel, using the formula ((|O-E|-0.5)^2)/E, I come up with a very different X2 value: 23.539.

I have triple checked the formula, and I know that my input is the same as in R (O=76, 26; E=51, 51).

What is going on? I have seen this question posed elsewhere (Exact formula Yates' correction in R), but there the discrepancy between R and Excel was solved by taking absolute value into account. I have already done that. Could the huge difference in X2 values really be the result of R using the smallest residual, instead of just 1/2 as I use in Excel?

• You seem to have a misunderstanding how the chi-square test works in R. – Glen_b Feb 26 '16 at 10:31

When you call chisq.test on a matrix, you're telling R you want to do a chi-square test of independence on a matrix of observed values.

What you appear to be trying to do is a chi-square goodness of fit test.

Yates correction is normally applied to chi-square tests of independence, rather than to goodness of fit tests (this is also the case in R).

[To perform a goodness of fit test on your data in R try prop.test(76,26+76)]

• Thanks, I definitely didn’t understand how R treats different arguments for chisq.test. I’m actually trying to conduct a χ2 test for homogeneity, which normally works in R if I use a numeric vector with the observed values. I decided to try a matrix instead because R wasn’t returning the same value that I obtained when I did the test by hand. But I clearly didn’t appreciate what the change in input would do to how R performs the test. The stat book I use applies Yates’ correction to tests for homogeneity when df=1. I’ll keep using the correction, but will calculate the χ2 value by hand – Amanda S Feb 28 '16 at 20:14
• @Amanda A chi square test for homogeneity of proportions would be equivalent to a test for independence -- you need at least a 2x2 table of observed for that. You only have two observed values. Whatever you're going to do with two proportions, it's not going to be a test for homogeneity of proportions. Can you describe your situation clearly? What are the 26 and the 76 counts of? – Glen_b Feb 28 '16 at 22:49
• I observed a group of primates sleeping under vines or no vines. I predicted they would sleep with and without vines equally frequently: 50% of the time vines, 50% of the time no vines. I have a total of 102 observations. They slept under vines for 76 of those observations, and under no vines for 26 of them. Since I predicted my data were homogenously distributed, I assumed that I would use a test for homogeneity. But I also wondered if I should be using a chi-square goodness of fit test with these data instead. My data do seem to be appropriate for a one-proportion z test. – Amanda S Mar 3 '16 at 16:45
• You'd have saved a lot of time if you'd included this description to begin with. Yes, that can be treated as either a chi-square goodness of fit or as a one-sample proportions test (not necessarily performed using the asymptotic normal approximation but with $n=102$ there's no problem if that's what you want to do; it can also be construed as a sign test). The two are equivalent if the alternative is two tailed and the presence or absence of continuity correction is the same. (I'd probably do the straight exact binomial calculation myself but it will have no impact on the conclusion.) – Glen_b Mar 3 '16 at 23:00
• Thanks so much for your help, Glen. I didn't want to bother people with the details of my data, but I see now how helpful those details are. – Amanda S Mar 6 '16 at 4:43