0

我正在尝试将这个简单的 for 循环作为并行进程运行,因为它需要大量的计算能力。有什么想法吗?

##Load files and libraries---
library(tidyverse)
library(caret)
library(insight)
library(MASS)
library(mfx)
library(furrr)

for (i in 1:16) {
      nested_inter$model[[i]]<- nb_thesis_inter(df= nested_nb$data[[i]], mdl= nested_nb$model[[i]])
      print (paste("Finished model ", i, "out of 16"))
    }

  #nested_inter<- nested_inter %>% 
  #  mutate(model= future_map2(.x= data, .y=model, .f = nb_thesis_inter))
4

2 回答 2

1

我要去的是future.apply包裹。

library(future.apply)
plan(multisession)

nested_inter$model = future_Map(nb_thesis_inter,
                                nested_nb$data,
                                nested_nb$model)

有两点需要注意。

  1. plan(multisession)允许并行使用 Windows。查看?plan所有选项。
  2. 我没有安装所有软件包,因为该示例不可重现。future_Map调用可能需要更改为future_map(function (x, y) nb_thesis_inter(df = x, mdl = y), ...)取决于 的默认参数顺序nb_thesis_inter
于 2020-10-17T10:22:32.913 回答
0

你可以使用pmap

nested_nb %>% furrr::future_pmap(function(...){
  row <- list(...)
  nb_thesis_inter(df = row$data, mdl= row$model)
})
于 2020-10-17T07:06:31.100 回答