Scikit-learn function roc_auc_score
can be used to get area under curve (AUC) of ROC curve. This score is generally used for numeric predictors' value in predicting outcomes.
However, this function can also be used for categorical variables also. Following is an example (in Python language) where the variable sex
is used to predict variable survived
and AUC is obtained using this function:
import seaborn, pandas, sklearn
from sklearn.metrics import roc_auc_score
tdf = seaborn.load_dataset('titanic')
print(tdf[['survived','sex']].head(10))
x = tdf['sex'].apply(lambda x: 1 if x=='female' else 0)
y = tdf['survived']
auc = roc_auc_score(y, x)
auc = round(auc, 4)
print()
print("AUC for sex to predict survived:", auc)
Output:
survived sex
0 0 male
1 1 female
2 1 female
3 1 female
4 0 male
5 0 male
6 0 male
7 0 male
8 1 female
9 1 female
AUC for sex to predict survived: 0.7669
However, is this technique statistically sound? Does the AUC obtained using this method a valid value for the relation between 2 categorical variables? Thanks for your help.
Edit: I have reversed the coding of sex to 0 and 1, so that the AUC now is 0.7669
Edit2: From very interesting answers given below, following points seem important:
AUC can be used with categorical variables also, provided it is interpreted correctly.
It needs to be emphasized that the greater the AUC is away from 0.5, the better it is, and not necessarily higher. Hence, AUC of 0.1 is more predictive, albeit in opposite direction, than AUC of 0.7
One may report "Absolute AUC" given by following simple Python code:
Abs_AUC = AUC if (AUC>0.5) else (1-AUC)
Hence, for an AUC of 0.1, the absolute AUC is 0.9; this will help in comparing AUCs of different variables without missing out ones on the other side of the diagonal in the ROC curve. Note: this is being suggested for predicted variable with only 2 categories.
x
into a number (int, float, etc)? $\endgroup$