The chi-square test is defined as follows:

This statistic is distributed according to a chi-square distribution with degrees of freedom equal to the difference between the number of parameters under the alternative hypothesis and the number of parameters under the null hypothesis.

This test is commonly used for testing the independence between two categorical variables. In this example, the test statistic is the same, with the expected values computed as the product of the ith row and jth column totals divided by the overall total, that is, ni+n+j/n. The degrees of freedom is the product (# rows-1) (# cols -1).