Read the dataset

Providing the detailed description of the glass set

hair, feathers, eggs, milk, airborne, aquatic, predator, toothed, backbone,

breathes, venomous, fins, legs, tail, domestic, catsize, type

#install.packages("pROC")
#install.packages("mlbench")
#install.packages("mlbench")

library(caret)
## Warning: package 'caret' was built under R version 3.5.1
## Loading required package: lattice
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.5.1
library(pROC)
## Warning: package 'pROC' was built under R version 3.5.1
## Type 'citation("pROC")' for a citation.
## 
## Attaching package: 'pROC'
## The following objects are masked from 'package:stats':
## 
##     cov, smooth, var
library(mlbench)
## Warning: package 'mlbench' was built under R version 3.5.1
library(lattice)

# Example 1 - Glass dataset (Classification)

zoo <- read.csv(file.choose())
#glass$Type[glass$Type==1] <- 'Type1'
# glass$Type[glass$Type==2] <- 'Type2'
# glass$Type[glass$Type==3] <- 'Type3'
# glass$Type[glass$Type==4] <- 'Type4'
# glass$Type[glass$Type==5] <- 'Type5'
# glass$Type[glass$Type==6] <- 'Type6'
# glass$Type[glass$Type==7] <- 'Type7'
str(zoo)
## 'data.frame':    101 obs. of  18 variables:
##  $ animal.name: Factor w/ 100 levels "aardvark","antelope",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ hair       : int  1 1 0 1 1 1 1 0 0 1 ...
##  $ feathers   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ eggs       : int  0 0 1 0 0 0 0 1 1 0 ...
##  $ milk       : int  1 1 0 1 1 1 1 0 0 1 ...
##  $ airborne   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ aquatic    : int  0 0 1 0 0 0 0 1 1 0 ...
##  $ predator   : int  1 0 1 1 1 0 0 0 1 0 ...
##  $ toothed    : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ backbone   : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ breathes   : int  1 1 0 1 1 1 1 0 0 1 ...
##  $ venomous   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ fins       : int  0 0 1 0 0 0 0 1 1 0 ...
##  $ legs       : int  4 4 0 4 4 4 4 0 0 4 ...
##  $ tail       : int  0 1 1 0 1 1 1 1 1 0 ...
##  $ domestic   : int  0 0 0 0 0 0 1 1 0 1 ...
##  $ catsize    : int  1 1 0 1 1 1 1 0 0 0 ...
##  $ type       : int  1 1 4 1 1 1 1 4 4 1 ...
zoo1 <- zoo[,2:18]
str(zoo1)
## 'data.frame':    101 obs. of  17 variables:
##  $ hair    : int  1 1 0 1 1 1 1 0 0 1 ...
##  $ feathers: int  0 0 0 0 0 0 0 0 0 0 ...
##  $ eggs    : int  0 0 1 0 0 0 0 1 1 0 ...
##  $ milk    : int  1 1 0 1 1 1 1 0 0 1 ...
##  $ airborne: int  0 0 0 0 0 0 0 0 0 0 ...
##  $ aquatic : int  0 0 1 0 0 0 0 1 1 0 ...
##  $ predator: int  1 0 1 1 1 0 0 0 1 0 ...
##  $ toothed : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ backbone: int  1 1 1 1 1 1 1 1 1 1 ...
##  $ breathes: int  1 1 0 1 1 1 1 0 0 1 ...
##  $ venomous: int  0 0 0 0 0 0 0 0 0 0 ...
##  $ fins    : int  0 0 1 0 0 0 0 1 1 0 ...
##  $ legs    : int  4 4 0 4 4 4 4 0 0 4 ...
##  $ tail    : int  0 1 1 0 1 1 1 1 1 0 ...
##  $ domestic: int  0 0 0 0 0 0 1 1 0 1 ...
##  $ catsize : int  1 1 0 1 1 1 1 0 0 0 ...
##  $ type    : int  1 1 4 1 1 1 1 4 4 1 ...
zoo1$hair <- as.factor(zoo1$hair)
zoo1$feathers <- as.factor(zoo1$feathers)
zoo1$eggs <- as.factor(zoo1$eggs)
zoo1$milk <- as.factor(zoo1$milk)
zoo1$airborne <- as.factor(zoo1$airborne)
zoo1$aquatic <- as.factor(zoo1$aquatic)
zoo1$predator <- as.factor(zoo1$predator)
zoo1$toothed <- as.factor(zoo1$toothed)
zoo1$backbone <- as.factor(zoo1$backbone)
zoo1$breathes <- as.factor(zoo1$breathes)
zoo1$venomous <- as.factor(zoo1$venomous)
zoo1$fins <- as.factor(zoo1$fins)
zoo1$legs <- as.factor(zoo1$legs)
zoo1$tail <- as.factor(zoo1$tail)
zoo1$domestic <- as.factor(zoo1$domestic)
zoo1$catsize <- as.factor(zoo1$catsize)
zoo1$type <- as.factor(zoo1$type)



# Data partition
set.seed(123)
ind <- sample(2,nrow(zoo1), replace = T, prob = c(0.7,0.3))
train <- zoo1[ind==1,]
test <- zoo1[ind==2,]

# KNN Model 

