如何计算R插入符号包中每个折叠的AUC?
我需要计算每个折叠的混淆矩阵和AUC。在R中使用插入符号包。 对于混淆矩阵,一切正常。但对于计算AUC,我有一个错误: 选择未定义的列 有没有其他方法来计算AUC如何计算R插入符号包中每个折叠的AUC?,r,machine-learning,r-caret,R,Machine Learning,R Caret,我需要计算每个折叠的混淆矩阵和AUC。在R中使用插入符号包。 对于混淆矩阵,一切正常。但对于计算AUC,我有一个错误: 选择未定义的列 有没有其他方法来计算AUC df$ximp<-df$ximp[sample(nrow(df$ximp)),] #Create 5 equally size folds folds <- cut(seq(1,nrow(df$ximp)),breaks=5,labels=FALSE) #Perform 5fold cross validation -
df$ximp<-df$ximp[sample(nrow(df$ximp)),]
#Create 5 equally size folds
folds <- cut(seq(1,nrow(df$ximp)),breaks=5,labels=FALSE)
#Perform 5fold cross validation - random forest
importance=list()
for(i in 1:5){
#Segement your data by fold using the which() function
testIndexes <- which(folds==i,arr.ind=TRUE)
testData <- df$ximp[testIndexes, ]
trainData <- df$ximp[-testIndexes, ]
rf_fit <- train(Vital_status ~ .,
data = trainData,
method = "ranger",
importance = 'permutation')
pred=predict(rf_fit, testData,type="prob")
paste("###### fold 1")
print(confusionMatrix(pred, testData$Vital_status))
a=varImp(rf_fit)
importance[[i]] <- a
auc(testData$Vital_status,as.numeric(pred[2]),direction="<", levels = levels(df$ximp$Vital_status))
par(pty="s")
roc(testData$Vital_status,as.numeric(pred),plot = TRUE,precent=TRUE,print.auc=TRUE,legacy.axes=TRUE,percent=TRUE,
col="#4daf4a",main = paste("ROC of random forest fold",i))
}
df$ximpi如果您使用的是pROC软件包,请参阅如何使用auc
,我怀疑您没有正确使用它