模拟时间序列 [英] Simulate a time series
问题描述
在最近的宏观经济预测在线课程中,有一个建模练习
In a recent online course on macroeconomic forecasting, there was an excercise to model
y(t) = 3.0 + 0.55 y(t-1) + e(t)
其中 e(t)
定义为
et <- c(-1.2138662, -0.2854597, 0.5902700, 0.8285463, -0.9954260, -0.3716332)
现在我尝试在 R 中执行此操作(课程使用 EViews),但是我没有找到那里给出的解决方案:第 5 个元素为 5.648.我试过了(类似于这个 blogpost):
Now I tried to do this in R (the course used EViews), however I do not come to the solution given there: 5.648 for the 5th element. I tried (similar to this blogpost):
y <- rep(NA,6)
y[1] <- 0
y[2] <- 3 + 0.55*y[1]+et[1]
y[3] <- 3 + 0.55*y[2]+et[2]
y[4] <- 3 + 0.55*y[3]+et[3]
y[5] <- 3 + 0.55*y[4]+et[4]
y[6] <- 3 + 0.55*y[5]+et[5]
然后
y <- rep(NA,6)
y[1] <- et[1]
y[2] <- 3 + 0.55*y[1]+et[2]
y[3] <- 3 + 0.55*y[2]+et[3]
y[4] <- 3 + 0.55*y[3]+et[4]
y[5] <- 3 + 0.55*y[4]+et[5]
y[6] <- 3 + 0.55*y[5]+et[6]
然后
arima.sim(list(order=c(1,0,0), ar=0.55), n=6, innov=head(et,6)+3)
然而,这三种方法都给出了不同的结果.我想知道这是为什么,恐怕我没有理解一些基本的东西.
However all three methods give different results. I wonder why this is, I am afraid I did not understand something basic.
推荐答案
arima.sim
有一个老化"期以达到平稳.参数n.start
设置这段时间的长度,必须有长度ar
+ ma
.对于 AR(1)
进程,n.start
必须至少为 1.注意,如果不指定 n.start
,arima.sim
会自动为你计算一个合理的.
arima.sim
has a "burn-in" period in order to reach stationarity. Argument n.start
sets the length of this period, and it must has length ar
+ ma
. For an AR(1)
process, n.start
must be at least 1. Note, if you don't specify n.start
, arima.sim
will automatically compute a reasonable one for you.
给定 n.start
(指定的或自动计算的),arima.sim
然后随机抽样 n.start
创新对于老化".这是正确的,因为这个随机程序,你会从一次运行到另一次得到不同的结果:
Given n.start
(either a specified one or auto-computed one), arima.sim
then randomly sample n.start
innovations for "burn-in". It is right because of this random procedure, you will get different result from one run to another:
## innovations of length 6
et <- c(-1.2138662, -0.2854597, 0.5902700, 0.8285463, -0.9954260, -0.3716332)
set.seed(0)
arima.sim(list(order = c(1,0,0), ar = 0.55), n = 6, innov = et, n.start = 1)
#[1] -0.5192413 -0.5710424 0.2761967 0.9804545 -0.4561760 -0.6225300
set.seed(1)
arima.sim(list(order = c(1,0,0), ar = 0.55), n = 6, innov = et, n.start = 1)
# [1] -1.55841580 -1.14258839 -0.03815361 0.80756181 -0.55126700 -0.67483005
我们可以通过使用 start.innov
参数提供一组起始创新来消除这种随机性.注意,这个向量的长度必须是 n.start
:
We can eliminate such randomness, by providing a set of starting innovations using start.innov
argument. Note, this vector must has length n.start
:
## fixing starting innovation at 0; i.e., `y[0] = 0`, so `y[1] = et[1]`.
arima.sim(list(order = c(1,0,0), ar = 0.55), n = 6, innov = et, n.start = 1,
start.innov = 0)
# [1] -1.21386620 -0.95308611 0.06607264 0.86488625 -0.51973856 -0.65748941
现在,将最后一个 arima.sim
与以下内容进行比较:
Now, compare this last arima.sim
with the following:
y <- rep(NA, 6)
y[1] <- et[1]
y[2] <- 0.55 * y[1] + et[2]
y[3] <- 0.55 * y[2] + et[3]
y[4] <- 0.55 * y[3] + et[4]
y[5] <- 0.55 * y[4] + et[5]
y[6] <- 0.55 * y[5] + et[6]
# [1] -1.21386620 -0.95308611 0.06607264 0.86488625 -0.51973856 -0.65748941
我们拥有完全可复制的一切.
We have everything completely reproducible.
以上生成零均值时间序列;如果您希望平均值为 3,请自行将结果移动 +3
:
The above generates zero-mean time series; if you want mean at 3, shift the result by +3
yourself:
y <- y + 3
这篇关于模拟时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!