【问题标题】:Customizing Importance Plot - R自定义重要性图 - R
【发布时间】: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 或 ggplot geom_col。例如,如果您的模型对象是 randomForest 模型,您可以执行 imp=importance(my_model) 来获取各种重要性统计信息,将其转换为数据框(如有必要),并根据需要进行绘图。

标签: r random-forest


【解决方案1】:

这里有几个选项:

library(randomForest)
library(tidyverse)

# Random forest model
iris.rf <- randomForest(Species ~ ., data=iris, importance=TRUE)

# Get importance values as a data frame
imp = as.data.frame(importance(iris.rf))
imp = cbind(vars=rownames(imp), imp)
imp = imp[order(imp$MeanDecreaseAccuracy),]
imp$vars = factor(imp$vars, levels=unique(imp$vars))

barplot(imp$MeanDecreaseAccuracy, names.arg=imp$vars)

imp %>% 
  pivot_longer(cols=matches("Mean")) %>% 
  ggplot(aes(value, vars)) +
  geom_col() +
  geom_text(aes(label=round(value), x=0.5*value), size=3, colour="white") +
  facet_grid(. ~ name, scales="free_x") +
  scale_x_continuous(expand=expansion(c(0,0.04))) +
  theme_bw() +
  theme(panel.grid.minor=element_blank(),
        panel.grid.major=element_blank(),
        axis.title=element_blank())

我也不会放弃点图,它(恕我直言)是更清晰的可视化。以下选项比您问题中的内置输出更具自定义性:

dotchart(imp$MeanDecreaseAccuracy, imp$vars, 
         xlim=c(0,max(imp$MeanDecreaseAccuracy)), pch=16)

imp %>% 
  pivot_longer(cols=matches("Mean")) %>% 
  ggplot(aes(value, vars)) +
  geom_point() +
  facet_grid(. ~ name) +
  scale_x_continuous(limits=c(0,NA), expand=expansion(c(0,0.04))) +
  theme_bw() +
  theme(panel.grid.minor=element_blank(),
        panel.grid.major.x=element_blank(),
        panel.grid.major.y=element_line(),
        axis.title=element_blank())

您也可以绘制值本身而不是点标记。例如:

imp %>% 
  pivot_longer(cols=matches("Mean")) %>% 
  ggplot(aes(value, vars)) +
  geom_text(aes(label=round(value,1)), size=3) +
  facet_grid(. ~ name, scales="free_x") +
  scale_x_continuous(limits=c(0,NA), expand=expansion(c(0,0.06))) +
  theme_bw() +
  theme(panel.grid.minor=element_blank(),
        panel.grid.major.x=element_blank(),
        panel.grid.major.y=element_line(),
        axis.title=element_blank())

【讨论】:

  • 这很有帮助,有没有办法从重要性图中获取实际的变量名称?当我执行imp = cbind(vars=rownames(imp), imp) 时,我的行名只有值 1:10,而不是“酒精”、“pH”等。
  • 为了弄清楚这一点,我需要一个可重现的示例,其中包含您实际运行的代码的信息以及用于测试它的示例数据。
  • 我已经更新了我的原始帖子以包含指向数据集的链接和我的部分代码。
【解决方案2】:

你也可以考虑棒棒糖图表(例如来自ggalt),或者像这里一样:https://uc-r.github.io/lollipop 示例:

suppressPackageStartupMessages({
    library(ggalt)
    library(randomForest)
    library(data.table)
})

# Random forest model (from @eipi10)
iris.rf <- randomForest(Species ~ ., data=iris, importance=TRUE)
imp <- data.table(importance(iris.rf), keep.rownames = TRUE, 
                  key = "MeanDecreaseAccuracy")
imp[, rn := factor(rn, unique(rn))]
ggplot(melt(imp, id.vars="rn")[grep("Mean", variable)], 
       aes(x=rn, y=value, label = round(value, 1))) + 
    geom_lollipop(point.size = 3, point.colour = "cadetblue") +
    geom_text(nudge_y = 5) +
    coord_flip() +
    facet_wrap(~variable) +
    theme_minimal() +
    labs(y="Percent", x=NULL)

reprex package (v0.3.0) 于 2020 年 4 月 28 日创建

【讨论】:

    猜你喜欢
    • 2021-04-23
    • 2019-02-11
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多