ben519 / mltools

Exploratory and diagnostic machine learning tools for R
Other
72 stars 26 forks source link

ROC and table in R #14

Closed fatimamb closed 6 years ago

fatimamb commented 6 years ago

I'm used 10 cross-validations with naive Bayes as shown in my code. I need to plot ROC, because of that I added (type=raw) to predict function to be as probabilities but I had an error with table function and ROC plot. How can deal with this error?

The error of ROC:

Error in roc.curve(rfe_nB_test_folds[, 7], rfe_nB_predict) : 
  Response and predicted must have the same length. 

The error of table:

 Error in table(rfe_nB_test_folds[, 7], rfe_nB_predict) : 
  all arguments must have the same length 

My code:

library(ROSE)#for ROC and AUC
library(caret)#for predict and confusionMatrix
library(e1071)#for naiveBayes
library(caTools)#for createFolds
set.seed(100)
rfe_nB_folds<-createFolds(BC_bind$outcome, k=10) #create folds 
rfe_nB_fun <- lapply (rfe_nB_folds, function(x){
  rfe_nB_traing_folds<-BC_bind[-x,]
  rfe_nB_test_folds<-BC_bind[x,]
  #build the model 
  rfe_nB_model<-naiveBayes(outcome ~ ., data = rfe_nB_traing_folds)
  #test the  model
  rfe_nB_predict<-predict(rfe_nB_model, rfe_nB_test_folds[-7],type="raw")
 #ROC
  CR<-roc.curve(rfe_nB_test_folds[,7],rfe_nB_predict)
  print(CR)
  rfe_nB_table<-table(rfe_nB_test_folds[,7],rfe_nB_predict)
  rfe_nB_confusionMatrix<-confusionMatrix(rfe_nB_table) 
    return (rfe_nB_confusionMatrix$table)
})
rfe_nB_sum_matrices <-Reduce('+', rfe_nB_fun)/10 #mean of 10 matrices
rfe_nB_final_confusionMatrix<-confusionMatrix(rfe_nB_sum_matrices, positive ="R")

best regards, fatima

ben519 commented 6 years ago

Where does BC_bind come from? I'm getting the error object 'BC_bind' not found

fatimamb commented 6 years ago

BC_bind is the dataset

ben519 commented 6 years ago

Please provide an example with the error that I can reproduce on my laptop. Thanks

fatimamb commented 6 years ago

https://www.dropbox.com/s/fsz07fkme5nlz4s/WPDC.xlsx?dl=0

here is the dataset

fatimamb commented 6 years ago

I'm used specific columns so I saved them on BC_bind as shown in the code.

Top_6featurs <- wpdc[,c(33,11,10,32,29,12)] #column number of top 6 featur
BC_bind <- data.frame(cbind(Top_6featurs , wpdc$outcome))
ben519 commented 6 years ago

Oops! I though you had a qestion regarding mltools. This type of question would be better posted on StackOverflow. Nonetheless, try working with this.

rfe_nB_folds<-createFolds(BC_bind$outcome, k=10) #create folds 
rfe_nB_fun <- lapply (rfe_nB_folds, function(x){

  rfe_nB_traing_folds<-BC_bind[-x,]
  rfe_nB_test_folds<-BC_bind[x,]

  #build the model 
  rfe_nB_model<-naiveBayes(outcome ~ ., data = rfe_nB_traing_folds)

  #test the  model
  rfe_nB_predict<-predict(rfe_nB_model, rfe_nB_test_folds, type="raw")
  predClasses <- c("N", "R")[rfe_nB_predict[, "N"] < 0.5 + 1]

  #ROC
  CR<-roc.curve(rfe_nB_test_folds[["outcome"]] == "N", rfe_nB_predict[, "N"])
  print(CR)

  rfe_nB_table<-table(rfe_nB_test_folds[["outcome"]], predClasses)
  rfe_nB_confusionMatrix<-confusionMatrix(rfe_nB_table) 

  return (rfe_nB_confusionMatrix$table)
})
fatimamb commented 6 years ago

I will try it, thank you for your time