绘制多条升力曲线

时间:2018-03-19 12:38:49

标签: r ggplot2 graph lift curve

我是R的新手并且正在努力学习。我试图在一个图中绘制多个分类器的提升曲线。我无法想办法做到这一点。我知道下面两个分类器基本上是相同的,但它们都给出了不同的图形,我只是想将两者结合起来。以下是我试过的代码。有人可以指出我正确的方向吗

    fullmod = glm(Response ~ page_views_90d+win_visits+osx_visits+mc_1+mc_2+mc_3+mc_4+mc_5+mc_6+store_page+orders+orderlines+bookings+purchase, data=training, family=binomial)
summary(fullmod)
fullmod.results <- predict(fullmod, newdata = testing, type='response')
plotLift(fitted.results, test_data_full$class, cumulative = TRUE,col="orange", n.buckets = 5)

redmod1 = glm(Response ~ win_visits+osx_visits+mc_2+mc_4+mc_6+store_page+orders+orderlines+bookings+purchase, data=training, family=binomial)
redmod1.results <- predict(redmod1, newdata = testing, type = 'response')
plotLift(redmod1.results, test_data_full$class, cumulative = TRUE,col="orange", n.buckets = 5)

# Attempt to plot multiple classifiers
plotLift((redmod1.results, fullmod.results), test_data_full$class, cumulative = TRUE,col="orange", n.buckets = 5)

1 个答案:

答案 0 :(得分:1)

这是一种使用插入符号库绘制多个提升曲线的方法。但首先是一些数据:

set.seed(1)
for_lift <- data.frame(Class = factor(rep(1:2, each = 50)),
                       model1 = sort(runif(100), decreasing = TRUE),
                       model2 = runif(100),
                       model3 = runif(100))

这里Class列是真正的类 model1是第一个模型的预测概率,依此类推。

现在使用以下方法从数据中创建一个电梯对象:

library(caret)
lift_curve <- lift(Class ~ model1 + model2, data = for_lift)

并绘制

xyplot(lift_curve, auto.key = list(columns = 3))

enter image description here

如果你想用ggplot绘图:

library(ggplot2)

ggplot(lift_curve$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_curve$pct, 100, 0),
                                 y = c(0, 100, 100, 0)),
               aes(x = x, y = y), alpha = 0.1)

enter image description here