Bootstrap 使用 rsample 包比使用 boot 包慢得多

Bootstrap using rsample package is much slower than using boot package

我发现像这样使用 boot

library(boot)

calculate_mean_1 <- function(.data, i){
  mean(.data$mpg[i])
}

system.time(
  samples_1 <- boot(mtcars, statistic = calculate_mean_1, R=10000)$t
)

我创建 bootstrap 样本的速度比使用 rsample 软件包快 10 倍:

library(tidyverse)
library(rsample)

calculate_mean_2 <- function(.data) {
  mean(analysis(.data)$mpg)
}

system.time(
  samples_2 <- bootstraps(mtcars, times = 10000) %>% 
    mutate(m = map_dbl(splits, calculate_mean_2))
)

我想知道我是否正确使用 rsample

看来你做得对。我认为不同之处在于 rsample 使用列表而不是标准向量。 rsample 的优点是您可以在同一个 运行 中计算多个统计信息,因此如果您这样做可能会节省一些时间(并使代码更易于阅读)。

要缩小性能差距,请考虑从数据帧的子集中抽取 bootstrap 个样本。

system.time(
  samples_3 <- bootstraps(mtcars %>% dplyr::select(mpg), times = 10000) %>% 
    mutate(m = map_dbl(splits, calculate_mean_2))
)