Chi Square for Independence-Mantel–Haenszel test in R
Chi-Square for Independence, It is simple to evaluate using the chi-square test or Fisher exact test when we have two categorical variables.
Consider the case when we have three categorical variables and wish to discover how they are related.
The Cochran–Mantel–Haenszel test, which is an extension of the chi-square test of association, is useful in this scenario.
What will the Cochran–Mantel–Haenszel test reveal?
The Cochran–Mantel–Haenszel test assesses the significance of categorical variables’ associations.
We’ll use the VCD package and the mantelhean.test() function in this example.
Let’s look at the test hypotheses.
Ho:-The two inner variables do not have any relationship.
H1: There is a link between the two inner variables.
Mantel–Haenszel test in R
First, we need to install vcd package from cran library,
Let’s load the library
ID Treatment Sex Age Improved 1 57 Treated Male 27 Some 2 46 Treated Male 29 None 3 77 Treated Male 30 None 4 17 Treated Male 32 Marked 5 36 Treated Male 46 Marked 6 23 Treated Male 58 Marked
Treatment, Sex, Age, and Improved are the four factors here. The goal is to figure out how these factors are related.
Chi Square for Independence
Consider the following three variables: Treatment, Sex, and Improved.
Cochran-Mantel-Haenszel test data: Arthritis$Treatment and Arthritis$Improved and Arthritis$Sex Cochran-Mantel-Haenszel M^2 = 14.632, df = 2, p-value = 0.0006647
Because the p-value of 0.0006647 is less than 0.05, the null hypothesis is rejected and the alternate hypothesis is accepted.
This suggests that at each level of sex, the treatment received and the improvement claimed are not independent.
Similarly, we can divide the age column into two or more components and measure the relationship.