3.1 C
London
Friday, December 20, 2024
HomePythonDescriptive Statistics in PythonHow to Calculate Cramer’s V in Python

How to Calculate Cramer’s V in Python

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 Python.

Example 1: Cramer’s V for a 2×2 Table

The following code shows how to calculate Cramer’s V for a 2×2 table:

#load necessary packages and functions
import scipy.stats as stats
import numpy as np

#create 2x2 table
data = np.array([[7,12], [9,8]])

#Chi-squared test statistic, sample size, and minimum of rows and columns
X2 = stats.chi2_contingency(data, correction=False)[0]
n = np.sum(data)
minDim = min(data.shape)-1

#calculate Cramer's V 
V = np.sqrt((X2/n) / minDim)

#display Cramer's V
print(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.

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:

#load necessary packages and functions
import scipy.stats as stats
import numpy as np

#create 2x2 table
data = np.array([[6,9], [8, 5], [12, 9]])

#Chi-squared test statistic, sample size, and minimum of rows and columns
X2 = stats.chi2_contingency(data, correction=False)[0]
n = np.sum(data)
minDim = min(data.shape)-1

#calculate Cramer's V 
V = np.sqrt((X2/n) / minDim)

#display Cramer's V
print(V)

0.1775

Cramer’s V turns out to be 0.1775.

Note that this example used a table with 2 rows and 3 columns, but this exact same code works for a table of any dimensions.

Additional Resources

Chi-Square Test of Independence in Python
Chi-Square Goodness of Fit Test in Python
Fisher’s Exact Test in Python

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories