1
votes

enter image description hereI'm trying to plot the variable importance scores for the below model.

The scores output fine but they're not plotting properly- do I need to add another parameter to the code?

Code and Output of Scores are below.

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")

Overall event_date 24.463358 event_month 22.358448 year 24.399390 event_id 26.878342 genderfemale 30.422470 gendermale 13.273062 distance_new 248.727351 New_Condition 22.574999 raceNo_Updated 9.812053 handicap_mins 134.914137 points 40.443116 Wind_Speed_knots 14.492203 Air_Temp_Celsius 16.562194 Water_Temp_Celsius 2.861662 Wave_Height_m 8.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

Mine looks like;

Supposed to look like

enter image description here

2
Can you add the class(vaImp2) and head(varImp2) in your question? So we can see the format and type of the vaImp2. - maydin
@maydin added above! - Kate English
Did you try plot(varImp2[,1],main="Variable Importance") ? - maydin
I added what my plot looks like now, what i'm looking for is the variables to be on the y axis and the importance score on the x - Kate English
There is only one column in your dataset which is called Overall...So I don't get what is the other one? Do you want to use the rownames as the x value even if they are characters? - maydin

2 Answers

0
votes

Well, in the commands I have asked if the rownames of the varImp2 are the desired x values in your plot or not, but you did not tell. In any case, assuming the rownames are the y values you want to assign, those codes give you the desired plot, you may arrange the x and y by yourself.

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'))

enter image description here

0
votes

Based on your desired outcome, your goal is to plot a numeric column from a dataframe, ordered on the y-axis by the values in the column. I'll use the mtcars dataset as an example.

library(caret)
LR_mtcars <- glm(mpg ~ ., data = mtcars, family = gaussian)
varImp2 <- varImp(LR_mtcars)

varImp2 is a dataframe. Now add a column named "labels". We'll make this column a factor, and then order it based on values from "Overall".

varImp2$labels <- factor(rownames(varImp2))
varImp2$labels <- reorder(varImp2$labels, varImp2$Overall)

We can then plot the values. For the first iteration of the plot we leave the titles for the x- and y- axis blank, as well as the labels for the y-axis. We then add those back in subsequently.

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")

This gives us

enter image description here