R语言 对LightGBM回归预测模型计算变量重要性,绘制横向对称柱状图,纵轴左侧为Gain,纵轴右侧为Cover,横轴为数值大小,纵轴排列Feature,背景为白色,全部字体设置为"times new roman"
时间: 2024-10-08 11:22:22 浏览: 32
安卓横向柱状图demo2
5星 · 资源好评率100%
在R语言中,使用LightGBM进行回归预测并计算变量的重要性通常涉及到`lightgbm`包以及数据可视化库,如`ggplot2`或`grDevices`。要创建一个包含Gain和Cover信息的横向对称柱状图,你可以按照以下步骤操作:
1. 首先,加载必要的库:
```R
library(lightgbm)
library(ggplot2)
```
2. 训练LightGBM模型并获取变量重要性数据:
```R
lgb_train <- lgb.Dataset(data = your_data, label = your_labels) # 替换为实际的数据和标签
params <- list(objective = "regression", # 如果是回归任务
num_leaves = 31, # 可调整的参数
learning_rate = 0.05,
min_child_weight = 10,
importance_type = "gain") # 获取Gain值
model <- lgb.train(params, lgb_train)
importance <- varImp(model)
```
3. 创建数据框来存储重要性数据:
```R
imp_df <- data.frame(FEATURE = rownames(importance), Gain = importance$gain, Cover = importance$cover)
```
4. 使用`ggplot2`绘制图形:
```R
ggplot(imp_df, aes(x = NULL, y = reorder(FEATURE, -Gain), fill = c(Gain, Cover))) +
geom_bar(stat = "identity", width = unit(1, "npc"), position = position_dodge(width = 0.9)) +
scale_fill_manual(values = c("white", "#E69F00")) + # Gain为黄色,Cover为橙色
geom_text(aes(label = round(Gain, 2), color = "black"),
hjust = 1, vjust = 1, size = 3, family = "Times New Roman") +
geom_text(aes(label = round(Cover, 2), color = "black"),
hjust = -1, vjust = 1, size = 3, family = "Times New Roman") +
theme_minimal(base_size = 12) +
coord_flip() +
labs(title = "Variable Importance (Gain and Cover)", x = "", y = "") +
guides(fill = guide_legend(reverse = TRUE)) +
theme(plot.background = element_rect(fill = "white", color = NA),
axis.title.y = element_blank(),
axis.text.y = element_text(family = "Times New Roman"),
axis.ticks.y = element_blank()) # 设置字体等样式
```
记得将`your_data`和`your_labels`替换为你实际的数据和目标变量。
阅读全文