Chi-Square Test of Independence in R

The chi-square test of independence is used to analyze the frequency table (i.e. contengency table) formed by two categorical variables. The chi-square test evaluates whether there is a significant association between the categories of the two variables.

Data format Contingency Tables

Example 6.13.3. Analytics Book Page 172

# Import the data
customer <- read.csv("Customer_Churn.csv")
samp2 <- customer[,-1]
rownames(samp2) <- customer[,1]
customer <- samp2
head(customer)
##          Churned Retained
## Segment1      25      250
## Segment2      41      484
## Segment3      28      172

The data is a contingency table containing 3 segments and their distribution in the customer churn:

Graphical Display of contingency Tables

Contingency table can be visualized using the function balloonplot() [in gplots package]. This function draws a graphical matrix where each cell contains a dot whose size reflects the relative magnitude of the corresponding component.

install gplots

library(gplots)
## Warning: package 'gplots' was built under R version 3.6.1
## 
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
## 
##     lowess
# 1. convert the data as a table
dt <- as.table(as.matrix(customer))
# 2. Graph
balloonplot(t(dt), main ="churn", xlab ="", ylab="",
            label = FALSE, show.margins = FALSE)

It’s also possible to visualize a contingency table as a mosaic plot. This is done using the function mosaicplot() from the built-in R package garphics:

library(graphics)
mosaicplot(dt, shade = TRUE, las=2,
           main = "churn")

The argument shade is used to color the graph
The argument las = 2 produces vertical labels

Blue color indicates that the observed value is higher than the expected value if the data were random
Red color specifies that the observed value is lower than the expected value if the data were random

From this mosaic plot, it can be seen that there is some relation
## Compute Chi-square test in R

chisq <- chisq.test(customer)
chisq
## 
##  Pearson's Chi-squared test
## 
## data:  customer
## X-squared = 6.5595, df = 2, p-value = 0.03764

In our example, the row and the column variables are statistically significantly associated (p-value = 0).

Thus we reject the null hypothesis that the customer segment and customer churn are not independent

# Observed counts
chisq$observed
##          Churned Retained
## Segment1      25      250
## Segment2      41      484
## Segment3      28      172
# Expected counts
round(chisq$expected,2)
##          Churned Retained
## Segment1   25.85   249.15
## Segment2   49.35   475.65
## Segment3   18.80   181.20

Nature of dependence between row and column variables

As mentioned above the total Chi-square statistic is 1944.456196.

If you want to know the most contributing cells to the total Chi-square score, you just have to calculate the Chi-square statistic for each cell:

Cells with the highest absolute standardized residuals contribute the most to the total Chi-square score.

round(chisq$residuals, 3)
##          Churned Retained
## Segment1  -0.167    0.054
## Segment2  -1.189    0.383
## Segment3   2.122   -0.683
library(corrplot)
## Warning: package 'corrplot' was built under R version 3.6.2
## corrplot 0.84 loaded
corrplot(chisq$residuals, is.cor = FALSE)

For a given cell, the size of the circle is proportional to the amount of the cell contribution.

Positive residuals are in blue. Positive values in cells specify an attraction (positive association) between the corresponding row and column variables.
In the image above, it’s evident that there are an association between the column Wife and the rows Laundry, Main_meal.
There is a strong positive association between the column Husband and the row Repair
Negative residuals are in red. This implies a repulsion (negative association) between the corresponding row and column variables. For example the column Wife are negatively associated (~ “not associated”) with the row Repairs. There is a repulsion between the column Husband and, the rows Laundry and Main_meal

# Contibution in percentage (%)
contrib <- 100*chisq$residuals^2/chisq$statistic
round(contrib, 3)
##          Churned Retained
## Segment1   0.426    0.044
## Segment2  21.539    2.235
## Segment3  68.635    7.121
# Visualize the contribution
corrplot(contrib, is.cor = FALSE)

It can be seen that:

The column “Segment3” is strongly associated with churn
The column “Segment2” is strongly associated with churn

there is no such relation in terms of retention

From the image above, it can be seen that the most contributing cells to the Chi-square are Segment3/churn (68.63%), Segment2/Churn (21.54%%)

These cells contribute about 89% to the total Chi-square score and thus account for most of the difference between expected and observed values.

This confirms the earlier visual interpretation of the data. As stated earlier, visual interpretation may be complex when the contingency table is very large. In this case, the contribution of one cell to the total Chi-square score becomes a useful way of establishing the nature of dependency.

Access to values returned by chisq.test()function

The result of chisq.test() function is a list containing the following components:

statistic: the value the chi-squared test statistic.
parameter: the degrees of freedom
p.value: the p-value of the test
observed: the observed count
expected: the expected count

# printing the p-value
chisq$p.value
## [1] 0.03763794
# printing the mean
chisq$estimate
## NULL