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); head(alldata)
#[2]-Assessment of training data ---------------------------------------
results.lda<-train(Species~.,
                  data=alldata,
                  method='lda',
                  #trControl=trainControl(method="boot",number=10)
                  #trControl=trainControl(method="cv",number=10)
                  #trControl=trainControl(method="repeatedcv",number=10,repeats=5)
                  trControl=trainControl(method="LOOCV")
                  )
names(results.lda)
results.lda$results
confusionMatrix(data=predict(results.lda,alldata),as.factor(alldata$Species))
results.lda$finalModel
#apply(results.lda$finalModel$means %*% results.lda$finalModel$scaling,2,mean)
#[3]--2D plot ----------------------------------------------
coefLDA<-as.matrix(results.lda$finalModel[[4]])
dataMatrix<-as.matrix(alldata[,1:4])
dataLDA<-dataMatrix%*%coefLDA
dataLDAfinal<-cbind(alldata[,5],dataLDA)
irislabel<-dataLDAfinal[,1]
plot(dataLDA[,1],dataLDA[,2],pch=c(15,1,16)[unclass(as.factor(irislabel))],
     col=c(1,1,1),xlab="LD1",ylab="LD2",main="LD1 score vs. LD2 score")
labeldata<-unique(irislabel)
legend("top",legend=labeldata,col=c(1,1,1),pch=c(15,1,16),lty=0,ncol=1,cex=1)
#[4]- We can obtain results.lda$ pred in only LOOCV ---------
pred<-results.lda$pred[,1]
obs<-results.lda$pred[,2]
TFdata<-(pred==obs)
PredDataAll<-data.frame(obs,pred,TFdata)
##PredDataAllTRUE <- PredDataAll[TFdata,]
PredDataAllFALSE <- PredDataAll[!TFdata,]
#[5]store final results----------------
Fdata<-data.frame(obs,pred)
colnames(Fdata)<-c("Org","LDA")
write.csv(Fdata,file="PredLDA.csv")