I'm working with pandas and scipy, and I would like to find a quick way to visualise unusual values in a contingency table.

Say this is my contingency table:

                  wolves   lions   tigers   chickens    total
ate_zebra         14112    13891   12978    54          41035
ignored_zebra     6403     6377    6109     2637        21526
total             20515    20268   19087    2691        62561

It looks as though chickens are (mostly) vegetarian, unlike the other animals.

If I run a chi-squared test, it tells me that the chi-squared value is significant and the expected values are:

wolves      lions       tigers      chickens    total
13456.19515 13294.18296 12519.54165 1765.080242 41035
7058.804846 6973.817043 6567.458353 925.9197583 21526
20515       20268       19087       2691        62561

But what I'd really like is some way to quickly, visually identify the most significant differences from the expected values, say something like this:

contingency table with most significant deviations from expected values highlighted

Excel would be good enough, but something Python-based would would be ideal :) Or do I need to write my own algorithm to do this?

Any suggestions much appreciated.

  • 3
    $\begingroup$ I may hint towards computing and presenting standardized or adjusted Pearson residuals. $\endgroup$ – ttnphns Sep 10 '14 at 7:43
  • $\begingroup$ @ttnphns Thanks! That does look like the right approach. $\endgroup$ – Richard Sep 10 '14 at 16:07
  • $\begingroup$ Another (somewhat related) possibility is contribution to chi-square, but I generally prefer standardized Pearson residuals. $\endgroup$ – Glen_b Sep 12 '14 at 1:48
  • $\begingroup$ heatmaps will be good. $\endgroup$ – kjetil b halvorsen Dec 7 '15 at 16:00

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.