I am trying to calculate the correlation between binary variables using Cramer's statistics:
def cramers_corrected_stat(confusion_matrix):
chi2 = ss.chi2_contingency(confusion_matrix)[0]
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)))
However I do not know how to apply the code above within my dataset:
CL UP NS P CL_S
480 1 0 1 0 1
1232 1 0 1 0 1
2308 1 1 1 0 1
1590 1 0 1 0 1
497 1 1 0 0 1
... ... ... ... ... ...
1066 1 1 1 0 1
1817 1 0 1 0 1
2411 1 1 1 0 1
2149 1 0 1 0 1
1780 1 0 1 0 1
I would appreciate your help in guiding me