#' Calculate the average of AUC value.
#' @param data  The training data as a data frame \code{\link{data.frame}}.
#' @param kfold an integer for the number of folds 
#' @param fmla  an object of class : a symbolic description of the model to be fitted 
#' @seealso \code{\link{formula}} for details
#' \code{\link[ROCR]{performance}} performs the evaluations of predictor. 
#' \code{\link[caret]{createFolds}} splits the data into kfold groups
#' @return An numeric.
#' @export

mean_AUC<-function(data,kfold=5,fmla){
  
  positive = data[data$Y==1, ]
  
  negative = data[data$Y==0, ] 
  
  y <-c(rep(1,nrow(positive)),rep(0,nrow(negative)))
  
  folds <- createFolds(y=y,k=kfold)
  
  auc = rep(0,kfold)
  
  for(i in 1:kfold)
  {
    fold_test <- data[folds[[i]],]  
    fold_train <- data[-folds[[i]],]  
    
    fold_pre <- glm(fmla,family=binomial(link='logit'),data=fold_train,control=list(maxit=100))
    fold_predict <- predict.glm(fold_pre,type='response',newdata=fold_test)
    
    fold_pred <- prediction(fold_predict, fold_test$Y)   
    auc[i]= as.numeric(performance(fold_pred,'auc')@y.values)       
    
  }
  
  avgAUC = mean(auc)
  
  return(avgAUC)

}
  
