x <- c('e1071','caret');for(i in x){if(!require(i,character.only=T)){
  install.packages(i,dependencies=T);library(i,character.only=T)}};rm(x,i)
#-----------------------------------------------------------
ifname<-"irisAll.csv"
#[1]-----------------------------------------------------------
alldata<-read.csv(ifname,row.names=1,header=T,as.is=TRUE)
#-Assessment of training data ---------------------------------------
results.knn<-train(Species~.,
                  data=alldata,
                  method='knn',
                  tuneGrid=expand.grid(k=c(1:50)),
                  trControl=trainControl(method="LOOCV")
                  #trControl=trainControl(method="boot",number=10)
                  #trControl=trainControl(method="cv",number=10)
                  #trControl=trainControl(method="repeatedcv",number=10,repeats=5)
                  )
names(results.knn)
results.knn$bestTune
plot(results.knn$results$k,results.knn$results$Accuracy,type="o",ylim=c(0.9,1.0))
bestk<-results.knn$bestTune
#[2]-------------------Best knn ----------------------------------------------------------
results.bestknn<-train(Species~.,
                   data=alldata,
                   method='knn',
                   tuneGrid=expand.grid(k=bestk),
                   trControl=trainControl(method="LOOCV")
                   #trControl=trainControl(method="boot",number=10)
                   #trControl=trainControl(method="cv",number=10)
                   #trControl=trainControl(method="repeatedcv",number=10,repeats=5)
                  )
confusionMatrix(data=predict(results.bestknn,alldata),as.factor(alldata$Species))
#[3]--- OUTPUT --------------------------
predData<-predict(results.bestknn,alldata)
Fdata<-data.frame(alldata[,5],predData)
colnames(Fdata)<-c("Org","knn")
write.csv(Fdata,file="PredKNN.csv")