Skip to content
Advertisement

Cramer V correlation in python but instead of using frequency using weights?

So the dataset for Cramer V correlation has multiple categorical variables in columns, but there is also a column that is there telling us how often these values appear. Similar to table below:

JavaScript

I want to figure out how to calculate the Cramer V correlation between season/Age/Weather and the weight is sales? If doable how would one write something to calculate it? Or is there a different approach one can take to figure out correlation here? thanks!

Advertisement

Answer

As you probably know, Cramer’s V measures association between two nominal variables. So you can convert your current table into separate contingency tables for each pairwise combination of your variables and then compute pairwise statistics.

Code to create a table similar to yours:

JavaScript

Convert the table into a contingency table for measuring association between Season and Age and save it as 2-d array:

JavaScript

Now, you can calculate the chi-squared statistic and from that compute Cramer’s V. The formula for Cramer’s V can be found here.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement