我对R很陌生... 我试图按照Hadley和其他人的建议在rsample包中使用bootstrap函数,但是它似乎不起作用。也许有一个错误?谁能帮助解决这个问题? 在“整理自举”的帮助窗口中提供了此示例。有很多错误
> ggplot(mtcars, aes(wt, mpg)) +
+ geom_point() +
+ geom_line(aes(y = predict(nlsfit)))
>
>
> ####Bootsrapping
> library(dplyr)
> library(rsample)
> library(broom)
> library(purrr)
>
>
> library(dplyr)
> set.seed(104)
> boots <- bootstraps(mtcars, times = 100)
> boots
# Bootstrap sampling
# A tibble: 100 x 2
splits id
<list> <chr>
1 <split [32/11]> Bootstrap001
2 <split [32/12]> Bootstrap002
3 <split [32/13]> Bootstrap003
4 <split [32/10]> Bootstrap004
5 <split [32/15]> Bootstrap005
6 <split [32/9]> Bootstrap006
7 <split [32/10]> Bootstrap007
8 <split [32/12]> Bootstrap008
9 <split [32/10]> Bootstrap009
10 <split [32/14]> Bootstrap010
# … with 90 more rows
>
>
> ####Creating a helper function to model each bootstrap sample
> fit_nls_on_bootstrap <- function(split) {
+ nls(mpg ~ k / wt + b, analysis(split), start = list(k = 1, b = 0))
+ }
>
> boot_models <- boots %>%
+ mutate(model = map(splits, fit_nls_on_bootstrap),
+ coef_info = map(model, tidy))
Error in UseMethod("tidy") :
no applicable method for 'tidy' applied to an object of class "nls"
>
> boot_coefs <- boot_models %>%
+ unnest(coef_info)
Error: Can't slice a scalar
>
>
>
> #####calculated conifdence intervals
> alpha <- .05
> boot_coefs %>%
+ group_by(term) %>%
+ summarize(low = quantile(estimate, alpha / 2),
+ high = quantile(estimate, 1 - alpha / 2))
Error: Column `term` is unknown
> ```