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"
#-----------------------------------------------------------
alldata<-read.csv(ifname,row.names=1,header=T,as.is=TRUE)
#[1]-Assessment of training data ---------------------------------------
results.nb<-train(Species~.,
                  data=alldata,
                  method='nb',
                  trControl=trainControl(method="LOOCV")
                  )
names(results.nb)
results.nb
#[2] prediction & posterior probability
pred<-predict(results.nb$finalModel,alldata)
names(pred)
head(pred$posterior)
plot(c(1:150),pred$posterior[,1],col=c(1),pch=0,xlim=c(0,150),
     xlab="Sample No.",ylab="Posterior probability",main="Posterior probability")
par(new=T)
plot(c(1:150),pred$posterior[,2],col=c(1),pch=1,xlim=c(0,150),xlab="",ylab="")
par(new=T)
plot(c(1:150),pred$posterior[,3],col=c(1),pch=2,xlim=c(0,150),xlab="",ylab="")
legend("left",legend=colnames(pred$posterior),
       col=c(1,1,1),pch=c(0,1,2))
confusionMatrix(data=predict(results.nb,alldata),as.factor(alldata$Species))
#------ store output data as file -----
predData<-predict(results.nb,alldata)
Fdata<-data.frame(alldata[,5],predData)
colnames(Fdata)<-c("Org","NBA")
write.csv(Fdata,file="PredNBA.csv")