【发布时间】:2022-01-05 19:46:47
【问题描述】:
大家下午好,
我试图找到我通过模拟运行生成的时间序列的标准预测误差,该模拟运行是通过名为 sim_11 的函数定义的,具有 250 次模拟。这是在下面的第一批代码中提供的。
第二批创建时间序列模型 (AR(1)) 并尝试预测接下来的 5 个值,我总共进行了 250 次模拟。对于每次模拟,我应该能够得到 5 个预测错误,并且在 250 次模拟之后,我应该有一个 250 行和 5 列的结果表。但是,当我尝试在 for 循环中设置它时,我最终只得到了 250 个单个值,而实际上我应该得到一个 250 x 5 的表/矩阵。我相信错误在
pred_error_AR1_100[i]<-table((pre_AR1_100$se[1]),(pre_AR1_100$se[2]),
(pre_AR1_100$se[3]),(pre_AR1_100$se[4]),
(pre_AR1_100$se[5]), ncol=5)
但是我无法弄清楚格式应该在哪里或应该是什么。
提前谢谢你。
下面提供了两个代码批处理以供复制。
# Setup the simulation run with 100 observations and 250 simulations
sim_11=function(){
e<-rnorm(200, mean=0, sd=0.2) # Produces 200 white noise values
Y_t=c(0,0) # Fills in the first 2 observations as a lag of 2 can be handled
for (i in 3:length(e)){
f1<- 0.138+(0.316+0.982*Y_t[i-1])*exp(-3.89*(Y_t[i-1])^2)
f2<- -0.437-(0.659+1.260*Y_t[i-1])*exp(-3.89*(Y_t[i-1])^2)
Y_t[i]<-f1*Y_t[i-1]+f2*Y_t[i-2]+e[i]
}
Y_t<-Y_t[101:200] # Removes the first 100 observations
Y_t # Prints the 100 observations
}
lapply(1:250, function(x) sim_11()) # Provides the results of the 250 simulations
x_100_lstar=replicate(250,sim_11()) # Places all results into one matrix
pred_error_AR1_100=0
# controls<-list(gammaInt=c(0.1,2000), nGamma=50)
for (i in 1:ncol(x_100_lstar)){
AR1_100<-ar(x_100_lstar[,i])
pre_AR1_100<-predict(AR1_100, n.ahead=5)
pred_error_AR1_100[i]<-table((pre_AR1_100$se[1]),(pre_AR1_100$se[2]),
(pre_AR1_100$se[3]),(pre_AR1_100$se[4]),
(pre_AR1_100$se[5]), ncol=5)
}
pred_error_AR1_100
【问题讨论】:
标签: r statistics time-series