Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tidy function cannot be used within future_map?

I have R code below. for the last row, when I used map() function, it worked well. however, when I changed to future_map() function, I got the following error message:

"Error: Problem with mutate() column model. i model = future_map(splits, fun1). x no applicable method for 'tidy' applied to an object of class "c('lmerMod', 'merMod')""

any idea on what's wrong? thanks.

fun1 <- function(data) {
    data %>% analysis %>%
       lmer(val ~ period + (1 | id), data = .) %>% tidy
 }
 
 plan(multisession)
 
 raw %>%
    nest(data = -c(analyte, var)) %>%
    mutate(boot = future_map(data, ~ bootstraps(
       data = .x,
       times = 5,
       strata = id
    ),
    .progress = T)) %>%
    unnest(boot) %>%
    mutate(model =future_map(splits, fun1)) 
like image 579
YanLi Avatar asked Jun 21 '26 21:06

YanLi


1 Answers

I experienced exactly the same problem with one of my scripts. In order to get future_map to work properly with tidy, I needed to explicitly reference the broom package (i.e. I needed to use broom::tidy in place of tidy). In your example, you are attempting to extract summary statistics from a mixed model, so the code should run without error if we modify fun1 to be as follows:

fun1 <- function(data) {
data %>% analysis %>%
   lmer(val ~ period + (1 | id), data = .) %>% broom.mixed::tidy
}

UPDATE (13-Dec-2021):

After a bit more reading, I now understand that the problem, as described in the original post, is due to the broom.mixed package not being attached in the R environment(s) where the future is evaluated. Instead of modifying fun1 (which is a very hacky way of resolving the problem), we should make use of the .options argument of future_map to guarantee that broom.mixed is attached (and all associated functions are available) in the future environments. The following code should run without error:

fun1 <- function(data) {
          data %>%
          analysis %>%
          lmer(val ~ period + (1 | id), data = .) %>%
          tidy
        }

plan(multisession)

raw %>%
  nest(data = -c(analyte, var)) %>%
  mutate(boot = future_map(data, ~ bootstraps(data = .x,
                                              times = 5,
                                              strata = id),
                           .progress = T)) %>%
  unnest(boot) %>%
  mutate(model = future_map(splits,
                            fun1,
                            .options = furrr_options(packages = "broom.mixed")))

My take-home from this is that it's probably good practice to always list the packages that we need to use (as a character vector) using the .options argument of future_map, just to be on the safe side. I hope this helps!

like image 74
homgran Avatar answered Jun 23 '26 19:06

homgran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!