Skip to content

Instantly share code, notes, and snippets.

@GuiMarthe
Created January 22, 2018 19:12
Show Gist options
  • Select an option

  • Save GuiMarthe/170903b6c47a2ea5c6b7a8e8a14624e8 to your computer and use it in GitHub Desktop.

Select an option

Save GuiMarthe/170903b6c47a2ea5c6b7a8e8a14624e8 to your computer and use it in GitHub Desktop.
A multiclass decision tree example
library(rpart)
library(tidyverse)
library(ggdendro)
ggplot(data = iris,
aes(Sepal.Length, Petal.Length, color = Species))+
geom_point()
dt <- rpart(Species ~ Sepal.Length + Petal.Length,
data = iris,
method = 'class')
printcp(dt)
plotcp(dt) #cross validation error
ddata <- dendro_data(dt)
ggplot() +
geom_segment(data = ddata$segments,
aes(x = x, y = y, xend = xend, yend = yend)) +
geom_text(data = ddata$labels,
aes(x = x, y = y, label = label), size = 3, vjust = 0) +
geom_text(data = ddata$leaf_labels,
aes(x = x, y = y, label = label), size = 3, vjust = 1) +
theme_void()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment