Skip to main content

Section 6.7 Cross Tables

In the gmodels package ([D.1.10]) we are able to utilize the CrossTable() command. Beware, this can at first be overwhelming, as a lot of information is thrown at you.
library(gmodels)

# This allows us to see the contributions each category has on chi-square.
# Note, you (infection$Treatment, infection$Infection) if you want to stick to defaults
CrossTable(infection$Treatment, infection$Infection,
           prop.chisq = TRUE,    # Shows the chi-square contribution
           chisq = TRUE,         # shows chi-square test
           expected = TRUE,      # shows expected counts
           prop.r = TRUE,        # shows row proportions
           prop.c = TRUE)        # shows column proportions
Cell Contents
|-------------------------|
|                       N |
|              Expected N |
| Chi-square contribution |
|           N / Row Total |
|           N / Col Total |
|         N / Table Total |
|-------------------------|

 
Total Observations in Table:  150 

 
                    | infection$Infection 
infection$Treatment |        No |       Yes | Row Total | 
--------------------|-----------|-----------|-----------|
            Control |        32 |        18 |        50 | 
                    |    34.667 |    15.333 |           | 
                    |     0.205 |     0.464 |           | 
                    |     0.640 |     0.360 |     0.333 | 
                    |     0.308 |     0.391 |           | 
                    |     0.213 |     0.120 |           | 
--------------------|-----------|-----------|-----------|
          Cranberry |        42 |         8 |        50 | 
                    |    34.667 |    15.333 |           | 
                    |     1.551 |     3.507 |           | 
                    |     0.840 |     0.160 |     0.333 | 
                    |     0.404 |     0.174 |           | 
                    |     0.280 |     0.053 |           | 
--------------------|-----------|-----------|-----------|
      Lactobacillus |        30 |        20 |        50 | 
                    |    34.667 |    15.333 |           | 
                    |     0.628 |     1.420 |           | 
                    |     0.600 |     0.400 |     0.333 | 
                    |     0.288 |     0.435 |           | 
                    |     0.200 |     0.133 |           | 
--------------------|-----------|-----------|-----------|
       Column Total |       104 |        46 |       150 | 
                    |     0.693 |     0.307 |           | 
--------------------|-----------|-----------|-----------|

 
Statistics for All Table Factors


Pearson's Chi-squared test 
------------------------------------------------------------
Chi^2 =  7.77592     d.f. =  2     p =  0.0204871
CrossTable gave us a lot of information (nothing we can not handle). Thankfully, there is an legend in the beginning of the results that identifies what each number is. Some of these we already discovered, such as expected values and the chi-square X^2 value, but also some new information. Specifically, we are able to see the chi-square contribution. To break this down:
  • When we run the chi-square test, we get the X^2 value. Importantly, each combination of the variables contributes individually to this number. We are looking for the biggest contributors to not only understand the chi-square value better, but to also understand what is the most impactful.
  • For this result, our third number’s in each cell are the chi-square contribution. It is evident that cranberry has the highest contribution to the chi-square test statistic.