Example 8.39: calculating Cramer’s V

June 3, 2011

(This article was first published on SAS and R, and kindly contributed to R-bloggers)

Cramer’s V is a measure of association for nominal variables. Effectively it is the Pearson chi-square statistic rescaled to have values between 0 and 1, as follows:

V = sqrt(X^2 / [nobs * (min(ncols, nrows) – 1)])

where X^2 is the Pearson chi-square, nobs represents the number of observations included in the table, and where ncols and nrows are the number of rows and columns in the table, respectively. For a 2 by 2 table, of course, this is just the square root of chi-square divided by the number of observations, which is also known as the phi coefficient.

As an example, we’ll revisit the table of homelessness vs. gender we present in Section 2.6.3.

In SAS, Cramer’s V is provided when the chisq option to the tables statement is used, in proc freq.

proc freq data = "c:\book\help.sas7bdat";
tables female*homeless / chisq;

resulting in

Statistics for Table of FEMALE by HOMELESS
Statistic DF Value Prob
Chi-Square 1 4.3196 0.0377
Likelihood Ratio Chi-Square 1 4.3654 0.0367
Continuity Adj. Chi-Square 1 3.8708 0.0491
Mantel-Haenszel Chi-Square 1 4.3101 0.0379
Phi Coefficient -0.0977
Contingency Coefficient 0.0972
Cramer's V -0.0977

where (as usual) several additional values are also included. The negative value shown for Cramer’s V is odd– it’s unclear what rationale should be used for using the negative root. According to the documentation, this is only a possibility for 2 by 2 tables.

As far as we know, Cramer’s V is not included in base R. Of course, it is easy to assemble directly. We found one version on line. However, this requires a table as input, so we’ve rewritten it here to accept vector input instead.

Here’s the function, which uses unique() (section 1.4.16) to extract the values of the rows and columns and length() (Section 1.4.15) to find their number and the number of observations. A more bullet-proof version of the function would check to ensure the two vectors are of equal length (or allow the input in a variety of formats).

cv.test = function(x,y) {
CV = sqrt(chisq.test(x, y, correct=FALSE)$statistic /
(length(x) * (min(length(unique(x)),length(unique(y))) - 1)))
print.noquote("Cramér V / Phi:")

So we can get Cramer’s V as

helpdata = read.csv("http://www.math.smith.edu/r/data/help.csv")
with(helpdata, cv.test(female, homeless)
[1] Cramér V / Phi:
[1] 0.09765063

To leave a comment for the author, please follow the link and comment on their blog: SAS and R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , ,

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)