【发布时间】:2020-08-12 14:49:48
【问题描述】:
我正在撰写结果分析,并希望在执行随机森林后从变量重要性图中更改我的 MeanDecreaseAccuracy 图。我只想获取 MeanDecreaseAccuracy 图表并将其转换为条形图,以提供比当前显示的更好的可视化效果。
最好的方法是什么?
我当前的代码(在此之前有很多事情要做,但是对于这个例子来说这应该足够了):
wine=read.csv("wine_dataset.csv")
wine$quality01[wine$quality >= 7] <- 1
wine$quality01[wine$quality < 7] <- 0
wine$quality01=as.factor(wine$quality01)
summary(wine)
num_data <- wine[,sapply(wine,is.numeric)]
hist.data.frame(num_data)
set.seed(8, sample.kind = "Rounding") #Set Seed to make sure results are repeatable
wine.bag=randomForest(quality01 ~ alcohol + volatile_acidity + sulphates + residual_sugar +
chlorides + free_sulfur_dioxide + fixed_acidity + pH + density +
citric_acid,data=wine,mtry=3,importance=T) #Use Random Forest with a mtry value of 3 to fit the model
wine.bag #Review the Random Forest Results
plot(wine.bag) #Plot the Random Forest Results
imp=as.data.frame(importance(wine.bag)) #Analyze the importance of each variable in the model
imp=cbind(vars=rownames(imp),imp)
barplot(imp$MeanDecreaseAccuracy, names.arg=imp$vars)
我目前的输出是:
我正在使用的当前代码可以在here找到:
【问题讨论】:
-
您可以获取重要性值并在其上运行 base
barplot或 ggplotgeom_col。例如,如果您的模型对象是randomForest模型,您可以执行imp=importance(my_model)来获取各种重要性统计信息,将其转换为数据框(如有必要),并根据需要进行绘图。
标签: r random-forest