jeudi 12 mars 2020

paste function does not work properly in ifelse statement

I wrote a function but paste0() does not work with if else().

mod <- function(method = "rf", train, test, eval, weights = NULL, sampling = NULL){

  model <- train(Target ~ ., data = train, 
                 method = method,
                 verbose = FALSE,
                 metric = "ROC",
                 weights = weights,
                 preProcess = c("scale", "center"),
                 trControl = trainControl(method = "repeatedcv",
                                          number = 10,
                                          repeats = 5,
                                          summaryFunction = twoClassSummary,
                                          classProbs = TRUE,
                                          sampling = sampling))

  test_roc <- function(model, test , eval) {roc(eval$Target, predict(model, test, type = "prob")[, "Yes"])}
  test_cm <- function(model, test , eval) {confusionMatrix(predict(model, test),eval$Target, positive = "Yes")}


  roc <- test_roc(model, test, eval)
  cm <- test_cm(model, test, eval)

  df <- data.frame(AUC = c(round(roc$auc, 3)), precision = c(round(cm$byClass["Precision"], 3)), Accuracy = c(round(cm$overall["Accuracy"],3)), row.names = paste0(method, ifelse(is.null(weights), "", "_weights"), ifelse(is.null(sampling), "", ("_"&sampling))))

  return(df)
}

And I run this:

mod(method = "gbm", train, test, eval, weights = weights, sampling = "down")

It showed:

              AUC precision Accuracy
gbm_weights 0.672     0.095    0.642

I want the row name to be gbm_weights_down or gbm_weights where no sampling or gbm without weights and sampling.

It did not paste down sampling. Where should I modify...

Aucun commentaire:

Enregistrer un commentaire