Advertisement
Nenkham

pipeline R

May 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.17 KB | None | 0 0
  1. autoDtree <- function(tree) {
  2.   #user input
  3.   ms <- readline(prompt="minsplit: ")
  4.   c <- readline(prompt="C-value: ")
  5.   md <- readline(prompt="maxdepth: ")
  6.   #transform
  7.   ms <- as.integer(ms)
  8.   c <- as.numeric(c)
  9.   md <- as.integer(md)
  10.   #decision tree
  11.   tree <- rpart(form, TREINO, control = rpart.control(minsplit = (ms),  cp = (c), maxdepth = (md)))
  12.   ptree <- predict(tree, VALID)
  13.   #ROC(AUC)
  14.   roctree <- roc.curve(scores.class0 = ptree, weights.class0 = label_valid_X[,label], curve=TRUE)
  15.   #Confusion Matrix
  16.   rptree <- round(ptree)
  17.   cm <- confusionMatrix(as.factor(rptree), as.factor(VALID[,'comprou_X']))
  18.  
  19.   #METRICS (precision e recall)
  20.     metrics <- function(cm) {
  21.    
  22.       tp <- cm['table']$table[4]
  23.       tn <- cm['table']$table[1]
  24.       fp <- cm['table']$table[2]
  25.       fn <- cm['table']$table[3]
  26.    
  27.       precision <- tp / (tp + fp)
  28.       recall <- tp / (tp + fn)
  29.    
  30.       values <- c(precision, recall)
  31.    
  32.       return(values)
  33.    
  34.     }
  35.   #print metrics
  36.   metrics(cm)
  37.  
  38.   #print decision tree
  39.   par(mar = rep(2, 4))
  40.   tplot <- rpart.plot(tree)
  41.  
  42.  
  43.   par(mar = rep(2, 4))
  44.   troc <- plot(roctree)
  45.  
  46.  
  47. }
  48.  
  49.  
  50. autoDtree(tree)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement