简体   繁体   English

在 R 中计算随机森林 Model 的 AUC

[英]Calculating AUC on Random Forest Model in R

I'm trying to calculate AUC on my two models Random Forest and Naive Bayes but getting the same error ""$ operator is invalid for atomic vectors". Would you have some ideas please?我正在尝试在我的两个模型随机森林和朴素贝叶斯上计算 AUC,但得到相同的错误“”$ 运算符对原子向量无效“。请问您有什么想法吗?

Background: Target variable "Diagnosis" is non-numerical with values B and M背景:目标变量“诊断”是非数值型,具有 B 和 M 值

Here is sample code for RF model这是 RF model 的示例代码

fitControl <- trainControl(method="cv",number = 5,preProcOptions = list(thresh = 0.4),classProbs = TRUE,summaryFunction = twoClassSummary)

wdbc_model_rf <- train(Diagnosis~.,train_wdbc,method="ranger",metric="ROC",preProcess = c('center', 'scale'),trControl=fitControl)

Below is an example of R code that works.下面是一个有效的 R 代码示例。 Please note: your interest in ROC implies there are only two classes.请注意:您对 ROC 的兴趣意味着只有两个课程。

Predict <- function(class_obj, newdata, Param) {

if(Param$method == 'RF') {
    Predicted_Probs         <- predict(class_obj, newdata = newdata, type = 'prob')
} else if(Param$method == 'GBM') {
    Predicted_Probs         <- predict(class_obj, newdata = newdata, type = 'response', n.trees = Param$n.trees)[,,1]
} else if(Param$method == 'SVM') {
    Predicted_Probs         <- predict(class_obj, newdata = newdata, type = 'probabilities')
} else if(Param$method == 'logit') {
    Predicted_Probs         <- predict(class_obj, newdata = newdata, type = 'response')
    Predicted_Probs         <- cbind(1 - Predicted_Probs, Predicted_Probs)
} else { 
    cat('\nPredict(): unknown classification method.')
}

Predicted_Probs[,2]

}

@@@ @@@

AUC <- function(Truth, Predicted_Probs) {

###########################################################################################################
# SETTINGS

d_Prob              <- 0.01

###########################################################################################################
# CALCULATIONS

Prob_Grid               <- seq(1, 0, -d_Prob)
NP                  <- length(Prob_Grid)
True_Positive_Rate      <- c()
False_Positive_Rate     <- c()

for(Prob_Threshold in Prob_Grid) {
    Forecast                <- as.factor( c(0, 1, 1 * (Predicted_Probs >= Prob_Threshold)) )
    levels(Forecast)            <- c('0', '1')
    Forecast                <- Forecast[-c(1,2)]
    Table               <- xtabs(~Truth + Forecast)
    False_Positive_Rate     <- c(False_Positive_Rate, Table[1,2] / (Table[1,1] + Table[1,2]))
    True_Positive_Rate      <- c(True_Positive_Rate, Table[2,2] / (Table[2,1] + Table[2,2]))
}

AUC                 <- 0

for(i in 2:NP) {
    AUC                 <- AUC + True_Positive_Rate[i] * (False_Positive_Rate[i] - False_Positive_Rate[i-1])
}

AUC

}

Please note: the code is quite generic and can be applied to many methods, like support vector machines , gradient boosting , random forests , etc. Hopefully, it is straightforward to modify the code to your needs.请注意:代码非常通用,可以应用于许多方法,如支持向量机梯度提升随机森林等。希望可以直接根据您的需要修改代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM