我想生成一个包含50个值的AR(2)过程(3阶自回归过程):
其中:Y(t)是时间序列,E(t)是误差项。
从标准正态分布中随机选取误差项值,我们假设如下:
作为一名前Java程序员和相对较新的R转换器,循环的使用对我来说非常明显,下面的代码就是我如何去做。但是,我一直在尝试转向矢量化范例,并一直在寻找一种方法来执行这项任务的矢量化方式'。非常感谢你的帮助。
set.seed(123) # Reproducible
u <- rnorm(50) # Vector of randomly generated numbers for the error term
Y <- vector(length = 50) # Vector that will contain the generated time series values
a1 <- 0.25 # alpha 1
a2 <- 1.17 # alpha 2
# Based on the assumptions made, we can derive the values for the first 2 time periods:
Y[1] <- a1*0 + a2*0 + u[1]
Y[2] <- a1*Y[1] + a2*0 + u[2]
# The following "loop" generates the remaining values
i <- 3
while(i <= 50){
Y[i] <- a1 * Y[i-1] + a2 * Y[i-2] + u[i]
i <- i+1
}
Y
[1] -0.5604756 -0.3702964 0.8103777 -0.1601440 1.0373937 1.7870450 2.1214280 1.3561384 2.1342525 1.6745831
[11] 4.1398030 3.3540268 6.0828477 5.5556059 7.9499921 10.2744701 12.3679588 13.1465026 18.4584934 19.5232400
[21] 25.4094235 28.9765717 35.9471640 42.1604887 51.9732648 60.6343946 76.8051055 90.2968912 111.2980593 134.7256925
[31] 164.3266167 198.4156429 242.7611779 293.7147301 358.2808418 433.9050850 528.2187738 639.6617312 777.6254355 942.4301133
[41] 1144.7345808 1388.6189605 1685.2288034 2048.1603406 2484.9657471 3016.4659267 3661.1235209 4444.0793591 5395.3143244 6548.3180622