I want R
to search for the seed(s)
that will simulate
`AR(2):
ar1.2 <- arima.sim(n = 100, model=list(ar=c(0.5, 0.4), order = c(2, 0, 0)), sd = 1)
Such that when I run the following:
set.seed()
ar1.2 <- arima.sim(n = 100, model=list(ar=c(0.5, 0.4), order = c(2, 0, 0)), sd = 1)
auto.arima(ar1.2)
It will confirm the AR- coefficients
as phi_1 = 0.5...
and phi_2 = 0.4...
I have two conditions to be met chronologically as follows:
- Make sure that the
ARIMA order
is (2, 0, 0)
- Out of the
seeds
that satisfied condition 1
above, search
for the one that its coefficients as 0.5...
and 0.4...
Edit
It is done for `AR(1) as follows:
FUN <- function(i) {
set.seed(i)
ar1 <- arima.sim(n=100, model=list(ar=0.4, order=c(1, 0, 0)), sd=1)
ar2 <- auto.arima(ar1, ic="aicc")
c(arimaorder(ar2), seed=i)
}
R <- 24000 ## this would be your 1e5
seedv <- 23000:R
library(parallel)
cl <- makeCluster(detectCores() - 1 + 1)
clusterExport(cl, c("FUN"), envir=environment())
clusterEvalQ(cl, suppressPackageStartupMessages(library(forecast)))
res <- parSapply(cl, seedv, "FUN")
stopCluster(cl)
seed_out <- res["seed", which(apply(res, 2, function(x) all(x[1:3] == c(1, 0, 0))))]
#########################################################################
sink("ARIMA.SIM_n20_ar0.4.txt")
##########################################################################
arima_order_results = data.frame()
for (my_seed in seed_out){
set.seed(my_seed)
ar1 <- arima.sim(n = 100, model=list(ar=0.4, 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.400") {
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)
}
}
##########################################################################
sink()
#######################################################################
Get the output in your Working Directory
#seed ar1
#1 23027 0.4009039
#seed ar1 intercept
#1 23305 0.4005298 0.4055362
#seed ar1
#1 23443 0.4004223
#seed ar1
#1 23845 0.400621
I confirm the output with a seed of 23027
set.seed(23027)
ar1 <- arima.sim(n=100, model=list(ar=0.4, order=c(1, 0, 0)), sd=1)
library(forecast)
auto.arima(ar1, ic="aicc")
A parallel and better Answer
for the case of AR(1) is here
I want such a solution for AR(2)