How to use the chi squared test (a biological investigation) Imagine four habitats - A, B, C and D - set up in a choice chamber and two species of the same family, X and Y, 20 individuals of each.
In a series of experiments, we put these species into the choice chamber - first one group of 20 (of the same species), then the other group of 20 (of the other species) in order to note down their 'choices'.
Now imagine that I have the count data of the number of individuals of each species in each quadrant of the choice chamber (mean value calculated after 22 repeats) - how do I use the chi-squared test in this scenario?
Do I use a 4x2 table with my four habitats along the top and my observed and expected values of the two different species in the table? Or do I have to separately calculate the chi squared of both species and their associated with the four habitats? Or do I do something else? It is necessary for me to do some sort of significance test. Essentially, I am interested in commenting on whether species A and/or species B have (potentially different) preferences for any of the four habitats, and if they do, then explaining that preference using biological theory from the existing literature.
For a bit more detail on how the data was collected, please see below:
I first put 20 individuals of species A into the choice chamber, all at the same time, waited 5 minutes, and then counted how many individuals were in each quadrant. I then took the species out, put them into a rest chamber and put 20 individuals of species B into the chamber and repeated the process for both species 20 times. I was not able to keep track of each individual I put in. I am therefore currently assuming that the data is totally independent each time, and that there were no herding effects, scent trails, 'memory' etc between trials. Once I had my count data (observed values), I then took a mean average for each species. These were (1.000  12.455  3.727   2.818) for species for A and (9.818 2.182   2.273   5.727) for species B. I now wish to analyse these values (or maybe I have to analyse the raw data, not just the mean?). Another thing which has just clicked - will my expected values be 5 (20/4 - assuming even distribution) or would I use the (row total*column total)/(grand total) formula?
NB. The same individuals were tested over and over again (with the rest periods in between as described above).
Please also note: A lot of these new ideas/clarification have come from a conversation in the comments down below. In that conversation there are also suggestions from other experts who have some ideas on how I might analyse the data - if you wish, please do skim over that conversation below. However, the main points (from my end, at least) have been summarised in this question itself.
 A: You can carry out Pearson's chi-squared test for homogeneity on the two-by-four contingency table of counts aggregated over individual animals & repetitions:





Habitat A
Habitat B
Habitat C
Habitat D




Species X
22
274
82
62


Species Y
216
48
50
126




But the assumption justifying the treatment of the test statistic as a variate from a chi-squared distribution (with three degrees of freedom) is that for each species you've got 440 independent observations from a categorical distribution. That means, as @Lewian & @Bernhard have pointed out ...

*

*Each animal chooses a habitat to settle in regardless of which the other 19 choose—they don't like to either on the one hand to crowd together, or on the other to spread themselves out.

*There aren't "carry-over" effects between repetitions—the animals don't follow (or avoid) scent trails from previous goes, or remember the way to a desirable or an undesirable habitat

*Given that you've repeated observations of each animal: each animal of the same species has the same propensity to choose any habitat over repetitions—it's not the case, for example, that each animal always chooses the same habitat, & you'd have effectively just 20 observations from which to conduct inference about the species. (This issue is often dealt with by using hierarchical models; but you haven't kept track of observations at the level of individual animals.)

The extent to which it's reasonable to suppose these ideal conditions are approached in this case is a matter of biology. As an implication is that the counts across habitats within each repetition are drawn from a common multinomial distribution, bar plots & tests for homogeneity by species may give some assurance that the data are not discrepant with the independence assumption.

There's a more conservative test you could perform, which you might feel appropriate given the above considerations. Calculate the chi-squared statistic as before, but treat the 44 repetitions as the experimental units. Under the null hypothesis the species labels 'X' & 'Y' are exchangeable when there are no "carry-over" effects, so generate the null distribution of the test statistic from their permutations. If you then limit statistical inference to the collection of individual animals participating in your study, inference about the species may be a purely biological argument.
