在 randomForest R 中使用 VarImpPlot 时遇到问题
Trouble using VarImpPlot in randomForest R
我使用以下代码构建了一个 RandomForest 模型:
library(randomForest)
set.seed(101)
RFs1 = ERC[sample(nrow(ERC),100000),]
RFs2 <- RFs1[,-c(1,2,3,228,229,230,232,233,234,235,240)]
RFs2 <- RFs2[complete.cases(RFs2),] # handling missing values
RFfit <- randomForest(as.factor(RFs2$earlyR)~., data=RFs2[,-231])
VI_F <- importance(RFfit)
varImpPlot(VI_F, type = 2)
现在,当我尝试绘制 Feature Importance
时,出现以下错误:
Error in varImpPlot(VI_F, type = 2) :
This function only works for objects of class `randomForest'
我在这里 (Stack Overflow) 和网上寻找问题的解决方案,但是我找不到。
任何帮助将不胜感激!
我将尝试解释代码中的两个问题。我将使用 mtcars
执行此操作,因为您没有提供样本数据。首先,您需要在调用 randomForest
时传递 importance = TRUE
。
mtrf <- randomForest(mpg ~ . , data = mtcars, importance = TRUE)
您可以将 importance
作为 table 与
importance(mtrf)
> importance(mtrf)
%IncMSE IncNodePurity
cyl 11.584480 194.396219
disp 12.560117 230.427777
hp 12.908195 201.095073
drat 5.238172 69.766801
wt 12.449930 233.921376
qsec 3.705991 27.621441
vs 4.221830 27.044382
am 1.982329 9.416001
gear 3.472656 18.282543
carb 6.116177 28.398651
但是,要绘制该图,您需要使用 importance = TRUE
参数对您创建的实际 randomForest
对象调用 varImpPlot
。
varImpPlot(mtrf)
我会推荐 Introduction to Statistical Learning with Applications in R 作为在 R
中使用 randomForest
包的一个很好的介绍。
我使用以下代码构建了一个 RandomForest 模型:
library(randomForest)
set.seed(101)
RFs1 = ERC[sample(nrow(ERC),100000),]
RFs2 <- RFs1[,-c(1,2,3,228,229,230,232,233,234,235,240)]
RFs2 <- RFs2[complete.cases(RFs2),] # handling missing values
RFfit <- randomForest(as.factor(RFs2$earlyR)~., data=RFs2[,-231])
VI_F <- importance(RFfit)
varImpPlot(VI_F, type = 2)
现在,当我尝试绘制 Feature Importance
时,出现以下错误:
Error in varImpPlot(VI_F, type = 2) : This function only works for objects of class `randomForest'
我在这里 (Stack Overflow) 和网上寻找问题的解决方案,但是我找不到。
任何帮助将不胜感激!
我将尝试解释代码中的两个问题。我将使用 mtcars
执行此操作,因为您没有提供样本数据。首先,您需要在调用 randomForest
时传递 importance = TRUE
。
mtrf <- randomForest(mpg ~ . , data = mtcars, importance = TRUE)
您可以将 importance
作为 table 与
importance(mtrf)
> importance(mtrf)
%IncMSE IncNodePurity
cyl 11.584480 194.396219
disp 12.560117 230.427777
hp 12.908195 201.095073
drat 5.238172 69.766801
wt 12.449930 233.921376
qsec 3.705991 27.621441
vs 4.221830 27.044382
am 1.982329 9.416001
gear 3.472656 18.282543
carb 6.116177 28.398651
但是,要绘制该图,您需要使用 importance = TRUE
参数对您创建的实际 randomForest
对象调用 varImpPlot
。
varImpPlot(mtrf)
我会推荐 Introduction to Statistical Learning with Applications in R 作为在 R
中使用 randomForest
包的一个很好的介绍。