bde*_*vic 21 parallel-processing r
我喜欢的的parallel包在R和它是多么容易和直观做并行版本apply,sapply等等.
是否有类似的并行功能replicate?
Gre*_*now 28
您可以使用lapply或的并行版本sapply,而不是说复制此表达式时n,您执行应用1:n而不是给出表达式,您将该表达式包装在忽略发送给它的参数的函数中.
可能是这样的:
#create cluster
library(parallel)
cl <- makeCluster(detectCores()-1)
# get library support needed to run the code
clusterEvalQ(cl,library(MASS))
# put objects in place that might be needed for the code
myData <- data.frame(x=1:10, y=rnorm(10))
clusterExport(cl,c("myData"))
# Set a different seed on each member of the cluster (just in case)
clusterSetRNGStream(cl)
#... then parallel replicate...
parSapply(cl, 1:10000, function(i,...) { x <- rnorm(10); mean(x)/sd(x) } )
#stop the cluster
stopCluster(cl)
Run Code Online (Sandbox Code Playgroud)
作为平行等价物:
replicate(10000, {x <- rnorm(10); mean(x)/sd(x) } )
Run Code Online (Sandbox Code Playgroud)
future.apply包提供了replicate()一个并行运行的插件替代品,并使用开箱即用的统计声音并行随机数生成:
library(future.apply)
plan(multisession, workers = 4)
y <- future_replicate(100, mean(rexp(10)))
Run Code Online (Sandbox Code Playgroud)