对向量的每一个元素做我对R中的一个元素所做的事



下面的R代码分步骤执行以下算法:

  1. 模拟ARIMA(1,1,0)时间序列(第1行至第9行(
  2. 确定要使用的块大小的矢量(第19行(
  3. 选择矢量的第一个元素(2((第20行(
  4. 将时间序列拆分为大小等于2的块(第22行(
  5. 随机对每个块重新采样1000次(第25行(
  6. 将重新采样的序列重新排列为时间序列数据(第27行(
  7. 获得重新采样的时间序列的RMSE(第29行(
  8. 将步骤5循环到步骤7十(10(次,获得10 RMSE的平均值(第30行到第32行(
# simulate arima(1,1,0)
library(forecast)
set.seed(100)
wn <- rnorm(10, mean = 0, sd = 1)
ts <- wn[1:2]
for (i in 3:10){
ts<-arima.sim(n=10,model=list(ar=-0.7048,order=c(1,1,0)),start.innov=4.1,n.start=1,innov=wn)
}
ts <-ts[-1]
# write the function for RMSE
rmse <- function(x) {
m <- auto.arima(x)
acu <- accuracy(m)
acu[1, 2]
}
#
t<-length(ts)# the length of the time series
li <- seq(n-2)+1 # vector of block sizes to be 1 < l < n (i.e to be between 1 and n exclusively)
l<- li[1]# first block size
m <- ceiling(t / l) # number of blocks
blk<-split(ts, rep(1:m, each=l, length.out = t)) # divides the series into blocks
singleblock <- vector() #initialize vector to receive result from for loop
for(i in 1:10){
res<-sample(blk, replace=T, 1000) # resamples the blocks
res.unlist<-unlist(res, use.names = F) # unlist the bootstrap series
tsunlist<-ts(res.unlist) # turns the bootstrap series into time series data
# use the RMSE function
RMSE <- rmse(tsunlist)
singleblock[i] <- RMSE # Assign RMSE value to final result vector element i
}
singleblock
mean(singleblock)

我希望R程序返回步骤3第19行以选择矢量中的第二个元素,继续执行步骤4直到步骤8以写入平均RMSE。再次返回步骤3,选择向量的下一个元素,并像以前一样操作,直到向量的元素用完为止。

我希望我的预期结果以表格形式排列为:

# 2    3     4    5    6    7    8    9 
# ... ...  ...   ...  ...  ...  ...  ...  

如果我正确理解您试图在向量li中的元素之间重复这些步骤。可能有一种更有效的方法来完成同样的事情,尤其是对于大的n值。我选了n=5。我创建了一个矩阵"RSMEblk"来存储块均值。如果您也需要这些值,您可以选择创建一个列表来存储单个块。

# simulate arima(1,1,0)
library(forecast)
set.seed(100)
wn <- rnorm(10, mean = 0, sd = 1)
ts <- wn[1:2]
for (i in 3:10){
ts<-arima.sim(n=10,model=list(ar=-0.7048,order=c(1,1,0)),start.innov=4.1,n.start=1,innov=wn)
}
ts <-ts[-1]
# write the function for RMSE
rmse <- function(x) {
m <- auto.arima(x)
acu <- accuracy(m)
acu[1, 2]
}
#
n<-5 # max block size
t<-length(ts)# the length of the time series
li <- seq(n-2)+1 # vector of block sizes to be 1 < l < n (i.e to be between 1 and n exclusively)
RMSEblk<-matrix(nrow = 1, ncol = length(li))#vector to store block means
colnames(RMSEblk)<-li
for (b in 1:length(li)){
l<- li[b]# block size
m <- ceiling(t / l) # number of blocks
blk<-split(ts, rep(1:m, each=l, length.out = t)) # divides the series into blocks
singleblock <- vector() #initialize vector to receive result from for loop
for(i in 1:10){
res<-sample(blk, replace=T, 1000) # resamples the blocks
res.unlist<-unlist(res, use.names = F) # unlist the bootstrap series
tsunlist<-ts(res.unlist) # turns the bootstrap series into time series data
# use the RMSE function
RMSE <- rmse(tsunlist)
singleblock[i] <- RMSE # Assign RMSE value to final result vector element i
}
#singleblock
RMSEblk[b]<-mean(singleblock) #store into matrix
}

对于n=5 ,输出变为

> RMSEblk
2        3         4
[1,] 0.4671414 0.792863 0.4482386

最新更新