DavisVaughan/furrr

future_map does not work with output from modelr::permute

Closed this issue · 2 comments

I am trying to replace purrr::map with furrr:future_map to run linear regressions on permuted data obtained from modelr::permute but I get the following error:

Error in as.data.frame.default(data) : cannot coerce class ‘"permutation"’ to a data.frame

I have read through the furrr page but I can't figure out if the gotchas they have described apply to my problem here: https://furrr.futureverse.org/articles/gotchas.html

Here's my code:

library(plyr); library(dplyr); library(modelr); library(purrr); library(furrr)

specify regression formula

reg_form <- 'mpg ~ cyl + disp + hp + wt'

plan(multisession, workers = 4)

Random permutations of 'hp'

perm_data <- permute(mtcars, 5000, 'hp')

permuted linear regressions with map

perm_models <- map(perm_data$perm, ~ lm(reg_form, data = .))

permuted linear regressions with future_map

perm_models_parallel <- future_map(perm_data$perm, ~ lm(reg_form, data = .))

furrr doesn't have quite enough information to know that the modelr package needs to be loaded on the worker so that the modelr:::as.data.frame.permutations() method is registered. So you have to tell it to do that manually. This should work

library(dplyr)
library(modelr)
library(furrr)
#> Loading required package: future

plan(multisession, workers = 4)

reg_form <- 'mpg ~ cyl + disp + hp + wt'

perm_data <- permute(mtcars, 5000, 'hp')

perm_models_parallel <- future_map(
  perm_data$perm, 
  ~ lm(reg_form, data = .), 
  .options = furrr_options(packages = "modelr")
)

Created on 2022-11-29 with reprex v2.0.2.9000

Thank you for responding - that works fine now!