Skip to content

list.of.packages <- c('tidyverse', 'rsample', 'randomForest','tree','rpart.plot', 'GGally')
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

library(tidyverse)
library(rsample)
library(randomForest)
library(tree)
library(rpart.plot)
library(GGally)
GGally::ggpairs(
  iris
  , ggplot2::aes(colour=Species)
)

Sepal.Length x Petal.Length

data_split <- initial_split(iris, prop = 0.8)

train <- training(data_split)
test  <- testing(data_split)

classifier <- randomForest(
  Species~Sepal.Length+Petal.Length
  , train, localImp = TRUE
)
(tr <- tree(Species~Sepal.Length+Petal.Length, data = train))
tm <- rpart(
  Species~Sepal.Length+Petal.Length
  , data = train
  , method = "class"
)
rpart.plot(tm, tweak = 1.6)
X1 <- seq(min(train$Sepal.Length), max(train$Sepal.Length), length.out = 1000)
X2 <- seq(min(train$Petal.Length), max(train$Petal.Length), length.out = 1000)
grid_set <- expand.grid(X1,X2)
colnames(grid_set) <- c('Sepal.Length','Petal.Length')

prob_set <- predict(
  classifier
  , type = 'response'
  , newdata = grid_set
)
plot(
  train$Sepal.Length
  , train$Petal.Length
  , main = 'Sepal.Length x Petal.Length'
  , xlab = 'Sepal.Length'
  , ylab = 'Petal.Length'
  , xlim = range(X1)
  , ylim = range(X2)
)
contour(
  X1, X2
  , matrix(as.numeric(prob_set), length(X1), length(X2))
  , add = T
)
points(
  grid_set, pch = '.'
  , col = ifelse(
    prob_set == 'setosa', 'tomato'
    , ifelse(prob_set == 'versicolor', 'springgreen3','steelblue')
  )
)
points(
  train$Sepal.Length
  , train$Petal.Length
  , pch = 21
  , bg = ifelse(
    train[,5] == 'setosa', '#c9503a'
    , ifelse(train[,5] == 'versicolor', '#18a15c','#2f55a8')
  )
)

Sepal.Width x Petal.Width

classifier <- randomForest(
  Species~Sepal.Width+Petal.Width
  , train, localImp = TRUE
)
(tr <- tree(Species~Sepal.Width+Petal.Width, data = train))
tm <- rpart(
  Species~Sepal.Width+Petal.Width
  , data = train
  , method = "class"
)
rpart.plot(tm, tweak = 1.6)
X1 <- seq(min(train$Sepal.Width), max(train$Sepal.Width), length.out = 1000)
X2 <- seq(min(train$Petal.Width), max(train$Petal.Width), length.out = 1000)
grid_set <- expand.grid(X1,X2)
colnames(grid_set) <- c('Sepal.Width','Petal.Width')

prob_set <- predict(
  classifier
  , type = 'response'
  , newdata = grid_set
)
plot(
  train$Sepal.Width
  , train$Petal.Width
  , main = 'Sepal.Width x Petal.Width'
  , xlab = 'Sepal.Width'
  , ylab = 'Petal.Width'
  , xlim = range(X1)
  , ylim = range(X2)
)
contour(
  X1, X2
  , matrix(as.numeric(prob_set), length(X1), length(X2))
  , add = T
)
points(
  grid_set, pch = '.'
  , col = ifelse(
    prob_set == 'setosa', 'tomato'
    , ifelse(prob_set == 'versicolor', 'springgreen3','steelblue')
  )
)
points(
  train$Sepal.Width
  , train$Petal.Width
  , pch = 21
  , bg = ifelse(
    train[,5] == 'setosa', '#c9503a'
    , ifelse(train[,5] == 'versicolor', '#18a15c','#2f55a8')
  )
)

Sepal.Length x Petal.Width