1/14/2018

Predicting the Species

  • It is important for botanists to have a quick way of determining the species of an Iris flower.
  • We are going to use the Iris dataset to predict the species of any new flower given its sepal and petal measurements.
  • In the left pane you may enter the measurements of a new flower using the sliders and click on the Submit button.
  • On click of the submit button a prediction of the species of this new flower is made using the Random Forest method and output is displayed in the main panel.
  • Following slides will explain the implementation of Random Forest model along with an example prediction.

Random Forest Prediction

server.R code
library(shiny)
library("caret")
shinyServer(function(input, output) {
  predRF <- reactive({
      fitRF <- train(Species ~ ., data = iris, method = "rf")
      pred <- predict(fitRF, newdata = data.frame(
              Sepal.Length <- input$SepalLength, 
              Sepal.Width <- input$SepalWidth, 
              Petal.Length <- input$PetalLength, 
              Petal.Width <- input$PetalWidth))
      as.character(pred)
  })
  output$Prediction <- renderText(predRF())
})

Random Forest Model Output

Random Forest 

150 samples
  4 predictor
  3 classes: 'setosa', 'versicolor', 'virginica' 

No pre-processing
Resampling: Bootstrapped (25 reps) 
Summary of sample sizes: 150, 150, 150, 150, 150, 150, ... 
Resampling results across tuning parameters:

  mtry  Accuracy   Kappa    
  2     0.9463150  0.9187684
  3     0.9492140  0.9231549
  4     0.9484022  0.9218607

Accuracy was used to select the optimal model using  the largest value.
The final value used for the model was mtry = 3.

Example Prediction Using the RF Model

pred <- predict(fitRF, newdata = data.frame(Sepal.Length <- 3.5, 
    Sepal.Width <- 3.5, Petal.Length <- 5.5, Petal.Width <- 5.5))
as.character(pred)
## [1] "virginica"

User Benefits of the Application

  • Extremely simple user interface
  • Quick Output
  • High Accuracy of prediction