R - Parallelizing multiple model learning (with dplyr and purrr)

删除回忆录丶 提交于 2019-12-03 17:02:20

问题


This is a follow up to a previous question about learning multiple models.

The use case is that I have multiple observations for each subject, and I want to train a model for each of them. See Hadley's excellent presentation on how to do this.

In short, this is possible to do using dplyr and purrr like so:

library(purrr)
library(dplyr)
library(fitdistrplus)
dt %>% 
    split(dt$subject_id) %>%
    map( ~ fitdist(.$observation, "norm")) 

So since the model building is an embarrassingly parallel task, I was wondering if dplyr, purrr have an easy to use parallelization mechanism for such tasks (like a parallel map).

If these libraries don't provide easy parallelization could it be done using the classic R parallelization libraries (parallel, foreach etc)?


回答1:


There is the furrr package now, for example something like:

library(dplyr)
library(furrr)
plan(multiprocess)

dt %>% 
    split(dt$subject_id) %>%
    future_map(~fitdist(.$observation, "norm"))



回答2:


Just adding an answer for completeness here, you will need to install multidplyr from Hadley's repo to run this, more info in the vignette:

library(dplyr)
library(multidplyr)
library(purrr)

cluster <- create_cluster(4)
set_default_cluster(cluster)
cluster_library(cluster, "fitdistrplus")

# dt is a dataframe, subject_id identifies observations from each subject
by_subject <- partition(dt, subject_id)

fits <- by_subject %>% 
    do(fit = fitdist(.$observation, "norm")))

collected_fits <- collect(fits)$fit
collected_summaries <- collected_fits %>% map(summary)


来源:https://stackoverflow.com/questions/38252013/r-parallelizing-multiple-model-learning-with-dplyr-and-purrr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!