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.ann<-train(Species~.,
                  data=alldata,
                  method='nnet',
                  trControl=trainControl(method="LOOCV"),
                  tuneGrid=expand.grid(size=c(1:10),decay=seq(0.1,1,0.1))
                  )
names(results.ann)
plot(results.ann,lwd=2)
results.ann
##size<- results.ann$results$size
##decay<- results.ann$results$decay
##Accuracy<- results.ann$results$Accuracy
##Kappa<- results.ann$results$Kappa
##plotdat<- cbind(size,decay,Accuracy,Kappa)
##head(plotdat)
results.ann$finalModel
results.ann$bestTune
#[2]-results with best conditions----------------------------------
results.ann$bestTune[[1]]
results.ann$bestTune[[2]]
results.annBest<-train(Species~.,
                   data=alldata,
                   method='nnet',
                   trControl=trainControl(method="LOOCV"),
                   tuneGrid=expand.grid(size=results.ann$bestTune[[1]],
                                        decay=results.ann$bestTune[[2]])
)
confusionMatrix(data=predict(results.annBest,alldata),as.factor(alldata$Species))
#[3] Store results as csv file----------------------------------
predData<-predict(results.annBest,alldata)
Fdata<-data.frame(alldata[,5],predData)
colnames(Fdata)<-c("Org","NNW")
write.csv(Fdata,file="PredNNW.csv")
#[4]---- visualization of neural network
if(!require(NeuralNetTools)){
  install.packages("NeuralNetTools");library(NeuralNetTools)}
plotnet(results.annBest,circle_cex = 1,cex_val=1,pad_x=0.75)
results.annBest$finalModel