# How to find the relationship between two categorical variables?

I'm building a model and trying to get the relationship between two multi-level categorical variables.

For example, I want to know the relationship between race and likelihood of graduate from collage, we have 5 races and YES or NO for graduation. How to find the which race have higher likelihood or which have lower?

Thanks

• Can we see the data please? – JahKnows May 4 '19 at 5:51

Traditional statistics like Chi-squared tests and Cramer's V can be used to determine relationship between two categorical features.

It can be a logistic regression, with Graduation as a binary dependent variable (1 for Yes and 0 for No) and Race as independent. You would encode Race as five separate dummy (1/0) variables, with each subject in your dataset having a 1 in one of those variables and 0 in the other four. Then you would run the regression with one of those five dummies omitted from the regression as the reference category. You would use the output to draw conclusions, such as odds of graduating for students of race A are X% higher than the odds for students of Race B.

• If Graduation wasn't binary, would you still use log. reg.? – jiggunjer Jun 3 '19 at 10:26
• @jiggunjer It depends. I recommend you ask a separate question, providing specifics about your data and research question. – AlexK Jun 3 '19 at 15:50

There is a Scikit-Learn module "LogisticRegression" which will easily perform this calculation (albeit its only for Python not R, which might be a problem). The advantage of Scikit is that if you wanted to perform a linear SVC ... its easy because it is just another module. Furthermore, if you wanted to assess the data within an ML framework, its an easy extension. Again, you would need to learn a bit of Python to import your data.

As bradS mentioned, you can use Cramers_V.

def cramers_v(confusion_matrix):
""" calculate Cramers V statistic for categorial-categorial association.
uses correction from Bergsma and Wicher,
Journal of the Korean Statistical Society 42 (2013): 323-328
"""
chi2 = ss.chi2_contingency(confusion_matrix)
n = confusion_matrix.sum()
phi2 = chi2 / n
r, k = confusion_matrix.shape
phi2corr = max(0, phi2 - ((k-1)*(r-1))/(n-1))
rcorr = r - ((r-1)**2)/(n-1)
kcorr = k - ((k-1)**2)/(n-1)
return np.sqrt(phi2corr / min((kcorr-1), (rcorr-1)))


One suggestion is to label encoding on these categorical variables and find the correlation. Or I believe we can use count plot for the same.

• you don't need encoding to find correlation – Blenz Jun 3 '19 at 12:03