I want to measure the magnitude and direction of a relation between two nominal variables: profession and hood. I choose to use Cramer's V based on this explanation.
import pandas as pd
import numpy as np
import scipy.stats as scis
# compute cramer's stat
def cramers_stat(contingency_table):
chi2 = scis.chi2_contingency(contingency_table)
print('chi2 p-value: ', chi2[1])
if chi2[1] < 0.05:
n = contingency_table.values.sum()
return np.sqrt(chi2[0] / (n*(min(contingency_table.shape)-1)))
else:
return 'no relation'
# create df
hoods = pd.DataFrame({
'profession': np.random.choice([
'painter',
'plumber',
'statistician'
], 200, p=[.2,.4,.4])
})
# fill hood value with trends
def hood(value):
if value == 'painter':
return np.random.choice(['Uptown', 'Downtown', 'Burbs'], p=[.8,.1,.1])
elif value == 'plumber':
return np.random.choice(['Uptown', 'Downtown', 'Burbs'], p=[.3,.5,.2])
elif value == 'statistician':
return np.random.choice(['Uptown', 'Downtown', 'Burbs'], p=[.2,.7,.1])
hoods['hood'] = hoods.profession.apply(hood)
Frequencies
pd.crosstab(hoods.profession, hoods.hood)
#hood Burbs Downtown Uptown
#profession
#painter 4 4 35
#plumber 12 46 25
#statistician 3 55 16
Results
cramers_stat(pd.crosstab(hoods.hood, hoods.profession))
#chi2 p-value: 4.17294374904e-11
#0.36905673033287301
Question 1
This V statistic tells me that there is a relation between profession and hood. Without other V statistics for context, how do I tell how strong this is? What is the plain english statement I can make about the statistic magnitude and direction?
Question 2
Does this statistic allow me to make statements about the strength of individual hoods and individual professions? For example, "painters are significantly more likely to live in Uptown (Cramer's V=0.35, p=4.7e-10)"?