使用rocr包的决策树的ROC曲线

时间:2016-02-15 08:46:45

标签: r classification data-mining decision-tree

我使用rpart包来开发我的树并预测模型。最后,为了绘制ROC曲线,我尝试使用rocr包。不使用内置数据集复制它的道歉是无法做到的。请找到我使用的csv的链接:

Wine Quality.csv

现在请看我的代码:

#setting up data
data<- read.csv(file.choose())
quality_binary <- ifelse(wine_quality >5,"high","low")
data <- data.frame(data,quality_binary)

#re shuffling the data
set.seed(9850)
g <- runif(nrow(data))
datar<- data[order(g),]
#removing the wine quality column since it has to be predicted
datar <- datar[-12]

library(rpart)  
library(rpart.plot)
library(cvTools) 
library(caret)
library(tree)

k <- 10 # setting the value for 10 fold validation 

folds <- cvFolds(NROW(datar), K=k)
datar$holdoutpred <- rep(0,nrow(datar))

for(i in 1:k){

train <- datar[folds$subsets[folds$which != i], ] #training set
validation <- datar[folds$subsets[folds$which == i], ] #validation set

#tree model
tree_model_rpart_gini = rpart(quality_binary~.,data = train,
        parms = list(split = "information"), method  = "class")
rpart.plot(tree_model_rpart_gini,type = 3,extra = 101)

#prediction
pred_model_rpart_gini <- predict(tree_model_rpart_gini,   
newdata=validation, type="class")

datar[folds$subsets[folds$which == i], ]$holdoutpred <-   
pred_model_rpart_gini

}

#plotting ROC curve

library(ROCR)
 pred1 <- prediction(predict(datar$pred_model_rpart_gini),   
 datar$quality_binary)
 perf1 <- performance(pred1,"tpr","fpr")
 plot(perf1)

我的错误是:

pred1 <- prediction(predict(datar$pred_model_rpart_gini),   
datar$quality_binary)
Error in UseMethod("predict") : 
no applicable method for 'predict' applied to an object of class "NULL"

1 个答案:

答案 0 :(得分:0)

local function getMinutes(hours,minutes) return (hours*60)+minutes end local value1 = getMinutes(time1.hours,time1.minutes) local value2 = getMinutes(time2.hours,time2.minutes) local currentTime = getMinutes(tonumber(os.date("%H"),tonumber(os.date("%M"))) local isBetween = false if (currentTime >= value1 and currentTime <= value2) or (currentTime >= value2 and currentTime <= value1) then isBetween = true end datar$pred_model_rpart_gini,即未定义。

Ou可能意味着使用NULL(不是pred_model_rpart_gini)代替?