I wish to run for loop
in parallel process
. The result I have with the for loop
R
code is good to my taste but will be applying it to a very huge data
thus, the timing of the execution is slow.
library(forecast)
library(dplyr)
arima_order_results = data.frame()
seed_out2 <- c(1, 16, 170, 178, 411, 630, 661, 1242, 1625, 1901, 1926, 1927, 1928, 2170, 2779, 3687, 4139, 4583, 4825, 4828, 4829, 4827, 5103, 5211, 5509, 5561, 5569, 5679, 6344, 6490, 6943, 6944, 6945, 6946, 6948, 6950, 6951, 6952)
for (my_seed in seed_out2){
set.seed(my_seed)
ar1 <- arima.sim(n = 100, model=list(ar = 0.8, order = c(1, 0, 0)), sd = 1)
ar2 <- auto.arima(ar1, ic = "aicc")
arr <- as.data.frame(t(ar2$coef))
if(substr(as.character(arr[1]), 1, 5) == "0.800") {
arr <- cbind(data.frame(seed=my_seed),arr)
print(arr)
arima_order_results = bind_rows(arima_order_results,arr)
# write.csv(my_seed, paste0(arr, ".csv"), row.names = FALSE)
} #else print("NOT AVAILABLE")
}
The result
# seed ar1
#1 170 0.8006368
# seed ar1
#1 411 0.8004152
# seed ar1
#1 630 0.8008459
# seed ar1
#1 661 0.8001553
# seed ar1 intercept
#1 1242 0.8000623 0.8474553
# seed ar1
#1 1625 0.8004982
# seed ar1
#1 1901 0.8007815
# seed ar1
#1 1927 0.8004587
# seed ar1
#1 2170 0.8003091
# seed ar1
#1 2779 0.8008643
#:
#:
#:
#seed ar1
#1 5679 0.800689
# seed ar1 intercept
#1 6344 0.80004 0.9800426
# seed ar1
#1 6490 0.8004093
# seed ar1
#1 6948 0.8006992
What I want
I will want a parallel process that will use up my four processors at the same time so that the job execution will be fast when I apply it to
huge data` while I have the same result.
See what I tried
library(parallel)
library(foreach)
library(forecast)
library(dplyr)
library(doSNOW)
cl <- parallel::makeCluster(detectCores(), type = "SOCK")
doSNOW::registerDoSNOW(cl)
arima_order_results = data.frame()
seed_out2 <- c(1, 16, 170, 178, 411, 630, 661, 1242, 1625, 1901, 1926, 1927, 1928, 2170, 2779, 3687, 4139, 4583, 4825, 4828, 4829, 4827, 5103, 5211, 5509, 5561, 5569, 5679, 6344, 6490, 6943, 6944, 6945, 6946, 6948, 6950, 6951, 6952)
lst_out <- foreach::foreach(my_seed = seq_along(seed_out2), .packages = c("dplyr", "forecast") ) %dopar% {
set.seed(my_seed)
ar1 <- arima.sim(n = 100, model=list(ar = 0.8, order = c(1, 0, 0)), sd = 1)
ar2 <- auto.arima(ar1, ic = "aicc")
arr <- as.data.frame(t(ar2$coef))
if(substr(as.character(arr[1]), 1, 5) == "0.800") {
arr <- cbind(data.frame(seed=my_seed),arr)
print(arr)
arima_order_results = bind_rows(arima_order_results,arr)
# write.csv(my_seed, paste0(arr, ".csv"), row.names = FALSE)
}
}
See my trial result
#>lst_out
#[[1]]
#NULL
#[[2]]
#NULL
#[[3]]
#NULL
#[[4]]
#NULL
#:
#:
#:
#[[36]]
#NULL
#[[37]]
#NULL
#[[38]]
#NULL
I am operating on windows.
Edith
I want @jay.sf answer modified in such a way that it will be contain in a function like the function I am providing below.
FUN1 <- function(n, ar, sd, arr, R, FUN2){
FUN2 <- function(i, n, ar, sd, arr) {
set.seed(i)
ar1 <- arima.sim(n=n, model=list(ar=ar, order=c(1, 0, 0)), sd=sd)
ar2 <- auto.arima(ar1, ic="aicc")
(cf <- ar2$coef)
if (length(cf) == 0) {
rep(NA, 2)
}
else if (all(grepl(c("ar1|intercept"), names(cf))) & ## using `grepl`
substr(cf["ar1"], 1, 5) %in% "arr") {
c(cf, seed=i)
}
else {
rep(NA, 2)
}
}
seedv <- 1:R
library(parallel)
cl <- makeCluster(detectCores() - 1)
clusterExport(cl, c("FUN2"), envir=environment())
clusterEvalQ(cl, suppressPackageStartupMessages(library(forecast)))
res <- parLapply(cl, seedv, "FUN2")
res1 <- res[!sapply(res, anyNA)] ## filter out NAs
stopCluster(cl)
}
FUN1(n = 10, ar = 0.8, sd = 1, arr = 0.800, R = 1000, FUN2 = FUN2)