绘制MLR中的升力曲线

时间:2017-09-18 16:11:56

标签: r mlr

我想知道如何在MLR中绘制升力曲线,特别是对于具有多种算法和任务的基准测试。帮助ROC曲线绘图也将受到赞赏。 感谢。

1 个答案:

答案 0 :(得分:2)

我不是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))

enter image description here

您可以使用相同的数据框来绘制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))

enter image description here

您还可以查看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"))

enter image description here