Title: Iris-caret-5ML-models

library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
library(kernlab)
## 
## Attaching package: 'kernlab'
## The following object is masked from 'package:ggplot2':
## 
##     alpha
library(C50)
library(klaR)
## Loading required package: MASS
library(rpart)
library(plyr)
library(MASS)

Data Partition

set.seed(3456)
attach(iris)

trainIndex <- createDataPartition(iris$Species, 
                                  p = 0.8, 
                                  list = FALSE, 
                                  times = 1)

irisTrain <- iris[ trainIndex,]
irisTest  <- iris[-trainIndex,]
Species <- factor(irisTrain$Species)

Training Parameters

control <- trainControl(## 3-fold CV
                           method = "repeatedcv",
                           number = 3,
                           ## repeated 3 times
                           repeats = 3)

Training Using caret

start_time <- Sys.time() # Start timer

# 1. KNN
set.seed(7)
modelknn <- train(Species ~ ., 
                  data=irisTrain, 
                  method="knn", 
                  trControl=control)
# 2. C50 Tree
set.seed(7)
modelc50 <- train(Species ~ ., 
                  data=irisTrain, 
                  method="C5.0Tree", 
                  trControl=control)
# 3. SVM
set.seed(7)
modelSvm <- train(Species ~ ., 
                  data=irisTrain, 
                  method="svmRadial", 
                  trControl=control)
# 4. Cubist
set.seed(7)
modellda <- train(Species ~ .,
                  data=irisTrain,
                  method="lda",
                  trControl=control)
# 5. Naive Bayes
set.seed(7)
modelnb <- train(Species ~ .,
                  data=irisTrain,
                  method="nb",
                  trControl=control)
# 6. CART
modelCart <- train(Species ~. , 
                  data=irisTrain, 
                  method="rpart", 
                  trControl=control)

end_time <- Sys.time()   # End timer
end_time - start_time    # Display time
## Time difference of 9.565613 secs
# collect resamples
results <- resamples(list(KNN=modelknn, 
                          C50=modelc50, 
                          SVMR=modelSvm, 
                          NB=modelnb, 
                          CART=modelCart, 
                          LDA=modellda))
# summarize the distributions
summary(results)
## 
## Call:
## summary.resamples(object = results)
## 
## Models: KNN, C50, SVMR, NB, CART, LDA 
## Number of resamples: 9 
## 
## Accuracy 
##           Min.   1st Qu.    Median      Mean   3rd Qu.      Max. NA's
## KNN  0.8250000 0.9487179 0.9500000 0.9472083 0.9750000 1.0000000    0
## C50  0.8717949 0.9000000 0.9250000 0.9191422 0.9500000 0.9512195    0
## SVMR 0.8974359 0.9000000 0.9230769 0.9276857 0.9500000 1.0000000    0
## NB   0.8974359 0.9250000 0.9512195 0.9472083 0.9750000 1.0000000    0
## CART 0.8461538 0.8717949 0.9024390 0.9075831 0.9250000 0.9761905    0
## LDA  0.9487179 0.9500000 0.9750000 0.9722153 0.9756098 1.0000000    0
## 
## Kappa 
##           Min.   1st Qu.    Median      Mean   3rd Qu.      Max. NA's
## KNN  0.7373358 0.9230769 0.9250936 0.9207970 0.9625117 1.0000000    0
## C50  0.8076923 0.8494826 0.8877456 0.8786620 0.9248120 0.9266547    0
## SVMR 0.8461538 0.8503274 0.8846154 0.8915599 0.9248826 1.0000000    0
## NB   0.8461538 0.8875351 0.9266547 0.9208092 0.9624413 1.0000000    0
## CART 0.7692308 0.8076923 0.8530466 0.8612653 0.8875351 0.9642857    0
## LDA  0.9230769 0.9249531 0.9625117 0.9582952 0.9633601 1.0000000    0
# boxplots of results
bwplot(results)

# dot plots of results
dotplot(results)