Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用带有3个分类的响应变量的rpart从模型中绘制ROC曲线?_R_Roc_Rpart - Fatal编程技术网

如何使用带有3个分类的响应变量的rpart从模型中绘制ROC曲线?

如何使用带有3个分类的响应变量的rpart从模型中绘制ROC曲线?,r,roc,rpart,R,Roc,Rpart,我正在尝试使用修剪rpart模型()后创建的模型创建ROC图表。我使用的响应变量有3个类,而不是2个。尽管如此,我相信你可以用另一种方法绘制ROC图。下面是我代码的一部分: #Original model creation control <- rpart.control(minsplit = 5L, maxdepth = 5L, minbucket = 5, cp = 0.002, maxsurrogate =4) model <- rpart(qualitylevel~.,

我正在尝试使用修剪rpart模型()后创建的模型创建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")
如果有人能帮助合并上面的代码来创建一个ROC图,它类似于我使用rpart()函数创建的模型。这将是非常感谢。如果你想让我澄清什么,请告诉我

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,1),
     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[,-12], 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,1), c(0,1))

mean(aucs)