多类别中的可变重要性

问题描述

我有一个像虹膜这样的数据集,而我的y是一个多类因子变量。有什么方法可以让method = rfmethod = treebagmethod = boost看到相同的结果,谢谢!

  data(iris); head(iris)
iris$Species <- factor(iris$Species)

set.seed(87)
inTrainingSet <- createDataPartition(iris$Species,p=.80,list=0)
train <- iris[inTrainingSet,]
test  <- iris[-inTrainingSet,]
ctrl <- trainControl(method = "cv",number = 2,verboseIter = TRUE)


pls <- train(Species ~ Sepal.Length+Sepal.Width+Petal.Length+Petal.Width,method = "pls",data = iris,trControl = ctrl)
attributes(varImp(pls))
varImp(pls)$importance

解决方法

您的问题有几点,因此,如果有一个内置方法可以针对每个模型正确估算,则可以使用默认的useModel = FALSE运行varImp。

对于randomforest,您在拟合时添加importance=TRUE

rf <- train(Species ~ Sepal.Length+Sepal.Width+Petal.Length+Petal.Width,method = "rf",data = iris,trControl = ctrl,importance=TRUE)
varImp(rf)

rf variable importance

  variables are sorted by maximum importance across the classes
             setosa versicolor virginica
Petal.Length  66.94     100.00     85.40
Petal.Width   63.86      92.22     89.87
Sepal.Length  16.75      24.05     24.90
Sepal.Width   12.75       0.00     17.49

如果模型没有内置的多类模型,则使用成对的roc曲线得出这些重要性,请参见page for caret上的特殊性:

tb <- train(Species ~ Sepal.Length+Sepal.Width+Petal.Length+Petal.Width,method = "treebag",importance=TRUE)

varImp(tb,useModel=TRUE)
treebag variable importance

             Overall
Petal.Length  100.00
Petal.Width    99.17
Sepal.Length   32.23
Sepal.Width     0.00

 varImp(tb,useModel=FALSE)
ROC curve variable importance

  variables are sorted by maximum importance across the classes
             setosa versicolor virginica
Petal.Width  100.00     100.00     100.0
Petal.Length 100.00     100.00     100.0
Sepal.Length  90.70      59.30      90.7
Sepal.Width   54.59      54.59       0.0

您没有指定使用哪种增强树方法,但是我想您可以轻松使用上面的选项之一