如何在 R 中使用 %>% 并计算多个指标?

Yan*_*ang 3 r dplyr tidymodels

我有一个小标题,我正在尝试计算多个指标。

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 )
Run Code Online (Sandbox Code Playgroud)

把问题延伸一点,是否可以同时计算rmse和?cor

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")
Run Code Online (Sandbox Code Playgroud)

感谢 jpsmith,是否可以将rmse和捆绑cor到一个summarise调用中?

# 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))
Run Code Online (Sandbox Code Playgroud)

jps*_*ith 5

我之前通过指定所需的指标metric_set然后将其传递来完成此操作:

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   
Run Code Online (Sandbox Code Playgroud)

这给出了相同的结果:

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

Run Code Online (Sandbox Code Playgroud)

对于cor,您需要将其包装在summarize

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

#     cor
#   <dbl>
# 1     1
Run Code Online (Sandbox Code Playgroud)

mets不确定如何优雅地组合和中定义的函数cor,但定义自己的函数可以做到这一点:

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
Run Code Online (Sandbox Code Playgroud)

祝你好运!