Search code examples
rtidyversepurrrtidymodels

How to retain object class after rowwise operations to make functions like int_pctl work?


I was trying to replace purrr::map with the rowwise function on the topics at https://www.tidymodels.org/learn/statistics/. I was able to do that on the first 2, but on the bootstrap topic, it breaks at int_pctl function as it expects to be the data to be rset object.

Here is my code:

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

When I compared with the purrr::map code on that webpage, it correctly produces the rset class of the boot_models object, but my rowwise attempt does not render the rset class?

How can I retain the rset class during this calculation? Or, in general, how can I get the int_pctl values assuming I want to use the rowwise function?


Solution

  • We may restore the class with class(boot_models) <- c("bootstraps", "rset", class(boot_models)) prior to calling int_pctl

    class(boot_models) <- c("bootstraps", "rset", class(boot_models))
    percentile_intervals <- int_pctl(boot_models, coef_info)