I Want to set and Automate Seed as a Vector Instead of an Integer in R

旧巷老猫 提交于 2021-02-17 05:10:12

问题


Using an arima.sim() function to simulate time series data that follows a particular ARIMA model requires a lot of trials of this nature:

library(forecast)
set.seed(1)
ar1 <- arima.sim(n = 10, model=list(ar=0.2, order = c(1, 0, 0)), sd = 1)
ar2 <- auto.arima(ar1, ic ="aicc")
ar2

One needs to be changing the seed integer until the desired result is archived. I now think of instead of changing the seed integer manually and checking with auto.arima() function I should automate the seeds with a vector like this:

library(forecast)
SEED <- c(1,2,3,4,5,6,7,8,9,10)
set.seed(SEED)
ar1 <- arima.sim(n = 10, model=list(ar=0.2, order = c(1, 0, 0)), sd = 1)
ar2 <- auto.arima(ar1, ic ="aicc")
arimaorder(ar2)

such that it will print the result of the arimaorder(ar2) function of a particular seeded trial along with its seed. With that, I will be able to see the seed integer that gives me the desired arimaorder and will go for it instead of manually trying it one after the order.


回答1:


I guess this should work for your purposes. You'll get a different arima order for each seed. And you could acccess since I saved it in the dataframe" arima_order_results

library(forecast)
SEED_vector <- c(1,2,3,4,5,6,7,8,9,10)
arima_order_results = data.frame()
for (my_seed in SEED_vector){
  set.seed(my_seed)
  ar1 <- arima.sim(n = 10, model=list(ar=0.2, order = c(1, 0, 0)), sd = 1)
  ar2 <- auto.arima(ar1, ic ="aicc")
  
  arima_order = arimaorder(ar2)
  arima_order = t(as.data.frame(arima_order))
  # Print the arima order.
  print(arima_order)
  # This line of code is just if yo uwant to store the results in a dataframe
  arima_order_results = rbind(arima_order_results,arima_order)
}
# See your results (you also printed them in console)
View(arima_order_results )


来源:https://stackoverflow.com/questions/65268535/i-want-to-set-and-automate-seed-as-a-vector-instead-of-an-integer-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!