Home > database >  How to aggregate rmse and cor in summarise() from tidymodels in R?
How to aggregate rmse and cor in summarise() from tidymodels in R?

Time:12-31

I have a tibble and I'd like to calculate both rmse and cor in summarise().

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

The following code works well.

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

However, the following code reports an error: no applicable method for 'rmse' applied to an object of class "c('integer', 'numeric')". How can we fix it? Thanks.

My_tibble %>%
  summarise(
    COR = cor(x = price, y = prediction),
    RMSE = rmse(truth = price, estimate = prediction))

CodePudding user response:

The rmse first argument is data, which we get by default by using %>% in the first case as the lhs is the whole data My_tibble, whereas in summarise, it is not the case, either use cur_data() (getting deprecated) or with pick

library(dplyr)
library(tidyr)
 My_tibble %>%
   summarise(
     COR = cor(x = price, y = prediction),
     RMSE = rmse(data = pick(everything()), 
     truth = price, estimate = prediction)) %>%  
  unnest_wider(where(is_tibble))

-output

# A tibble: 1 × 4
    COR .metric .estimator .estimate
  <dbl> <chr>   <chr>          <dbl>
1     1 rmse    standard        2.93

Or with cur_data()

My_tibble %>%
  summarise(
    COR = cor(x = price, y = prediction),
    RMSE = rmse(data = cur_data(),
     truth = price, estimate = prediction)) %>% 
    unnest_wider(where(is_tibble))

-output

# A tibble: 1 × 4
    COR .metric .estimator .estimate
  <dbl> <chr>   <chr>          <dbl>
1     1 rmse    standard        2.93
  • Related