knitr::opts_chunk$set( echo = TRUE, dpi = 300 )
r emo::ji("tree")
r emo::ji("pen")
The beginnings...
TreeTracer is an R package for creating trace plots of trees from random forests fit using the randomForest R package. Trace plots are useful tools for visually comparing trees from a random forest. See @urbanek:2008 for additional information about trace plots.
# Load packages library(dplyr) library(ggpcp) library(ggplot2) library(TreeTracer)
# Load the Palmer penguins data penguins <- na.omit(palmerpenguins::penguins)
# Select the features for training the model penguin_features <- penguins %>% select(bill_length_mm, bill_depth_mm, flipper_length_mm, body_mass_g)
# Create a parallel coordinate plot of features that will # be used to fit a random forest colored by the variable # of interest to predict (species) penguins %>% ggplot(aes(color = species)) + geom_pcp(aes( vars = vars( bill_length_mm, bill_depth_mm, flipper_length_mm, body_mass_g ) ), alpha = 0.5) + scale_color_brewer(palette = "Paired")
# Fit a random forest set.seed(71) penguin_rf <- randomForest::randomForest( species ~ bill_length_mm + bill_depth_mm + flipper_length_mm + body_mass_g, data = penguins, ntree = 50 )
# Print feature importance penguin_rf$importance %>% data.frame() %>% arrange(desc(MeanDecreaseGini))
# Trace plots of trees in the forest trace_plot( rf = penguin_rf, train = penguin_features, tree_ids = 1:penguin_rf$ntree, alpha = 0.4 ) + theme(aspect.ratio = 1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.