问题
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")
it will give me arimaorder of (1, 0, 0). In my MWEthe seeds are2and3`.
What I want
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.
I am running R on windows
回答1:
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)
}
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)
In the resulting matrix 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 ...
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
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
来源:https://stackoverflow.com/questions/65404549/parallel-processing-for-setting-seed-in-r