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)

  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


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


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
  • 2
    $\begingroup$ 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 $\endgroup$ – Glen_b Nov 24 '16 at 9:45
  • $\begingroup$ @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?. $\endgroup$ – Shivendra Nov 24 '16 at 9:49
  • $\begingroup$ 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. $\endgroup$ – Glen_b Nov 24 '16 at 9:58
  • $\begingroup$ 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? $\endgroup$ – Shivendra Nov 24 '16 at 10:05
  • 1
    $\begingroup$ 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. $\endgroup$ – Glen_b Nov 24 '16 at 10:14

Your Answer

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

Browse other questions tagged or ask your own question.