# How to compare across rows/columns in a cross-tabulated table?

I am analyzing a survey which a table which crosstabulates a respondent's preferred candy bar size against how many candy bars they purchased in the last month. It looks like this:

            | Total | Zero  | One   | Two   | Three+
------------|-------|-------|-------|-------|--------
Base        | 35212 | 11494 | 19029 | 3696  | 993
------------|-------|-------|-------|-------|--------
Fun Size    | 812   | 187   | 496   | 95    | 34
------------|-------|-------|-------|-------|--------
King Size   | 1685  | 335   | 1048  | 231   | 71
------------|-------|-------|-------|-------|--------
Normal Size | 32715 | 10972 | 17485 | 3370  | 888
------------|-------|-------|-------|-------|--------


I would like to be able to answer two types of questions from this data, which basically boils down to determining significant differences across either the rows or the columns of this table.

1. Given a particular candy bar size, are those who purchase a given number of candy bars in a month more likely to prefer that size than those who purchased a different number? For example, 95% of those who purchased 0 candy bars prefer normal sized bars, where this percentage is 92% for those who purchased one. Is this a significant difference?

2. Given a particular number purchased, is there a preference for a particular candy bar size? For example, 5.5% of those who purchased one candy bar prefer a king sized bar while 2.6% prefer a fun size bar. Is this a significant difference?

• Search Chi-Square test for two-way table. – user158565 Jul 13 '19 at 3:53

(1) A chi-squared test on the counts in the 12 cells of the table tests whether the categorical variable 'BarSize' and 'NrBought' are independent. The null hypothesis is rejected with a P-value near $$0.$$ Roughly speaking, those with different preferences of bar size have different buying patterns.

b.0 = c(187, 335, 10972)
b.1 = c(496, 1084, 17485)
b.2 = c(95, 231, 3370)
b.3 = c(34, 71, 888)
DTA = cbind(b.0, b.1, b.2, b.3)
candy = chisq.test(DTA)
candy

Pearson's Chi-squared test

data:  DTA
X-squared = 191.17, df = 6, p-value < 2.2e-16

candy$obs b.0 b.1 b.2 b.3 [1,] 187 496 95 34 [2,] 335 1084 231 71 [3,] 10972 17485 3370 888 candy$exp
b.0        b.1        b.2       b.3
[1,]   264.7846   439.1960   85.14389  22.87551
[2,]   561.1999   930.8575  180.45892  48.48369
[3,] 10668.0155 17694.9465 3430.39719 921.64080

candy\$resid
b.0       b.1       b.2       b.3
[1,] -4.780214  2.710503  1.068141  2.325920
[2,] -9.548466  5.019427  3.762317  3.233698
[3,]  2.943131 -1.578280 -1.031204 -1.108117


Cells for which Pearson residuals have absolute values substantially above 2 may be of particular interest. For example, under independence, of those who prefer King size we would have expected about 561 to have bought no candy bars, but only 335 did. Also, of those who prefer King size more than expected bought one candy bar. Across the board those who prefer King size had different buying patterns than those with other preferences.

(2) If we look at everyone in the study, preferences for various sizes of bars are far from equal. The null hypothesis that each size is equally popular, is overwhelming rejected with a P-value very near to $$0.$$

chisq.test(c(812, 1685, 32715))

Chi-squared test for given probabilities

data:  c(812, 1685, 32715)
X-squared = 56271, df = 2, p-value < 2.2e-16


The strong preference for Regular size bars, obvious from the data table, also holds true for those who bought various numbers of candy bars.

chisq.test(b.0)$$p.val [1] 0 chisq.test(b.1)$$p.val
[1] 0
chisq.test(b.2)$$p.val [1] 0 chisq.test(b.3)$$p.val
[1] 1.778789e-306


Notes: (a) The test in (1) does not take into account that the categorical variable 'NrBought' is ordinal. If the P-value had been anywhere near 5% it would have been a good idea to do a test that does take this into account. But the somewhat less powerful Pearson chi-squared test is definitive for your data.

(b) This recent Q&A discusses similar issues. Other links under 'Related' in the right margin may also be relevant.

• I haven't taken the time to unpack your comment, but I wanted to say before too much time has gone by how much I appreciate your detailed writeup. This will be invaluable for me to complete the project I'm working on! – limp_chimp Jul 15 '19 at 19:07