AC3112
November 18, 2021, 5:58pm
1
Hi All,
I've been following a published script on conditional variable importance, here:https://www.r-bloggers.com/2018/06/be-aware-of-bias-in-rf-variable-importance-metrics/
Yet, I've noticed when I follow this particular script that I am unable to obtain variable names on the vertical axis. Instead, they are identified by their column number.
I wondered if someone knew of a way around this? Would be appreciated
library(tidyverse)
library(caret)
library(ranger)
library(ggplot2)
library(party)
rf3 <- cforest( score ~ ., data = readingSkills, control = cforest_unbiased(mtry = 2, ntree = 500)
imp <- rf %>%
varimp(conditional = TRUE) %>%
as_tibble() %>%
rownames_to_column("Feature") %>%
rename(Importance = value)
ggplot(imp, aes(x = reorder(Feature, Importance), y = Importance)) +
geom_bar(stat = "identity", fill = "#53cfff", width = 0.65) +
coord_flip() +
theme_light(base_size = 20) +
theme(axis.title.x = element_text(size = 15, color = "black"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black"),
axis.text.y = element_text(size = 15, color = "black"))
WARNING. THE SCRIPT TAKES AROUND 15-20 MINS
I reduced the ntree down from 500 to 100 to reduce waiting time... I found I had to add a missing bracket from the end there.
also when you make imp , there is no rf to make it from, rather rf3.
The problem you had was the varimp result gives a named numeric vector, as_tibble default behaviour wont capture that as nicely as other more appropriate tooling, such as dplyr::enframe.
library(tidyverse)
library(caret)
library(ranger)
library(party)
rf3 <- cforest( score ~ ., data = readingSkills, control = cforest_unbiased(mtry = 2, ntree = 100))
imp <- rf3 %>%
varimp(conditional = TRUE) %>%
enframe() %>%
rename(Feature = name,
Importance = value)
ggplot(imp, aes(x = reorder(Feature, Importance), y = Importance)) +
geom_bar(stat = "identity", fill = "#53cfff", width = 0.65) +
coord_flip() +
theme_light(base_size = 20) +
theme(axis.title.x = element_text(size = 15, color = "black"),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 15, color = "black"),
axis.text.y = element_text(size = 15, color = "black"))
AC3112
November 19, 2021, 11:56am
3
Thank you @nirgrahamuk . That worked perfectly for me. Thanks for your help
system
Closed
November 26, 2021, 11:56am
4
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.