如何在按行操作后保留对象 class 以使 int_pctl 之类的函数起作用?
How to retain object class after rowwise operations to make functions like int_pctl work?
我试图将 purrr::map 替换为 https://www.tidymodels.org/learn/statistics/ 中主题的行向函数。我能够在前 2 个中做到这一点,但在 bootstrap 主题上,它在 int_pctl 函数处中断,因为它希望数据成为 rset 对象。
这是我的代码:
library(tidyverse); library(tidymodels); library(rsample)
boots <- bootstraps(mtcars, times = 2000, apparent = T)
fit_nls_on_bootstrap <- function(split) {
nls(mpg ~ k / wt + b, analysis(split), start = list(k = 1, b = 0))
}
boot_models <-
boots %>%
rowwise() %>%
mutate(model = list(fit_nls_on_bootstrap(splits)),
coef_info = list(tidy(model))) %>%
ungroup()
boot_coefs <-
boot_models %>%
unnest(coef_info)
percentile_intervals <- int_pctl(boot_models, coef_info)
percentile_intervals
当我与该网页上的 purrr::map 代码进行比较时,它正确地生成了 boot_models 对象的 rset class,但是我的 rowwise 尝试没有呈现 rset class?
如何在计算过程中保留 rset class?或者,一般来说,假设我想使用 rowwise 函数,如何获得 int_pctl 值?
我们可以在调用 int_pctl
之前用 class(boot_models) <- c("bootstraps", "rset", class(boot_models))
恢复 class
class(boot_models) <- c("bootstraps", "rset", class(boot_models))
percentile_intervals <- int_pctl(boot_models, coef_info)
我试图将 purrr::map 替换为 https://www.tidymodels.org/learn/statistics/ 中主题的行向函数。我能够在前 2 个中做到这一点,但在 bootstrap 主题上,它在 int_pctl 函数处中断,因为它希望数据成为 rset 对象。
这是我的代码:
library(tidyverse); library(tidymodels); library(rsample)
boots <- bootstraps(mtcars, times = 2000, apparent = T)
fit_nls_on_bootstrap <- function(split) {
nls(mpg ~ k / wt + b, analysis(split), start = list(k = 1, b = 0))
}
boot_models <-
boots %>%
rowwise() %>%
mutate(model = list(fit_nls_on_bootstrap(splits)),
coef_info = list(tidy(model))) %>%
ungroup()
boot_coefs <-
boot_models %>%
unnest(coef_info)
percentile_intervals <- int_pctl(boot_models, coef_info)
percentile_intervals
当我与该网页上的 purrr::map 代码进行比较时,它正确地生成了 boot_models 对象的 rset class,但是我的 rowwise 尝试没有呈现 rset class?
如何在计算过程中保留 rset class?或者,一般来说,假设我想使用 rowwise 函数,如何获得 int_pctl 值?
我们可以在调用 int_pctl
class(boot_models) <- c("bootstraps", "rset", class(boot_models))
恢复 class
class(boot_models) <- c("bootstraps", "rset", class(boot_models))
percentile_intervals <- int_pctl(boot_models, coef_info)