我正在尝试使用在修剪rpart model()之后创建的模型来创建ROC图表。我正在使用的响应变量具有3个类,而不是2个类。虽然,我相信您可以使用其他方法来绘制ROC图表。以下是我的部分代码:
#Original model creation
control <- rpart.control(minsplit = 5L,maxdepth = 5L,minbucket = 5,cp = 0.002,maxsurrogate =4)
model <- rpart(qualitylevel~.,train,method = "class",control = control
#finding the best cp
bestcp=model$cptable[which.min(model$cptable[,"xerror"]),"CP"] #Find best CP
#creating the 'best tree' based on the best cp
best.tree = prune(model,cp=bestcp)
#prediction based on the best.tree model using classification
best.pred = predict(best.tree,val[,-12],type = "class")
我尝试在代码中使用以下方法。我确实得到了结果,但是使用的模型不同。这是使用朴素贝叶斯模型,而不是从rpart()函数派生的模型。我是否可以更改此代码,所以我正在使用best.tree和预测来计算ROC。
lvls = levels(wine_quality$qualitylevel)
testidx = which(1:length(wine_quality[,1]) %% 5 == 0)
wq.train = wine_quality[testidx,]
wq.test = wine_quality[-testidx,]
#Creating the ROC chart
aucs = c()
plot(x=NA,y=NA,xlim=c(0,1),ylim=c(0,ylab='True Positive Rate',xlab='False Positive Rate',bty='n')
for (type.id in 1:3) {
type = as.factor(wq.train$qualitylevel == lvls[type.id])
nbmodel = rpart(type ~ .,data=wq.train[,-12])
nbprediction = predict(nbmodel,wq.test[,type='class')
score = nbprediction$posterior[,'TRUE']
actual.class = wq.test$qualitylevel == lvls[type.id]
pred = prediction(score,actual.class)
nbperf = performance(pred,"tpr","fpr")
roc.x = unlist(nbperf@x.values)
roc.y = unlist(nbperf@y.values)
lines(roc.y ~ roc.x,col=type.id+1,lwd=2)
nbauc = performance(pred,"auc")
nbauc = unlist(slot(nbauc,"y.values"))
aucs[type.id] = nbauc
}
lines(x=c(0,c(0,1))
mean(aucs)
如果任何人都可以帮助合并以上代码来创建类似于我使用rpart()函数创建的模型的ROC图表。这将是非常感谢。如果您要我澄清任何事情,请告诉我。