本文介绍了在 R 中设置种子的并行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 R 代码,当我使用 arima.sim() 函数来模拟 时,它可以帮助我知道什么 seed>ARIMA(1, 0, 0) 它实际上会在 auto.arima() 时模拟 order 1, 0, 0ARIMA代码>函数用于检查.

I Have an R code that helps me to know at what seed when I use arima.sim() function to simulate ARIMA(1, 0, 0) it will actually simulate ARIMA of order 1, 0, 0 when auto.arima() function is employed for a check.

MWE

library(forecast)
SEED_vector <- 1:10
arima_order_results <- data.frame()
flag <- TRUE
i <- 1
seed_out <- c()
while(flag){ 

  set.seed(SEED_vector[i])
  ar1 <- arima.sim(n = 20, model=list(ar=0.8, order = c(1, 0, 0)), sd = 1)
  ar2 <- auto.arima(ar1, ic = "aicc")
  if(all(arimaorder(ar2)==c(1,0,0))) {

    #print(arima_order_results)
    print(paste0('arimaorder', SEED_vector[i], ' ' , 
                 paste(arimaorder(ar2), collapse=" ")))
    seed_out <- c(seed_out, SEED_vector[i])

  }

  arima_order = arimaorder(ar2)
  arima_order = t(as.data.frame(arima_order))


  arima_order_results = rbind(arima_order_results,arima_order)

  i <- i+1
  if(i == length(SEED_vector)) {

    flag <- FALSE
  }

}

我对运行时设置的种子感兴趣

I am interested in what seed will I set such that when I run

set.seed(seed_out)
ar1 <- arima.sim(n = 20, model=list(ar=0.8, order = c(1, 0, 0)), sd = 1)
auto.arima(ar1, ic = "aicc")

它会给我 (1, 0, 0) 的 arimaorder.在我的 MWE 种子是2and3`.

it will give me arimaorder of (1, 0, 0). In my MWEthe seeds are2and3`.

我想要的

我希望在 parallel processing 中使用我的 MWE,因为我实际上正在运行 1 到 100,000 个种子,这需要 3 个小时.

I want this my MWE in parallel processing because I am actually running for seeds of 1 to 100,000 and it is taking 3 hours.

我在 Windows 上运行 R

I am running R on windows

推荐答案

您可以设置一个 FUN 动作来与 parallel::parSapply 并行化.我相信 printing 不会那么容易(类似于进度条之类的东西),所以我把它排除在外.FUN()ar2 的 arima 顺序与种子连接起来,因此 parSapply 的结果将是一个很好的矩阵 res,您可以在此查看 arima 顺序和 seed 之后.

You could set up a FUNction to parallelize with parallel::parSapply. I believe the printing wouldn't work so easily (similar to progress bars and such stuff) so I leave it out. FUN() concatenates the arima order of ar2 with the seed, thus the result of parSapply will be a nice matrix res, where you may check arima order and seed afterwards.

FUN <- function(i) {
  set.seed(i)
  ar1 <- arima.sim(n=20, model=list(ar=0.8, order=c(1, 0, 0)), sd=1)
  ar2 <- auto.arima(ar1, ic="aicc")
  c(arimaorder(ar2), seed=i)
}

要并行化,请设置一个种子向量,您将在该向量上使用 parSapply 进行循环.FUN"forecast" 包需要导出到集群中.

To parallelize, set up a seed vector over which you'll loop with parSapply. "FUN" and the "forecast" package need to be exported to the clusters.

R <- 1e2  ## this would be your 1e5
seedv <- 1:R

library(parallel)
cl <- makeCluster(detectCores() - 1)
clusterExport(cl, c("FUN"), envir=environment())
clusterEvalQ(cl, suppressPackageStartupMessages(library(forecast)))

res <- parSapply(cl, seedv, "FUN")

stopCluster(cl)

在结果矩阵res中,

res
#      [,1] [,2] [,3] [,4] [,5] [,6] 
# p       2    1    1    0    2  ...
# d       0    0    0    1    0  ...
# q       0    0    0    0    0  ...
# seed    1    2    3    4    5  ...

你可以查询哪个seed"的arima顺序是c(1, 0, 0).

you may look-up for which "seed" the arima order is c(1, 0, 0).

res["seed", which(apply(res, 2, function(x) all(x[1:3] == c(1, 0, 0))))]

# [1]  2  3 11 16 17 23 24 25 28 30 33 34 42 43 45 50 51 54 59 60 63 64 66 67
# [25] 71 72 77 79 84 91 96 97

我用我的机器检查了 seedv 长度为 1e3,预计 1e5 的预计长度的执行时间为

I checked with seedv length 1e3 with my machine and would expect an execution time of <30 min for the projected length of 1e5.

seedv <- 1:1e3
system.time(parSapply(cl, seedv, "FUN"))
# user  system elapsed 
# 0.00    0.00   17.05

这篇关于在 R 中设置种子的并行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 01:30