为变量重要性和绘图实现 varImp
Implement varImp for variable importance and plot
我正在尝试绘制以下模型的变量重要性分数。
分数输出正常,但绘图不正确 - 我需要向代码添加另一个参数吗?
分数的代码和输出如下。
library(caret)
#GENERALISED LINEAR MODEL
LR_swim <- lm(racetime_mins ~ event_date+ event_month +year +event_id +
gender + distance_new + New_Condition+
raceNo_Updated +
handicap_mins +points+
Wind_Speed_knots+
Air_Temp_Celsius +Water_Temp_Celsius +Wave_Height_m,
data = SwimmingTrain)
family=gaussian(link = "identity")
varImp2<-varImp(object=LR_swim)
plot(varImp2,main="Variable Importance")
总体
event_date 24.463358
event_month 22.358448
年 24.399390
event_id 26.878342
性别女 30.422470
性别男 13.273062
distance_new 248.727351
New_Condition 22.574999
raceNo_Updated9.812053
handicap_mins134.914137
点 40.443116
Wind_Speed_knots 14.492203
Air_Temp_Celsius16.562194
Water_Temp_Celsius2.861662
Wave_Height_m8.592716
#ClassOutput
class(varImp2)
[1] "data.frame"
#HeadOutput
> head(varImp2)
Overall
event_date 24.46336
event_month 22.35845
year 24.39939
event_id 26.87834
genderfemale 30.42247
gendermale 13.27306
我的看起来像;
应该看起来像
根据您想要的结果,您的目标是从数据框中绘制一个数字列,按列中的值在 y-axis 上排序。我将使用 mtcars
数据集作为示例。
library(caret)
LR_mtcars <- glm(mpg ~ ., data = mtcars, family = gaussian)
varImp2 <- varImp(LR_mtcars)
varImp2
是一个数据帧。现在添加一个名为 "labels" 的列。我们将此列设置为 factor
,然后根据 "Overall".
中的值对其进行排序
varImp2$labels <- factor(rownames(varImp2))
varImp2$labels <- reorder(varImp2$labels, varImp2$Overall)
然后我们可以绘制这些值。对于绘图的第一次迭代,我们将 x 轴和 y 轴的标题以及 y-axis 的标签留空。然后我们随后将它们添加回去。
plot(x = varImp2$Overall, y = varImp2$labels, main = "Variable Importance",
yaxt = "n", ylab = "", xlab = "")
axis(2, at = 1:nrow(varImp2), labels = levels(varImp2$labels), las = 2)
title(xlab = "Importance")
这给了我们
好吧,在命令中我问过 varImp2
的行名是否是您绘图中所需的 x 值,但您没有告诉。在任何情况下,假设行名是您要分配的 y 值,这些代码会为您提供所需的图,您可以自己安排 x 和 y。
library(ggplot2)
ggplot(data= varImp2, aes(x=rownames(varImp2),y=Overall)) +
geom_bar(position="dodge",stat="identity",width = 0, color = "black") +
coord_flip() + geom_point(color='skyblue') + xlab(" Importance Score")+
ggtitle("Variable Importance") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.background = element_rect(fill = 'white', colour = 'black'))
分数输出正常,但绘图不正确 - 我需要向代码添加另一个参数吗?
分数的代码和输出如下。
library(caret)
#GENERALISED LINEAR MODEL
LR_swim <- lm(racetime_mins ~ event_date+ event_month +year +event_id +
gender + distance_new + New_Condition+
raceNo_Updated +
handicap_mins +points+
Wind_Speed_knots+
Air_Temp_Celsius +Water_Temp_Celsius +Wave_Height_m,
data = SwimmingTrain)
family=gaussian(link = "identity")
varImp2<-varImp(object=LR_swim)
plot(varImp2,main="Variable Importance")
总体 event_date 24.463358 event_month 22.358448 年 24.399390 event_id 26.878342 性别女 30.422470 性别男 13.273062 distance_new 248.727351 New_Condition 22.574999 raceNo_Updated9.812053 handicap_mins134.914137 点 40.443116 Wind_Speed_knots 14.492203 Air_Temp_Celsius16.562194 Water_Temp_Celsius2.861662 Wave_Height_m8.592716
#ClassOutput
class(varImp2)
[1] "data.frame"
#HeadOutput
> head(varImp2)
Overall
event_date 24.46336
event_month 22.35845
year 24.39939
event_id 26.87834
genderfemale 30.42247
gendermale 13.27306
我的看起来像;
应该看起来像
根据您想要的结果,您的目标是从数据框中绘制一个数字列,按列中的值在 y-axis 上排序。我将使用 mtcars
数据集作为示例。
library(caret)
LR_mtcars <- glm(mpg ~ ., data = mtcars, family = gaussian)
varImp2 <- varImp(LR_mtcars)
varImp2
是一个数据帧。现在添加一个名为 "labels" 的列。我们将此列设置为 factor
,然后根据 "Overall".
varImp2$labels <- factor(rownames(varImp2))
varImp2$labels <- reorder(varImp2$labels, varImp2$Overall)
然后我们可以绘制这些值。对于绘图的第一次迭代,我们将 x 轴和 y 轴的标题以及 y-axis 的标签留空。然后我们随后将它们添加回去。
plot(x = varImp2$Overall, y = varImp2$labels, main = "Variable Importance",
yaxt = "n", ylab = "", xlab = "")
axis(2, at = 1:nrow(varImp2), labels = levels(varImp2$labels), las = 2)
title(xlab = "Importance")
这给了我们
好吧,在命令中我问过 varImp2
的行名是否是您绘图中所需的 x 值,但您没有告诉。在任何情况下,假设行名是您要分配的 y 值,这些代码会为您提供所需的图,您可以自己安排 x 和 y。
library(ggplot2)
ggplot(data= varImp2, aes(x=rownames(varImp2),y=Overall)) +
geom_bar(position="dodge",stat="identity",width = 0, color = "black") +
coord_flip() + geom_point(color='skyblue') + xlab(" Importance Score")+
ggtitle("Variable Importance") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.background = element_rect(fill = 'white', colour = 'black'))