Juno Keynes
Juno Keynes

Reputation: 21

Association matrix in r

The way corrplot allows you to plot a correlation matrix in R

Any idea how i can plot a association matrix in R where the method of association is using any user specified method like Cramer's V

Upvotes: 2

Views: 6798

Answers (3)

Psyndrom Ventura
Psyndrom Ventura

Reputation: 139

library(vcd)
library(corrplot)

I would suggest corrplot(PairApply(df, cramerV),diag = F,is.corr = F) to change color scale from -1,1 (is.corr = T) to 0,1 (is.corr = F).

Upvotes: 1

graggsd
graggsd

Reputation: 306

Building upon the example by Alexey Knorre:

library(DescTools)
library(corrplot)

# Simulate data
df <- data.frame(x1 = sample(letters[1:5], 20, replace = TRUE), 
                 x2 = sample(letters[1:5], 20, replace = TRUE), 
                 x3 = sample(letters[1:5], 20, replace = TRUE))

# Use CramerV as input for corrplot
corrplot::corrplot(DescTools::PairApply(df, DescTools::CramerV))

Upvotes: 4

Alex Knorre
Alex Knorre

Reputation: 630

The answer to your question strongly depends on the data you've got and specific correlation method. I assume you have a bunch of nominal variables and want to see whether they are correlated using Cramer's V on the correlation plot. In this case, a way to do this is following:

  1. Calculate Cramer's V correlation coefficient for every pair of variables.I used vcd library, as it has method to calculate Cramer's V.
  2. Put these coefficients together and basically get correlation matrix
  3. Visualize the matrix

Ugly but working code to do this is listed below. I played around outer - the clearest and most precise way to work with row and column indexes, but encountered problems with indexing columns in df using row and column index from m: for some reason it just didn't want to get variable from df.

install.packages("vcd")
library(vcd)

# Simulate some data or paste your own
df <- data.frame(x1 = sample(letters[1:5], 20, replace = TRUE), 
                 x2 = sample(letters[1:5], 20, replace = TRUE), 
                 x3 = sample(letters[1:5], 20, replace = TRUE))

# Initialize empty matrix to store coefficients
empty_m <- matrix(ncol = length(df),
            nrow = length(df),
            dimnames = list(names(df), 
                            names(df)))
# Function that accepts matrix for coefficients and data and returns a correlation matrix
calculate_cramer <- function(m, df) {
 for (r in seq(nrow(m))){
   for (c in seq(ncol(m))){
     m[[r, c]] <- assocstats(table(df[[r]], df[[c]]))$cramer
   }
 }
    return(m)
}

cor_matrix <- calculate_cramer(empty_m ,data)

corrplot(cor_matrix)

corrplot for cramer's V

Upvotes: 3

Related Questions