Skip to content

Instantly share code, notes, and snippets.

@ericnovik
Last active August 29, 2015 14:06
Show Gist options
  • Save ericnovik/33a8a02004190761d50d to your computer and use it in GitHub Desktop.
Save ericnovik/33a8a02004190761d50d to your computer and use it in GitHub Desktop.
var_imp <- function(rpart_fit) {
var_imp <- sort(rpart_fit$variable.importance)
in_model <- levels(rpart_fit$frame$var)[-1]
return(list(var_imp = var_imp, in_model = in_model))
}
var_imp_plot <- function(rpart_fit) {
vars <- var_imp(rpart_fit)
groups <- factor(x = rep("less so", length(vars$var_imp)),
levels = c("important", "less so"))
match_idx <- match(vars$in_model, names(vars$var_imp))
groups[match_idx] <- "important"
color <- character(length(vars$var_imp))
color[match_idx] <- "red"
color[-match_idx] <- "blue"
dotchart(vars$var_imp, labels = names(vars$var_imp), cex = 0.7, pch = 19,
groups = groups, color = color)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment