I tried to find R functions to calculate the Gini-coefficient and the AUC in R.
I found the packages ROCR
and MLmetrics
. Usually you can switch between AUC and Gini by
Gini = 2 AUC -1
in the following example this is true for the case of 2 exaplantory variables but not true for the case of just one variable.
Is this a flaw in the packages?
library(ROCR)
library(MLmetrics)
data(cars)
## 2 explanatory variables
logreg <- glm(vs ~ hp + wt,
family = binomial(link = "logit"), data = mtcars)
pred = prediction(logreg$fitted.values, mtcars$vs)
auc_perf = performance( pred, measure = "auc" )
## both lines give 0.8968254
2*auc_perf@y.values[[1]]-1
Gini(y_pred = logreg$fitted.values, y_true = mtcars$vs)
plot( performance( pred, "tpr", "fpr" ) )
## 1 explanatory variable
logreg <- glm(vs ~ hp ,
family = binomial(link = "logit"), data = mtcars)
pred = prediction(logreg$fitted.values, mtcars$vs)
auc_perf = performance( pred, measure = "auc" )
## different results:
2*auc_perf@y.values[[1]]-1
Gini(y_pred = logreg$fitted.values, y_true = mtcars$vs)
plot( performance( pred, "tpr", "fpr" ) )
Session info is:
R version 3.4.3 (2017-11-30) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale: [1] LC_COLLATE=German_Austria.1252 LC_CTYPE=German_Austria.1252 [3] LC_MONETARY=German_Austria.1252 LC_NUMERIC=C [5] LC_TIME=German_Austria.1252
attached base packages: [1] stats graphics grDevices utils
datasets methods baseother attached packages: [1] MLmetrics_1.1.1 ROCR_1.0-7
gplots_3.0.1loaded via a namespace (and not attached): [1] compiler_3.4.3
tools_3.4.3 KernSmooth_2.23-15 gdata_2.18.0 [5] caTools_1.17.1 bitops_1.0-6 pacman_0.4.6 gtools_3.5.0