gpt4 book ai didi

r - 分组数据上的 yardstick::rmse 返回错误和不正确的结果

转载 作者:行者123 更新时间:2023-12-01 22:10:38 26 4
gpt4 key购买 nike

我想评估几个回归模型的性能,并使用 yardstick 包来计算 RMSE。这是一些示例数据

  model obs pred
1 A 1 1
2 B 1 2
3 C 1 3

当我运行下面的代码时

library(yardstick)
library(dplyr)
dat %>%
group_by(model) %>%
summarise(RMSE = yardstick::rmse(truth = obs, estimate = pred))

出现以下错误

Error in summarise_impl(.data, dots) : no applicable method for 'rmse' applied to an object of class "c('double', 'numeric')".

但是,当我明确提供 . 作为第一个参数时(我认为这不是必需的),我没有收到任何错误,但结果不正确。

dat %>%
group_by(model) %>%
summarise(RMSE = yardstick::rmse(., truth = obs, estimate = pred))
# A tibble: 3 x 2
model RMSE
<fctr> <dbl>
1 A 1.29
2 B 1.29
3 C 1.29

我期待以下内容

# A tibble: 3 x 2
model RMSE
<fctr> <dbl>
1 A 0
2 B 1.00
3 C 2.00

我知道这个函数有替代方法,但我仍然不明白这种行为。

数据

dat <- structure(list(model = structure(1:3, .Label = c("A", "B", "C"), class = "factor"), obs = c(1, 1, 1), pred = 1:3), .Names = c("model", "obs", "pred"), row.names = c(NA, -3L), class = "data.frame")

最佳答案

根据帮助页面 ?yardstick::rmse,它似乎需要一个数据框作为它的第一个参数,这解释了您遇到的错误。

我不太了解这个新包的速度,但该函数似乎希望跨数据框计算汇总统计数据,而不是逐行计算。要强制它逐行运行,您需要让它认为每一行都是它自己的数据框,并在每个数据框中应用该函数:

library(tidyverse)
dat %>%
group_by(model) %>%
nest() %>%
mutate(rmse_res = map(data, rmse, truth = obs, estimate = pred)) %>%
unnest(rmse_res)

# A tibble: 3 x 3
model data rmse
<fctr> <list> <dbl>
1 A <tibble [1 x 2]> 0
2 B <tibble [1 x 2]> 1.00
3 C <tibble [1 x 2]> 2.00

关于r - 分组数据上的 yardstick::rmse 返回错误和不正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48131632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com