Caret - 多类别分类中的因子问题

时间:2017-11-13 17:44:11

标签: r r-caret

我想在caret包中执行多类分类。以下是最低限度的示例。

library(caret)   
library(randomForest)


x <- data.frame("A"=seq(1,100), "B"=seq(1,100), "C"="class1")
x[,"C"] <- as.character(x[,"C"])
x[1,"C"] <- "class2"
x[2,"C"] <- "class3"
x[3,"C"] <- "class4"
x[4,"C"] <- "class5"
x[5,"C"] <- "class6"
x[6,"C"] <- "class7"
x[7,"C"] <- "class8"
x[8,"C"] <- "class9"
x[9,"C"] <- "class10"
x[10,"C"] <- "class11"
x[11,"C"] <- "class12"
x[,"C"] <- as.factor(x[,"C"])

control <- trainControl(method="repeatedcv", number=10, repeats=1,   search="grid")     set.seed(5)     tunegrid <- expand.grid(.mtry=c(1:2))     fit <- train(x=x[,1:2], y=x$C, method="rf", metric=metric,   tuneGrid=tunegrid, trControl=control)  
print(fit)  
plot(fit)

运行代码时,我收到一条错误,指出1: model fit failed for Fold2.Rep1: mtry=1 Error in randomForest.default(x, y, mtry = param$mtry, ...) : Can't have empty classes in y.

相关帖子表明,这是由于响应变量中的未计入因素 - 在重新采样时未考虑这一因素。通常,如果要预测的类数量较多(并且观察很少),则会遇到问题。

是否有任何解决方法可以更改插入符号包,以便在重采样方法中删除缺失的因子(例如,droplevels())?

0 个答案:

没有答案