0

I am learning how to treat with imbalanced data with this article: https://www.r-bloggers.com/dealing-with-unbalanced-data-in-machine-learning/

And Mutate is a simple method to replace original NA values with new values from "dplyr".

Here is the code going wrong:

    models <- list(original = model_rf,
                       under = model_rf_under,
                       over = model_rf_over,
                       smote = model_rf_smote,
                       rose = model_rf_rose)
    comparison <- data.frame(model = names(models),
                         Sensitivity = rep(NA, length(models)),
                         Specificity = rep(NA, length(models)),
                         Precision = rep(NA, length(models)),
                         Recall = rep(NA, length(models)),
                         F1 = rep(NA, length(models)))

    for (name in names(models)) {
  model <- get(paste0("cm_", name))

  comparison[comparison$model == name, ] <- filter(comparison, model == name) %>%
    mutate(Sensitivity = model$byClass["Sensitivity"],
           Specificity = model$byClass["Specificity"],
           Precision = model$byClass["Precision"],
           Recall = model$byClass["Recall"],
           F1 = model$byClass["F1"])
}

However, when I run it, I always got the error: $ operator is invalid for atomic vectors. And I double check the code and I find out the problem may come from the function "mutate". And I try to use mutate_ which works.

But I don't know why it works. I am eager to know the differences between mutate() and mutate_() Thanks!

Lin Ma
  • 1
  • 1
  • 2
    See the [programming with dplyr vignette](https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html). `mutate_` is a standard evaluation version of mutate, but it is deprecated and you should use `enquo` and `!!` to more reliably get the result you want. – Calum You Jun 18 '18 at 18:28
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 18 '18 at 18:49

1 Answers1

0

I cannot test this, because I don't have any of the data, so there may be some bugs. However, you should try this:

library(dplyr)
get_results <- function(name) {
  model <- sym(paste0("cm_", name))

  list(data.frame(
    Sensitivity = !!model$byClass["Sensitivity"],
    Specificity = !!model$byClass["Specificity"],
    Precision = !!model$byClass["Precision"],
    Recall = !!model$byClass["Recall"],
    F1 = !!model$byClass["F1"]
  ))
}

comparison <- comparison %>%
  group_by(model) %>%
  mutate(temp_obj = get_results(model))) %>%
  unnest()

This replaces the for-loop entirely.

Melissa Key
  • 4,476
  • 12
  • 21