Here is the I code used to build a decision tree model while using a scatterplot for visualization. I was trying to plot the decision boundary but I got an error message that I have pasted after the code. I am uncertain whether setting the species as a function of all other variables is causing this error to rise. I would appreciate if anybody had any recommendations I can follow to plot a proper decision boundary.
#load data
data(iris)
#set a seed for randomness reproducable
set.seed(42)
#randomnly sample 100 - 150 row indexes
indexes <- sample(
x = 1:150,
size = 100
)
#create a training set from indexes
train <- iris[indexes,]
#load decision tree package
library(tree)
#train a decision tree model
model <- tree(Species ~ .,train)
#visualize
plot(model)
text(model)
#load color palette
library(RColorBrewer)
#create a scatterplot colored by species
palette <- brewer.pal(3, "Set2")
plot(
x = iris$Sepal.Length,
y = iris$Petal.Width,
pch = 19,
col = palette[as.numeric(iris$Species)],
main = "Length vs Width",
xlab = "Length",
ylab = "Width")
#plot the decision boundaries
partition.tree(
tree = model,
label = "Species",
add = TRUE)
Here is the error I get:-
Error in partition.tree(tree = model, label = "Species", add = TRUE) :
tree can only have one or two predictors
p.s this is the Rstudio version I installed in my computer: Version 1.2.5033