In previous articles, we introduced methods such as t test and analysis of variation (ANOVA) for comparing means between groups. In today's article, this article mainly introduces what the chi-square test is and how the chi-square test is used for comparison between groups.
What is the chi-square test?
According to Wikipedia, the Chi-Squared Test is often used to compare rates or proportions between groups. It is a statistical quantity whose distribution approximately obeys the Chi-square distribution when the null hypothesis is established. of hypothesis test . In the absence of other qualifications or explanations, the chi-square test generally refers to Pearson chi-square test .
Before using the chi-square test, there are two points to note:
1. The chi-square test is only applicable to variables with binary or disordered multi-category outcomes; if the outcome is hierarchical data, the chi-square test is usually not used, but the rank sum test. We will use the detailed usage method in the following series. The article will be introduced again.
2. In addition to comparing rates or proportions, the chi-square test is often used to test the goodness of fit to compare the difference between actual values and theoretical values (or predicted values). Therefore, it is often used to test whether a certain sample satisfies a specific distribution or a certain distribution. Whether the prediction effect of a model is good or not waits.
Five scenarios commonly used in chi-square tests
When do you need to use the chi-square test? Here are six common situations:
- Comparison of rates between two or more groups , that is, the outcome is a binary variable (such as death and survival, negative and positive, etc.). In this case, the chi-square test can be used directly.
- Comparison of proportions of two or more groups , Usually the outcome is an unordered multi-categorical variable (Such as disease subtypes, etc.) In this case, the chi-square test can also be used directly. However, if The number of samples is too small (generally considered to be too small if less than 40) , usually the chi-square test is not used directly to compare rates or proportions between groups, but It is recommended to use Fisher's exact test .
- Comparison of rates or proportions among multiple groups . If there is a statistical difference and you want to make further pairwise comparisons, the Bonferroni method is often used to correct the test level. For example, if three groups A, B, and C are compared in pairs and a total of three comparisons are required, the chi-square test can be performed on A and B, A and C, and B and C respectively, but the test level is not 0.05, but 0.05/3. =0.0167, that is, when comparing three times, P<0.0167 is considered to be statistically different.
- Comparison of rates among multiple groups , if the grouping variables have a hierarchical order, you can also consider Chi-square test for trend , whether the observation rate increases as the grouping level increases.
- in the case of Paired design , need to consider Paired chi-square test , the result conclusion at this time is different from the ordinary chi-square test.
Chi-square test: Comparison of rates between the two groups
We will use the OSAS data shown in Figure 1 to explain. In the data in Figure 1, we intend to compare whether there is a difference in the incidence of OSAS between obese people and normal people. Both grouping indicators and outcome indicators are categorical variables, so the chi-square test was used.

Figure 1 OSAS data
Select the JMP menu "Analyze → Fit Y with X" (Figure 2) to enter the inter-group comparison interface. In this example, obesity is the group and OSAS is the result, so put "obesity" into "X, factor" and "OSAS" into "Y, response" in the dialog box (Figure 3).

Figure 2 Chi-square test operation—menu selection

Figure 3 Chi-square test operation—variable selection
The results automatically output by JMP software include three parts of results: mosaic plot, contingency table and chi-square test results.
Let’s interpret these results one by one.
1. mosaic diagram
The first part is a mosaic chart (Figure 4), which visually presents the proportion of each group of people through graphics. The total length of the horizontal and vertical coordinates represents 100%. In Figure 4, the horizontal axis represents obesity, and the vertical axis represents whether OSAS occurs. The legend on the right shows that red represents OSAS, and blue represents no OSAS (normal).
It can be seen from the abscissa of the figure that there are more people in the normal weight group, about twice as many as the obese group.
It can be seen from the ordinate that obese people have a higher proportion of OSAS (the obese group has a higher red area than the normal group), but whether this difference is statistically significant needs to be judged by referring to the results of the chi-square test.

Figure 4 Chi-square test results—mosaic plot
2. contingency table
The second part is the contingency table. Each grid in the contingency table has 4 rows of data. The header on the upper left side of the table gives the meaning of these 4 rows of data. They are count, total percentage, column percentage and row percentage.

