2017-01-24 1 views
2

다음 코드는 @adibender에서 "하나의 플롯 ROCR에서 여러 ROC 곡선"으로 찍은 것입니다. 이 코드는 부분적으로? plot.performance에서 가져온 것입니다.R : ROCR을 사용하여 여러 가지 다른 색상의 ROC 곡선을 그립니다.

library(ROCR) 
data(ROCR.simple) 
preds <- cbind(p1 = ROCR.simple$predictions, 
      p2 = abs(ROCR.simple$predictions + 
      rnorm(length(ROCR.simple$predictions), 0, 0.1))) 

pred.mat <- prediction(preds, labels = matrix(ROCR.simple$labels, 
      nrow = length(ROCR.simple$labels), ncol = 2)) 

perf.mat <- performance(pred.mat, "tpr", "fpr") 
plot(perf.mat) 

위의 코드와 같이 하나의 플롯에서 r 패키지 ROCR을 사용하여 여러 ROC 곡선을 보여주고 싶습니다. 그러나 ROC 커브가 다른 색으로 나타나길 원합니다. 다른 곡선에 다른 색상을 적용하려면 어떻게해야합니까? 미리 감사드립니다.

답변

3

것은이 (당신이 ROCR와 함께 할 수 있습니다) 시도 :

library(ROCR) 
data(ROCR.simple) 
preds <- cbind(p1 = ROCR.simple$predictions, 
       p2 = abs(ROCR.simple$predictions + 
          rnorm(length(ROCR.simple$predictions), 0, 0.1))) 
n <- 2 # you have n models 
colors <- c('red', 'blue') # 2 colors 
for (i in 1:n) { 
    plot(performance(prediction(preds[,i],ROCR.simple$labels),"tpr","fpr"), 
              add=(i!=1),col=colors[i],lwd=2) 
} 

enter image description here

0

ROCR 개체의 경우 plot 기능은이 옵션을 제공하지 않는 것 같습니다. 따라서 객체를 따로 따로 골라 수동으로 처리해야합니다. ggplot2를 사용하여 예를 들어

,

library(ggplot2) 
df <- data.frame(Curve=as.factor(rep(c(1,2), each=length([email protected][[1]]))), 
       FalsePositive=c([email protected][[1]],[email protected][[2]]), 
       TruePositive=c([email protected][[1]],[email protected][[2]])) 
plt <- ggplot(df, aes(x=FalsePositive, y=TruePositive, color=Curve)) + geom_line() 
print(plt) 
관련 문제