# 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.

Repeated Measures of ANOVA in R Complete Tutorial »

### Hypothesis

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,

#install.packages("vcd")

Let’s load the library

library(vcd) head(Arthritis)

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.

Deep Neural Network in R » Keras & Tensor Flow

### Chi Square for Independence

Consider the following three variables: Treatment, Sex, and Improved.

mantelhaen.test(Arthritis$Treatment,Arthritis$Improved,Arthritis$Sex)

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

## Conclusion

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.