# How to Chi-square test in statistics? [duplicate]

Can we be 99% sure that there is a difference between the health habits of the group suffering from the disease and the group not suffering from the disease? The calculation formula and data are shown in the figure below.That is to say, is there any built-in function in mathematica to automatically calculate the formula k ^ 2 in the diagram?

Clear["Global*"]

data1 = {{"", "not good enough", "good", "total"}, {"case group", 40,
60, 100}, {"control group", 10, 90, 100}, {"total", 50, 150, 200}};

Grid[data1, Frame -> All, ItemSize -> 15]

• Jan 30, 2023 at 23:28
• The content of the above post is too long. Is there a new built-in function or a simpler method in the new version 13.2? Jan 31, 2023 at 0:32

You can analyze contingency tables in Mathematica by using Poisson regression:

myData={{notGood,case,40},{good,case,60},
{notGood,control,10},{good,control,90}};

myModel=GeneralizedLinearModelFit[myData,{x,y},{x,y},
NominalVariables->All,ExponentialFamily->"Poisson"];

myModel["PearsonChiSquare"]


This returns the value of the chi-square test statistic to be 24. Since 24 exceeds the tabled value of 6.635, then we reject the null hypothesis of independence at the 0.010 level, and conclude that there is a difference between the health habits of the group suffering from the disease and the group not suffering from the disease.

This analysis can be extended to contingency tables of any size.

• Clear["Global*"] data1 = {{"", "not good enough", "good", "total"}, {"case group", 40, 60, 100}, {"control group", 10, 90, 100}, {"total", 50, 150, 200}}; Grid[data1, Frame -> All, ItemSize -> 15] Jan 31, 2023 at 23:34
• How to find Chi square with the above code? Because the code above is easy to draw tables Jan 31, 2023 at 23:35