8/31/2021

Introduction

The first step to built the app was to create a Machine Learning model. The objective was to create a model to predict the iris species based on their features. For this, the caret package was used along with the decision tree algorithm (rpart)

The iris data with the features used to build the model are as follows:

##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

Algorithm workflow

The workflow for the creation of the algorithm was:

  • Load caret package
  • Partition of the data into training and testing
  • Train the algorithm using the rpart method(decision tree) and cross validation
  • Apply model into the test set to verify the error rate
  • Creation of the decision tree plot
  • Apply the model into the new data given by the user

Algorithm code

The code for the algorithm is below:

library(caret)

partition <- createDataPartition(y=iris$Species,p=0.7,list=FALSE)
train <- iris[partition,]
test <- iris[-partition,]

dtree <-  train(Species ~ .,data=train,method="rpart",
                trControl = trainControl(method = "cv"))
    
testpred <-  predict(dtree, newdata = test)
    
error  <- round(mean(testpred != test$Species), 2)

The finished model

library(rattle)
fancyRpartPlot(dtree$finalModel, uniform=TRUE)

Input and Output of the app

The code presented before is embedded into the Shiny app along with the programming for appropriate interactivity.

When values are inputted into the side panel of the app the model is applied to such values and a output of the predicted species is given.

An example of some values and its output is given below.

sl =1.5 ; sw =10 ; pl=15 ; pw = 7
predict(dtree, newdata=data.frame(Sepal.Length=sl, Sepal.Width=sw, 
                                  Petal.Length=pl, Petal.Width=pw))
## [1] virginica
## Levels: setosa versicolor virginica