trcontrol <- trainControl(method = "repeatedcv", number = 10,repeats = 3
                           # classprobs are needed when u want to select ROC for optimal K Value
                          )
set.seed(222)
fit <- train(type ~., data = train, method = 'knn', tuneLength = 20,
              trControl = trcontrol, preProc = c("center","scale"))
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs5
## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8

## Warning in preProcess.default(thresh = 0.95, k = 5, freqCut = 19, uniqueCut
## = 10, : These variables have zero variances: legs8
    # default metric is accuracy but if u want to use ROC, then mention the same
# Model Performance :
fit # the optimum value for k should be 7
## k-Nearest Neighbors 
## 
## 72 samples
## 16 predictors
##  7 classes: '1', '2', '3', '4', '5', '6', '7' 
## 
## Pre-processing: centered (20), scaled (20) 
## Resampling: Cross-Validated (10 fold, repeated 3 times) 
## Summary of sample sizes: 65, 63, 66, 64, 65, 65, ... 
## Resampling results across tuning parameters:
## 
##   k   Accuracy   Kappa    
##    5  0.9040741  0.8751166
##    7  0.9062963  0.8773174
##    9  0.8635847  0.8214341
##   11  0.8599339  0.8144032
##   13  0.8464815  0.7974134
##   15  0.7956878  0.7305769
##   17  0.7781349  0.7089185
##   19  0.7551852  0.6774064
##   21  0.7340873  0.6471112
##   23  0.7158995  0.6229688
##   25  0.7164947  0.6235111
##   27  0.7069709  0.6072264
##   29  0.6680556  0.5490114
##   31  0.6216931  0.4701934
##   33  0.5720899  0.3900492
##   35  0.5485847  0.3462768
##   37  0.5234524  0.3002657
##   39  0.4913492  0.2441984
##   41  0.4620503  0.1877586
##   43  0.4303042  0.1312393
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was k = 7.
plot(fit)

varImp(fit)
## ROC curve variable importance
## 
##   variables are sorted by maximum importance across the classes
##               X1     X2      X3      X4      X5      X6      X7
## milk     100.000 100.00 100.000 100.000 100.000 100.000 100.000
## breathes  20.000 100.00   0.000   0.000  75.000  20.000 100.000
## feathers 100.000 100.00 100.000 100.000 100.000 100.000   0.000
## legs      70.370  96.30  70.370 100.000  70.370  70.370  96.296
## backbone   0.000   0.00   0.000 100.000 100.000   0.000   0.000
## hair     100.000 100.00 100.000 100.000 100.000 100.000 100.000
## eggs      96.296  96.30  96.296  96.296  96.296  96.296  96.296
## toothed   96.296  96.30  96.296  96.296  96.296  96.296  16.296
## fins       7.407  92.59   7.407   7.407   7.407   7.407  92.593
## aquatic    8.262  85.19  85.185  14.815  47.685   8.262  85.185
## tail      18.519  18.52  81.481  81.481  81.481  18.519  18.519
## airborne  69.516  69.52  69.516  75.926  69.516  76.923   7.407
## catsize   54.074  51.00  74.074  74.074  61.574  50.997  54.074
## venomous  40.000   0.00  50.000  33.333  12.500  40.000  40.000
## predator  31.852  24.58   5.698  31.481  26.852  26.154  31.852
## domestic  18.519  18.52  18.519  10.826  18.519  10.826  18.519
pred <- predict(fit, newdata = test )
confusionMatrix(pred, test$type)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  1  2  3  4  5  6  7
##          1 12  0  0  0  0  0  0
##          2  0  7  0  0  0  0  0
##          3  0  0  0  0  0  0  0
##          4  2  0  0  2  2  0  0
##          5  0  0  0  0  0  0  0
##          6  0  0  0  0  0  2  1
##          7  0  0  0  0  0  0  1
## 
## Overall Statistics
##                                           
##                Accuracy : 0.8276          
##                  95% CI : (0.6423, 0.9415)
##     No Information Rate : 0.4828          
##     P-Value [Acc > NIR] : 0.0001373       
##                                           
##                   Kappa : 0.7599          
##  Mcnemar's Test P-Value : NA              
## 
## Statistics by Class:
## 
##                      Class: 1 Class: 2 Class: 3 Class: 4 Class: 5 Class: 6
## Sensitivity            0.8571   1.0000       NA  1.00000  0.00000  1.00000
## Specificity            1.0000   1.0000        1  0.85185  1.00000  0.96296
## Pos Pred Value         1.0000   1.0000       NA  0.33333      NaN  0.66667
## Neg Pred Value         0.8824   1.0000       NA  1.00000  0.93103  1.00000
## Prevalence             0.4828   0.2414        0  0.06897  0.06897  0.06897
## Detection Rate         0.4138   0.2414        0  0.06897  0.00000  0.06897
## Detection Prevalence   0.4138   0.2414        0  0.20690  0.00000  0.10345
## Balanced Accuracy      0.9286   1.0000       NA  0.92593  0.50000  0.98148
##                      Class: 7
## Sensitivity           0.50000
## Specificity           1.00000
## Pos Pred Value        1.00000
## Neg Pred Value        0.96429
## Prevalence            0.06897
## Detection Rate        0.03448
## Detection Prevalence  0.03448
## Balanced Accuracy     0.75000
# 89.66 % is accuracy