# How can I test if there are significant differences between four distributions?

Here is a graphical display of my data:

The x-axis is categorical from 0-5 in terms of how many of a certain damage type are present on a leaf (with pink and blue bars representing the different types). The y-axis is how many leaves (out of 100 sampled) on that tree have that type of damage. I am interested in seeing whether there is a significant difference between the distributions within each tree (e.g. Tree 1 pink vs. Tree 1 blue distribution), and whether there are differences between the trees (e.g. Tree 1 pink vs. Tree 2 pink). Is there a statistical test to do that? I was thinking chi-squared but I feel as though it is not appropriate because some frequencies are zero.

• The chi-squared test works just fine with data that have frequencies of zero. What matters is the number of cells with tiny expected frequencies. There are various ways to cope with those small expected values. Consult our threads about the chi-squared test for suggestions and examples.
– whuber
Nov 28, 2021 at 17:16

Yes, chi-square. I had a similar problem lately, and perhaps you may use my solution. I believe that for the purpose of chi-square test, prior to computing the chi-square statistics you are permitted to aggregate the categories with low expected count into one category named "other". Then you are good to go, because you will have very few cells with low frequencies, so you meet the chi2 condition of 80% high-frequency cells.

Below is my Python implementation if you are interested. Note it implements the chi-square test for independence to compare two variables (while you need the chi-square goodness of fit to compare two populations, which is slightly different) but I believe you could directly use my code with the following approach.

If you introduce a variable (column) "tree" so that each record (leaf) would have the tree number associated to it (it might be that you already have such variable) then this becomes your target (dependent) variable, against which the independent variable "damage type" can be tested.

Then the problem you described can be reduced to the chi2 test for independence between these two variables. In the result you will see if the distribution of the variable "damage type" is dependent of the tree specimen or not.

The implementation is here (look at the usage file), and here is an intro article about it.

I would also welcome critics or improvements to the approach I propose.

• There is a subtle problem with this recommendation: in this context you can't know what the expected frequencies are before collecting the data. Thus, your choice of bins depends on the data and that sometimes leads to serious failures of the test statistic to follow the chi-squared distribution. See my example at stats.stackexchange.com/a/17148/919 for an illustration. One standard cure is to use a permutation test or bootstrap. Another, where applicable, is to employ Fisher's Exact Test.
– whuber
Nov 29, 2021 at 21:15