3

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))
Yang Yang
  • 858
  • 3
  • 26
  • 49
  • 3
    Like the example of using 2 metrics in the package vignette? https://yardstick.tidymodels.org/articles/metric-types.html – camille Dec 30 '22 at 18:00

1 Answers1

5

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!

jpsmith
  • 11,023
  • 5
  • 15
  • 36
  • Hi @YangYang - see edit, I am not 100% sure how to do it elegantly, so perhaps a better coder can identify a way. In the meantime, I just wrote a function to do it which is a bit inelegant. – jpsmith Dec 30 '22 at 18:31
  • 1
    Thanks a lot. I notice that this link https://stackoverflow.com/questions/40643095/aggregating-rmse-and-r2-in-r provides a solution to data.frame. Unfortunately, it is not working for tibble. – Yang Yang Dec 30 '22 at 18:34