Figure 5 Chi-square test results—contingency table
3. Chi-square test statistical analysis results
The third part is the statistical analysis results of the chi-square test. The results include three parts: likelihood ratio chi-square, Pearson chi-square and Fisher's exact test (Figure 6). In practice, the Pearson chi-square test result is usually used, but if the number of cases is less than 40 or the theoretical frequency is less than 5 or even less than 1, it is generally recommended to choose the Fisher exact test result.

Figure 6 Chi-square test results - Chi-square test statistical analysis results
This example has a large number of samples, so Pearson chi-square can generally be selected. Of course, for the sake of rigor, we still look at the theoretical frequency situation. Click the red triangle button next to the contingency table and select the expected value in the drop-down menu (Figure 7). You can get the theoretical frequency of each grid, which is the position indicated by the arrow in Figure 8.

Figure 7 Chi-square test operation—outputting theoretical frequencies

Figure 8 Chi-square test results—theoretical frequency results
It can be seen that the theoretical frequencies of all grids are greater than 5, so the Pearson chi-square result can be selected. The results show that there is a statistical difference in the incidence of OSAS between obese and normal people (Pearson=5.970, P=0.0146) (Figure 6).

Figure 9 Chi-square test results - Chi-square test statistical analysis results
Chi-square test: Comparison of rates among multiple groups
We will continue to explain based on the OSAS data shown in Figure 1. In the data in Figure 1, we intend to compare whether there is a difference in the incidence of OSAS in patients with different tonsil sizes.
In this example, the outcome indicator is OSAS, which is a two-category variable; the grouping indicator is tonsil size, which is divided into three categories: <2, =2 and >2, which is a multi-category variable. First, perform the chi-square test. The operation is the same as above. The results are shown in Figure 10 below.

Figure 10 Chi-square test results
The contingency table in Figure 10 shows that the number of study cases is >40 and the theoretical frequencies (expected values) of all grids are greater than 5. Therefore, the Pearson result is selected. The difference in the incidence of OSAS in patients with different tonsil sizes is statistically significant (Pearson=6.305 , P=0.0428).
The above results only show that there is a statistical difference in the incidence of OSAS between the three groups, but the specific differences between the two groups require further pairwise comparisons between the groups.
For classified data, the JMP program cannot directly give pairwise comparison results, and the Bonferroni method needs to be used to correct the calibration level. Then perform a chi-square test on any two groups and compare it with the corrected test level. In this example, a total of three pairwise comparisons were performed, and the corrected calibration level is:

In other words, when comparing pairs, the difference is considered statistically significant if the P value is less than 0.0167. The results of the pairwise comparison are shown in Figure 11. The results show that the P value of less than 2 vs greater than 2 is less than 0.0167, which is statistically significant, and the other groups have no statistical significance.

Figure 11 Pairwise comparison results
In summary, statistical analysis shows that there is a statistical difference in the incidence of OSAS between obese people and normal people. There is a statistical difference in the incidence of OSAS between people with tonsil sizes less than 2 and greater than 2. There is no statistically significant difference between the remaining groups.
Chi-square test: Comparison of proportions of two or more groups
The comparison of the proportions of two groups/multiple groups, the idea and the implementation process of JMP software are completely consistent with the comparison of the rates of two groups/multiple groups. The explanations are just different. If there is a statistical difference in the proportions between groups, it means that the distributions between the groups are different. Similarly, if there is a statistical difference in the comparison of proportions between multiple groups, pairwise comparisons can also be performed. The idea is the same as the pairwise comparison of rates among multiple groups.
That’s it for today’s sharing. In the next article in this series, we will introduce extensions of the chi-square test—the rank sum test for rank data, the trend chi-square, and the paired chi-square test. If you want to know more in-depth applications of JMP, welcome Join JMP LINE official account , regularly obtain practical JMP operation skills and online course information!

original: Dry information | Chi-square test - an artifact for comparison between categorical data groups
Recommended articles on statistical methods analysis:
Thinking context and analysis methods of rank sum test and pairwise comparison
Yiwen learns three commonly used t-tests - independent sample t-test, single-sample t-test and paired t-test
Perform normality test and variation number homogeneity test in JMP
Analysis of variation (ANOVA) and pairwise comparison thinking and analysis methods