Home > Net >  How to use %>% and calculate multiple metrics in R?
How to use %>% and calculate multiple metrics in R?

Time:12-31

I have a tibble and I am trying to calculate multiple metrics.

library(tidymodels)
price = 1:50
prediction = price * 0.9
My_tibble = tibble(price=price, prediction=prediction)

# The following code can calculate the rmse
My_tibble %>% 
  rmse(truth = price, estimate = prediction)

# Is it possible to calculate `rmse` and `rsq` at the same time?
# The following code reports an error: object '.pred' not found
My_tibble %>%
  rmse(truth = price, estimate = prediction ) %>% 
  rsq(truth = price, estimate = prediction )

To extend the question a little bit, is it possible to calculate rmse and cor at the same time?

My_tibble %>%
      rmse(truth = price, estimate = prediction)

# An error occurs: the condition has length > 1 and only the first element will be used
My_tibble %>%
      cor(x= price, y= prediction, method = "kendall")

Thanks to jpsmith, is it possible to bundle rmse and cor into a single summarise call?

# An error occurs: no applicable method for 'rmse' applied to an object of class "c('integer', 'numeric')"
My_tibble %>%
  summarize(
    COR = cor(x = price, y = prediction),
    RMSE = rmse(truth = price, estimate = prediction))

CodePudding user response:

I've done this before by specifying desired metrics in metric_set and then passing it through:

mets <- metric_set(rmse, rsq)
My_tibble %>% 
     mets(price, prediction)

#   .metric .estimator .estimate
#   <chr>   <chr>          <dbl>
# 1 rmse    standard        2.93
# 2 rsq     standard        1   

Which gives the same as:

My_tibble %>%
  rmse(truth = price, estimate = prediction)

#  .metric .estimator .estimate
#  <chr>   <chr>          <dbl>
#   1 rmse    standard        2.93

My_tibble %>%
  rsq(truth = price, estimate = prediction)

#   .metric .estimator .estimate
#   <chr>   <chr>          <dbl>
# 1 rsq     standard           1

For cor, you need to wrap it in summarize:

My_tibble %>%
  summarize(cor = cor(x = price, y = prediction))

#     cor
#   <dbl>
# 1     1

Not sure how to combine both the functions defined in mets and cor elegantly, but defining your own function can do it:

met_fun <- function(df){
  mets <- metric_set(rmse, rsq)
  a <- df %>% 
    mets(price, prediction) %>% 
    tidyr::pivot_wider(values_from = .estimate, names_from = .metric) %>%
    select(-.estimator)
  b <- df %>%
    summarize(cor = cor(x = price, y = prediction))
  
  cbind(a, b)
}

met_fun(My_tibble)

#       rmse rsq cor
# 1 2.930017   1   1

Good luck!

  • Related