我希望R
搜索将simulate
`AR(2(:的seed(s)
ar1.2 <- arima.sim(n = 100, model=list(ar=c(0.5, 0.4), order = c(2, 0, 0)), sd = 1)
这样当我运行以下程序时:
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)
它将确认AR- coefficients
为phi_1 = 0.5...
和phi_2 = 0.4...
按时间顺序,我有两个条件需要满足,如下所示:
- 确保
ARIMA order
是(2, 0, 0)
- 在满足上述
condition 1
的seeds
中,其系数为0.5...
和0.4...
的search
编辑
对`AR(1(进行如下操作:
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()
#######################################################################
在工作目录中获取输出
#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
我用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")
AR(1(情况下的parallel and better Answer
在这里是
我想要这样一个AR(2(的解决方案
我使用了上面问题中链接的解决方案中的方法。我也使用ar1 = 0.3
,而ar2 = 0.4
FUN2 <- function(i) {
set.seed(i)
ar1 <- arima.sim(n=20, model=list(ar=c(0.3, 0.4), order=c(2, 0, 0)), sd=1)
ar2 <- auto.arima(ar1, ic="aicc")
(cf <- ar2$coef)
if (length(cf) == 0) {
rep(NA, 2)
}
else if (all(grepl(c("ar1|ar2|intercept"), names(cf))) &
substr(cf["ar1"], 1, 3) %in% "0.3" & substr(cf["ar2"], 1, 3) %in% "0.4") {
c(cf, seed=i)
}
else {
rep(NA, 2)
}
}
R <- 1e4
seedv <- 1:R
library(parallel)
cl <- makeCluster(detectCores() - 1 + 1)
clusterExport(cl, c("FUN2"), envir=environment())
clusterEvalQ(cl, suppressPackageStartupMessages(library(forecast)))
res <- parLapply(cl, seedv, "FUN2")
(res1 <- res[!sapply(res, anyNA)])
stopCluster(cl)
res2 <- Reduce(function(...) merge(..., all=T), lapply(res1, function(x)
as.data.frame(t(x))))
res2[order(res2$seed), ]