我想设置和自动将种子设置为向量,而不是R中的整数 [英] I Want to set and Automate Seed as a Vector Instead of an Integer in R

查看:73
本文介绍了我想设置和自动将种子设置为向量,而不是R中的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 arima.sim()函数模拟遵循特定 ARIMA 模型的时间序列数据需要进行大量此类试验:

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

需要更改种子整数,直到将所需结果存档为止.我现在想到的不是代替手动更改种子整数并使用 auto.arima()函数进行检查,我应该使用如下矢量将种子自动化:

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)

,以便它将打印特定种子试验的 arimaorder(ar2)函数的结果及其种子.这样,我将能够看到为我提供所需的 arimaorder 的种子整数,并将其用于替代在订购后手动尝试的种子整数.

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.

推荐答案

我想这应该对您有用.您将为每个种子获得不同的Arima顺序.由于我将其保存在数据框"arima_order_results

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 )

这篇关于我想设置和自动将种子设置为向量,而不是R中的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