-2

我想知道如何在 MLR 中绘制提升曲线,特别是对于具有多种算法和任务的基准实验。帮助绘制 ROC 曲线也将不胜感激。谢谢。

4

1 回答 1

3

我不是mlr用户,但这是一种通用方法。首先是一些数据:

二级问题

iris2 = iris[iris$Species!="setosa",]
iris2$Species = factor(iris2$Species)

第一个模型:

log_model = glm(Species~., data = iris2, family = "binomial")
prob = predict(log_model, iris2,  type = "response") #get the logistic regression prob

第二个模型:

library(e1071)
svm_model = svm(Species~., data = iris2, probability = TRUE)
prob_svm = predict(svm_model, iris2, probability = TRUE)
prob_svm  = attr(prob_svm , "probabilities")[,2] #get the probability for svm model

从类(1/0 编码)和附加列中生成每个模型的预测概率的数据框

for_lift = data.frame(Class = as.factor(ifelse(iris2$Species == "versicolor", 1, 0)),  glm = prob, svm = prob_svm)

做一个电梯对象

library(caret)
lift_obj = lift(Class ~ glm+svm, data = for_lift)

xyplot(lift_obj, auto.key = list(columns = 2,
                                            lines = TRUE,
                                            points = FALSE))

在此处输入图像描述

您可以使用相同的数据框来绘制 ROC 曲线

library(pROC)

plot(pROC::roc(response = for_lift$Class,
               predictor = for_lift$glm,
               levels=c(0, 1)),
     lwd=1.5) 

plot(
  pROC::roc(response = for_lift$Class,
            predictor = for_lift$svm ,
            levels=c(0, 1)), 
  add=T, lty=2, lwd=1.5)
legend(0.9, 0.9, c("logistic", "svm"), lty = c(1,2))

在此处输入图像描述

您还可以查看 ROCR 包:https ://cran.r-project.org/web/packages/ROCR/ROCR.pdf它具有绘制两种类型图的方法

此外,如果您是 ggplot2 用户,您也可以使用它lift_obj来绘制提升和 ROC 曲线。

library(ggplot2)


 p1 = ggplot(lift_obj$data)+
  geom_line(aes(CumTestedPct, CumEventPct, color = liftModelVar))+
  xlab("% Samples tested")+
  ylab("% Samples found")+
  scale_color_discrete(guide = guide_legend(title = "method"))+
  geom_polygon(data = data.frame(x = c(0, lift_obj$pct, 100, 0),
                              y = c(0, 100, 100, 0)),
                              aes(x = x, y = y), alpha = 0.1)

p2 = ggplot(lift_obj$data)+
  geom_line(aes(1-Sp , Sn, color = liftModelVar))+
  scale_color_discrete(guide = guide_legend(title = "method"))
library(cowplot)
plot_grid(p1, p2, labels=c("lift", "ROC"))

在此处输入图像描述

于 2017-09-18T19:21:39.457 回答