使用 rocr 包的决策树的 ROC 曲线
ROC curve for decision trees using rocr package
我使用 rpart
包来开发我的树并预测模型。最后为了绘制 ROC 曲线,我尝试使用 rocr
包。抱歉不能用内置数据集复制它。请找到我使用的 csv 的 link:
现在请看我的代码:
#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"
datar$pred_model_rpart_gini
是 NULL
即未定义。
你可能打算用 pred_model_rpart_gini
(不是 datar$
)代替?
我使用 rpart
包来开发我的树并预测模型。最后为了绘制 ROC 曲线,我尝试使用 rocr
包。抱歉不能用内置数据集复制它。请找到我使用的 csv 的 link:
现在请看我的代码:
#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"
datar$pred_model_rpart_gini
是 NULL
即未定义。
你可能打算用 pred_model_rpart_gini
(不是 datar$
)代替?