我试图找到R函数来计算基尼系数和R中的AUC。
我找到了包ROCR
和MLmetrics
。通常你可以通过
基尼= 2 AUC -1
在下面的例子中,对于2个exaplantory变量的情况是正确的,但对于只有一个变量的情况则不然。
这是包装中的缺陷吗?
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" ) )
会话信息是:
R版本3.4.3(2017-11-30)平台:x86_64-w64-mingw32 / x64(64位) 运行于:Windows 7 x64(内部版本7601)Service Pack 1
Matrix产品:默认
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
附加基础包:[1] stats graphics grDevices utils
数据集方法基础其他附件包:[1] MLmetrics_1.1.1 ROCR_1.0-7
gplots_3.0.1通过命名空间加载(而不是附加):[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