对向量的每个元素做,就像我对R中的一个元素做的一样

对向量的每个元素做,就像我对R中的一个元素做的一样,r,for-loop,R,For Loop,下面的R代码按步骤执行以下算法: 模拟ARIMA(1,1,0)时间序列(第1行到第9行) 确定要使用的块大小向量(第19行) 选择向量的第一个元素(2)(第20行) 将时间序列拆分为大小相等的2个块(第22行) 随机对每个块重新采样1000次(第25行) 将重新采样的序列重新排列为时间序列数据(第27行) 获取重采样时间序列的RMSE(第29行) 将步骤5到步骤7循环十(10)次,并获得10个RMSE的平均值(第30行到第32行) 如果我正确理解了您试图执行的操作,请在vectorli中的元素之

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

  • 模拟
    ARIMA(1,1,0)
    时间序列(第1行到第9行)
  • 确定要使用的块大小向量(第19行)
  • 选择向量的第一个元素(2)(第20行)
  • 将时间序列拆分为大小相等的2个块(第22行)
  • 随机对每个块重新采样1000次(第25行)
  • 将重新采样的序列重新排列为时间序列数据(第27行)
  • 获取重采样时间序列的RMSE(第29行)
  • 将步骤5到步骤7循环十(10)次,并获得10个RMSE的平均值(第30行到第32行)

  • 如果我正确理解了您试图执行的操作,请在vector
    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
    }
    

    如果你喜欢我的问题,那就大惊小怪吧
    # 2    3     4    5    6    7    8    9 
    # ... ...  ...   ...  ...  ...  ...  ...  
    
    # 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
    }
    
    > RMSEblk
                 2        3         4
    [1,] 0.4671414 0.792863 0.4482386