# Getting NaN value for X-Squared and N/A for p-value in Chi-Square Test

I have to perform a chi-square test on this given data set:

Upon doing so with the following code:

I get the following result:

        Pearson's Chi-squared test

data:  data.table
X-squared = NaN, df = 44, p-value = NA

Warning message:
In chisq.test(data.table) : Chi-squared approximation may be incorrect


Why is the X-squared value "NaN" and the p-value "N/A"? What am I doing wrong here?

• Could you tell us what units of measurement your numbers are in?
– whuber
Mar 27, 2013 at 13:20
• Are the units of measurement even relevant? The data that I am performing the Chi-Square test upon, is composed of the monthly rainfall average(in cms) of a particular location, for 5 years:2008,2009,2010,2011,2012.
– TGK
Mar 27, 2013 at 14:14
• They are as relevant as anything else, because--among other things--the chi-squared test is applied to counts. So let's go back to the beginning, since obviously the chi-squared test is wrong here: what are you trying to learn about these data?
– whuber
Mar 27, 2013 at 14:16
• If you type ?chisq.test you will see that, if (as in your case) x is a vector then the values in it must be non negative integers Mar 27, 2013 at 18:48
• It simply makes no sense whatever to apply a chi-square test to these measurements. The technical reason for getting NAs is because for the months March, July and December the expecteds you're generating are all zero. Their contribution to chi-square is a sum of terms like $(0 - 0)^2/0$... but they're meaningless anyway, so it's actually a good thing the analysis doesn't work; better no answer than a meaningless one that appears meaningful - that's dangerous. Whoever has asked you to do such a thing apparently has no idea what they're doing. Mar 27, 2013 at 23:08

## 1 Answer

As Glen_B pointed out, your problem is that you can't have expected frequencies to be zero, because that would throw off the calculation of the chi-squared statistic (you'd have zeros in the denominator). For example, see that

x1 <- matrix(c(0, 1, 0, 1), 2, 2)
x1
[,1] [,2]
[1,]    0    0
[2,]    1    1


and

x2 <- matrix(c(0, 0, 1, 1), 2, 2)
x2
[,1] [,2]
[1,]    0    1
[2,]    0    1


will both fail to produce chi-squared tests (as they should), whereas

x3 <- matrix(c(0, 1, 0, 1), 2, 2)
x3
[,1] [,2]
[1,]    0    1
[2,]    1    0


will give you no problems.

BTW, this issue was also addressed here and here.