# Performing chi-squared test of significance with zero (0) observed frequency in the contingency table

I am trying to find if the flag is significantly affecting the groups distribution. I am trying to perform the chi-squared test but it is throwing a NaN value (as expected because 0 observed frequency for some groups). But then how to find if the 2 flags are really having 2 different distributions. Should I just remove the groups with 0 in either flag?

Sample of the contingency table (contingency_table_wide)

             groups
flag    0-1    1-4    5-9  10-14  15-19  20-24  25-29  30-34
0      2      1      0  28798 218272 464149 519604 412537
1      0      0      0   4552  66845 157689 147428  99612


Code

contingency_table <- xtabs(~flag+groups, data=Result_table) # xtabs is from stats package
contingency_table_wide <- as.data.frame.matrix(xtabs(Freq~flag+groups, data=contingency_table)) # for visual purposes
summary(contingency_table)


Output

Number of cases in table: 3173422
Number of factors: 2
Test for independence of all factors:
Chisq = NaN, df = 18, p-value = NA
Chi-squared approximation may be incorrect

• Having some observed 0's isn't necessarily a problem in any way at all. The problem you're having occurs when you get an entire row or column of 0's. See here or here or here – Glen_b Nov 24 '16 at 9:45
• @Glen_b I just realized that seeing one of your other answers. I used drop.unused.levels = TRUE in xtabs() and it removed the column (5-9) with all 0s. Will that give a valid chi-square statistic? Also, I am getting p-value as 0 after removal so that's no indication for something gone wrong?. – Shivendra Nov 24 '16 at 9:49
• 1. Which answer did you see? 2. There's almost no information in the three low column categories because the column totals are so small. Note that combining the column of zeros with any other column is identical to dropping it, Unless it makes no sense to do so for your problem, I'd suggest combining the three leftmost columns. 3. With such huge counts you should expect extremely tiny p-values -- even very small effects will be detectable at huge sample sizes. – Glen_b Nov 24 '16 at 9:58
• Sorry for causing confusion, I was referring to this . Thanks for explaining. If I may ask, does chi-square remain authentic here or I should do something else to see if the flag is casing a change in trend? – Shivendra Nov 24 '16 at 10:05
• I'm not sure what you're asking there. Combining adjacent columns purely on the basic of the column totals (i.e. without reference to the pattern of values) should present no problem. If you're asking about whether - even after combining - the test statistic should be approximately distributed as chi-square in spite of two low expected values under the null (one a bit less than 1), that should be okay. – Glen_b Nov 24 '16 at 10:14