0

I am pretty new to R... I tried to use the bootstrap function in the rsample package as suggested by Hadley and others but it does not seem to work. Perhaps, there is a bug? Could anyone help solve this problem? This example was provided in the help window of "Tidy bootstrapping". There are a lot of errors

> 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
> ```
pogibas
  • 27,303
  • 19
  • 84
  • 117
SimRock
  • 229
  • 3
  • 10
  • I couldn't find a single error in the help-file and was able to run all of the examples without a problem. Did you try to run it in a clean session? Have you installed all the packages so that they load when you call `library()` and that you don't get any errors? For example, `tidyr` is required by the `rsample` package. – edsandorf Dec 10 '19 at 17:31
  • Thank you so much. It restart R and reinstalled the `rsample` package and it works now. I appreciate it very much – SimRock Dec 10 '19 at 19:16
  • I had a very similar issue (I think I might have a package conflict with the tidy function), I got around it by accessing Broom’s tidy function directly with broom::tidy(my_model) – CallumH Dec 10 '20 at 16:59
  • Check out my recent post right here: https://stackoverflow.com/questions/68355066/r-bootstrap-regression-with-facet-wrap the problem is the group_by(term) = term doesn't exist after the nesting. – hachiko Jul 24 '21 at 16:56

0 Answers0