2017-09-18 62 views
-2

我想知道如何繪製MLR中的升力曲線,特別是對於具有多種算法和任務的Benchmark實驗。 ROC曲線繪圖的幫助也將被讚賞。 謝謝。在MLR中繪製升力曲線

+0

你有沒有看看[教程](https://mlr-org.github.io/mlr-tutorial/devel/html/roc_analysis/index.html)? –

回答

2

我不是mlr用戶,但這裏是一個通用的方法。 首先是一些數據:

兩類問題

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

1模型:

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它的方法來繪製兩種類型的地塊

。另外,如果你是,你可以使用lift_obj還繪製升力和ROC曲線與它GGPLOT2用戶。

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