3

I have the following set of observed versus expected values:

> chisq.test(matrix(c(35,11,44209,44233), nrow=2))
     [,1]  [,2]
[1,]   35 44209
[2,]   11 44233

Where [1,1] = expected yes, [1,2] = expected no, [2,1] = observed yes, [2,2] observed no.

When I run this, I get...

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

data:  matrix(c(35, 11, 44209, 44233), nrow = 2)
X-squared = 11.506, df = 1, p-value = 0.0006937

So, the $\chi^2 =11.506$.

However, when I hash it out by hand, I get

> (11-35)^2/35 + (44233-44209)^2/(44209)
[1] 16.47017

...which is considerably larger. What is the cause of this difference? Is this the Yates continuity correction in action? Am I entering the data incorrectly somehow? I see the function definition is overloaded in that it does both contingency table tests /and/ goodness-of-fit tests, but this looked like the right syntax for contingency function?

Sycorax
  • 90,934
  • 3
    Yes, it is because of the continuity correction, do chisq.test(matrix(c(35,11,44209,44233), nrow=2),correct=F) to do the test without it. Also if I remember correctly the matrix upper row should contain observed, and the lower should contain expected. – ltronneberg May 07 '14 at 21:59
  • I see at least two differences (it's doing a 2x2 independence test with continuity correction, and you're not doing either of those things), but I'm not 100% sure what you're calculating. How do your expected values arise? – Glen_b May 07 '14 at 22:52

1 Answers1

6

If you give chisq.test a matrix of counts, it's going to assume that it's a matrix of observed counts and will perform a chi-square test of independence. If you want to perform a chi-square goodness of fit test, which seems to be what you want to do, then you would need to use the command

chisq.test(x,p)

where x is the vector of observed counts c(11, 44233), and p is the vector of probabilities from the null hypothesis that were used to calculated the expected counts.

jsk
  • 3,112
  • 1
    Yeah, yikes. I wish the two tests had different function calls---I would definitely have preferred an error to be thrown for what I was trying to do given the way I entered it. – Mittenchops May 08 '14 at 16:14
  • Also, just to confirm, this is different than the fisher.test() syntax, right? Which does actually take the form of... fisher.test(matrix(c(35,11,44209,44233), nrow=2)). – Mittenchops May 08 '14 at 21:38
  • fisher.test will perform a test analogous to the chi-square test of independence but not the chi-square goodness of fit test. – jsk May 08 '14 at 22:10