#' Draw the ROC curve.
#' @param newdata  The training data as a data frame
#' @param kfold  an integer for the number of folds
#' @param K  the number of the negatives   
#' @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 k groups
#' @export

drawAUC<-function(newdata,kfold,K,flm){

  positive = newdata[newdata$Y==1, ]
  

y <-c(rep(1,nrow(positive)),rep(0,K))
folds <- createFolds(y=y,k=kfold)

TPR = matrix(0,nrow = kfold,ncol = 100000)
FPR = matrix(0,nrow = kfold,ncol = 100000)

for(i in 1:kfold){
  
  fold_test <- newdata[folds[[i]],]   
  fold_train <- newdata[-folds[[i]],]   
  
  fold_pre <- glm(fmla,family=binomial(link='logit'),data=fold_train,maxit = 100)
  
  fold_predict <- predict.glm(fold_pre,type='response',newdata=fold_test)
  
  for(j in 1:100000)
  { 
    p0 = j/100000;
    ypred<-1*(fold_predict > p0)  
    TPR[i,j]=sum(ypred*fold_test$Y)/sum(fold_test$Y)  
    FPR[i,j]=sum(ypred*(1-fold_test$Y))/sum(1-fold_test$Y)
  }
  
}

plot(colMeans(FPR),colMeans(TPR),type="l",col=2,xlab="1-Specificity",ylab="Sensitivity",
     lty=1, lwd=2, cex.lab=1.5, cex.axis=1.5, cex.main=2, main="ROC plot",
     xlim =c(0,1))
points(c(0,1),c(0,1),type="l",lty=2)

}