Cramer’s V is a measure of the strength of association between two nominal variables.
It ranges from 0 to 1 where:
- 0 indicates no association between the two variables.
- 1 indicates a strong association between the two variables.
It is calculated as:
Cramer’s V = √(X2/n) / min(c-1, r-1)
where:
- X2: The Chi-square statistic
- n: Total sample size
- r: Number of rows
- c: Number of columns
This tutorial provides a couple examples of how to calculate Cramer’s V for a contingency table in R.
Example 1: Cramer’s V for a 2×2 Table
The following code shows how to use the CramerV function from the rcompanion package to calculate Cramer’s V for a 2×2 table:
#create 2x2 table data = matrix(c(7,9,12,8), nrow = 2) #view dataset data [,1] [,2] [1,] 7 12 [2,] 9 8 #load rcompanion library library(rcompanion) #calculate Cramer's V cramerV(data) Cramer V 0.1617
Cramer’s V turns out to be 0.1617, which indicates a fairly weak association between the two variables in the table.
Note that we can also produce a confidence interval for Cramer’s V by indicating ci = TRUE:
cramerV(data, ci = TRUE)
Cramer.V lower.ci upper.ci
1 0.1617 0.003487 0.4914
We can see that Cramer’s V remains unchanged at 0.1617, but we now have a 95% confidence interval that contains a range of values that is likely to contain the true value of Cramer’s V.
This interval turns out to be: [.003487, .4914].
Example 2: Cramer’s V for Larger Tables
Note that we can use the CramerV function to calculate Cramer’s V for a table of any size.
The following code shows how to calculate Cramer’s V for a table with 2 rows and 3 columns:
#create 2x3 table data = matrix(c(6, 9, 8, 5, 12, 9), nrow = 2) #view dataset data [,1] [,2] [,3] [1,] 6 8 12 [2,] 9 5 9 #load rcompanion library library(rcompanion) #calculate Cramer's V cramerV(data) Cramer V 0.1775
Cramer’s V turns out to be 0.1775.
You can find the complete documentation for the CramerV function here.
Additional Resources
Chi-Square Test of Independence in R
Chi-Square Goodness of Fit Test in R
Fisher’s Exact Test in R