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

查看:15
本文介绍了我想在 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天全站免登陆