尝试在R的插入符包中获取类概率时出错

时间:2022-12-07 12:49:12

I found questions similar to this, but the didn't fix my problem: I use caret with the ranger method to fit a random forest, then use predict to predict my evaluation data. This works. But when I try to get the prediction probabilities, I get the following error:

我发现了与此类似的问题,但并没有解决我的问题:我使用插入方法的插入符合随机森林,然后使用预测来预测我的评估数据。这很有效。但是当我试图获得预测概率时,我得到以下错误:

Error in [.data.frame(out, , obsLevels, drop = FALSE) : undefined columns selected

[.data.frame(out ,, obsLevels,drop = FALSE)出错:选择了未定义的列

The code (an example)

代码(例子)

require(caret)

mtcars$carb <- as.factor(mtcars$carb)

tuneGrid <- expand.grid(mtry = c(10), min.node.size = c(1), splitrule = "extratrees")
rf_model<-train(carb~.,data=mtcars,method="ranger",
                trControl=trainControl(method="none")
                , tuneGrid = tuneGrid
)

predict(rf_model, mtcars, type="prob")

I made sure that carb is a factor as suggested elsewhere.

我确保碳水化合物是其他地方建议的因素。

Thoughts?

1 个解决方案

#1


4  

There are a couple of issues. First, this approach requires that the class levels of the factor follow the convention of valid R variable names, so renaming the levels of the carb factor to start with a letter is the first step

有几个问题。首先,这种方法要求因子的类级别遵循有效R变量名称的约定,因此将碳水化合物因子的级别重命名为以字母开头是第一步

mtcars$carb <- as.factor(paste0("c",mtcars$carb))

Second, the default argument of classProbs in TrainControl is set to FALSE. This should be TRUE in your case.

其次,TrainControl中classProbs的默认参数设置为FALSE。在您的情况下,这应该是正确的。

library("caret")

tuneGrid <- expand.grid(mtry = c(10), min.node.size = c(1), splitrule = "extratrees")
rf_model <- train(carb ~ ., data = mtcars, method = "ranger",
              trControl = trainControl(method = "none", classProbs = TRUE),
              tuneGrid = tuneGrid)

classprobs <- predict(rf_model, newdata = mtcars, type = "prob")

#1


4  

There are a couple of issues. First, this approach requires that the class levels of the factor follow the convention of valid R variable names, so renaming the levels of the carb factor to start with a letter is the first step

有几个问题。首先,这种方法要求因子的类级别遵循有效R变量名称的约定,因此将碳水化合物因子的级别重命名为以字母开头是第一步

mtcars$carb <- as.factor(paste0("c",mtcars$carb))

Second, the default argument of classProbs in TrainControl is set to FALSE. This should be TRUE in your case.

其次,TrainControl中classProbs的默认参数设置为FALSE。在您的情况下,这应该是正确的。

library("caret")

tuneGrid <- expand.grid(mtry = c(10), min.node.size = c(1), splitrule = "extratrees")
rf_model <- train(carb ~ ., data = mtcars, method = "ranger",
              trControl = trainControl(method = "none", classProbs = TRUE),
              tuneGrid = tuneGrid)

classprobs <- predict(rf_model, newdata = mtcars, type = "prob")