Skip to main content

Section 6.8 Contribution

If we want to hone in on this more, we can take the contributions and turn them into percentages.

To find out more about the X^2 value, it is important to answer the question:.

What percentage of the X^2 value is each contribution responsible for?
# Calculate contribution to chi-square statistic
contributions <- ((chi_square_test$observed-chi_square_test$expected)^2)/chi_square_test$expected

contributions
Infection
Treatment              No       Yes
  Control       0.2051282 0.4637681
  Cranberry     1.5512821 3.5072464
  Lactobacillus 0.6282051 1.4202899

The formula for calculating the chi-square statistic is:.

\(X^2= ((observed-expected)^2)/expected\)
percent_contributions <- contributions / chi_square_test$statistic * 100

percent_contributions
Infection
Treatment              No       Yes
  Control        2.637993  5.964158
  Cranberry     19.949821 45.103943
  Lactobacillus  8.078853 18.265233
Through this, we discovered that about 65% of the X^2 value is due to the cranberry treatment.
You already know that visualizations really help paint the picture, and chi-square contributions are not exempt. For this, the pheatmap package ([D.1.18]) comes in handy with the pheatmap() function..
library(pheatmap)

# Create heatmap for percentage contributions
pheatmap(percent_contributions,
         display_numbers = TRUE,
         cluster_rows = FALSE,
         cluster_cols = FALSE,
         main = "% Contribution to Chi-Square Statistic")
A heatmap with treatments (Control, Cranberry, Lactobacillus) as rows and infection outcomes (No, Yes) as columns. Cells are shaded by their percentage contribution to the chi-square statistic, with numerical values displayed. The Cranberry/Yes cell is the darkest, indicating the largest contribution.
Figure 6.8.1. Heatmap showing the percentage contribution of each cell to the overall chi-square statistic. Darker shading indicates cells contributing more strongly to the chi-square value. The Cranberry/Yes cell contributes approximately 45% of the statistic.
That glaring, deep red box? That is the cranberry yes cell!
Tying all of this together, we have discovered:
  1. Cranberry has the most people that were not infected (least amount infected).
  2. The chi-square test shows a significant relationship between treatment and outcome.
  3. When looking at residuals, cranberry was the only treatment with less people infected than expected.
  4. When looking at chi-square contributions, cranberry had the largest contribution, with about 65% of the test statistic being accounted for by cranberry.
Now, how impactful is this overall? What is the effect of treatment overall on outcome?