Correlation matrix for categorical variables

In R, you can easily create a correlation matrix of continuous variables using the base function ‘cor’. But there’s no comparable way to create a correlation matrix of categorical variables. The function below provides a matrix of Cramer’s V (requiring the ‘vcd’ package), where:

‘vars’ is a string vector of categorical variables that you want to correlate
‘dat’ is a data.frame containing the variables

catcorrm <- function(vars, dat) sapply(vars, function(y) sapply(vars, function(x) assocstats(table(dat[,x], dat[,y]))$cramer))
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s