绘制由Ranger函数计算的特征重要性

问题描述 投票:4回答:1

我需要使用游侠函数绘制变量重要性,因为我有一个大数据表,而randomForest在我的研究中不起作用。

这是我的代码:

library(ranger)
set.seed(42)
model_rf <- ranger(Sales ~ .,data = data[,-1],importance = "impurity")

然后我创建了新的数据帧DF,它包含上面的代码,就像这样

> v<-as.vector(model_rf$variable.importance$Importance)
> w<-(as.vector((row.names(df))))
> DF<-cbind(w,v)
> DF<-as.data.frame(DF)
> DF
                           w                v
1                  DayOfWeek 376393213095.426
2                  Customers 1364058809531.96
3                       Open 634528877741.021
4                      Promo 261749509069.205
5               StateHoliday 5196666310.34041
6              SchoolHoliday  6522969049.3763
7                   DateYear  7035399071.0376
8                  DateMonth 20134820116.2625
9                    DateDay 37631766745.2306
10                  DateWeek 32834962167.9479
11                 StoreType 31568433413.5718
12                Assortment 20257406597.8358
13       CompetitionDistance  111847579772.77
14 CompetitionOpenSinceMonth 46332196019.0118
15  CompetitionOpenSinceYear 45548903472.6485
16                    Promo2                0
17           Promo2SinceWeek 50666744628.7906
18           Promo2SinceYear 40964066303.0407
19           CompetitionOpen 39927447341.0351
20                 PromoOpen  28319356095.063
21            IspromoinSales 2844220121.08598

但我需要根据上面显示的结果绘制这样的图形:

enter image description here

编辑

正如@Sam建议我尝试调整此代码:

> ggplot(DF, aes(x=reorder(w,v), y=v,fill=v))+ 
+   geom_bar(stat="identity", position="dodge")+ coord_flip()+
+   ylab("Variable Importance")+
+   xlab("")+
+   ggtitle("Information Value Summary")+
+   guides(fill=F)+
+   scale_fill_gradient(low="red", high="blue")

但我得到这个错误:

错误:提供给连续刻度的离散值此外:有42个警告(使用警告()来查看它们)>

我该怎么办?先感谢您!

r ggplot2
1个回答
4
投票

这是未经测试但我认为这应该会给你你所追求的:

 ggplot(model_rf$variable.importance, aes(x=reorder(variable,importance), y=importance,fill=importance))+ 
      geom_bar(stat="identity", position="dodge")+ coord_flip()+
      ylab("Variable Importance")+
      xlab("")+
      ggtitle("Information Value Summary")+
      guides(fill=F)+
      scale_fill_gradient(low="red", high="blue")
© www.soinside.com 2019 - 2024. All rights reserved.