如何在循环中获取九 (9) 个子系列的精度测量

How to Get Accuracy Measures for Nine(9) Sub-series in a loop

我想通过以下步骤获得下面 9 个不同子系列as described in myR 循环的 accuracy measures

  1. 模拟 AR(1) 系列的 10 个样本。
  2. 将系列拆分为大小为 1, 2, 3, 4, 5, 6, 7, 8, 9 的子系列而不重叠。
  3. 对每个块大小的子系列重新采样 1000 次并进行替换。
  4. 通过连接每个块大小的所有重采样子系列来形成一个新系列。
  5. 检查每个块大小的新形成的 NINE(9) 子系列的 accuracyME、RMSE、MAE、MPE、MAPE)。

如下:

################################################################################
## Load packages and prepare multicore process
library(forecast)
library(future.apply)
plan(multisession)
library(parallel)
library(foreach)
library(doParallel)
n_cores <- detectCores()
cl <- makeCluster(n_cores)
registerDoParallel(cores = detectCores())

## simulate ARIMA(1,0, 0)
set.seed(289805)
N10_ar0.8_seed289805 <- arima.sim(n=10, model=list(ar=0.8, order=c(1, 0, 0)), sd=1)
auto.arima(N10_ar0.8_seed289805, ic="aicc")

########################################################
## create a vector of block sizes
t <- length(N10_ar0.8_seed289805)    # the length of the time series
lb <- seq(t-1) #seq(t-2)+1   # vector of block sizes to be 1 < l < n (i.e to be between 1 and n exclusively)
########################################################
## This section create matrix to store block means
BOOTSTRAP <- matrix(nrow = 1, ncol = length(lb))
colnames(BOOTSTRAP) <- lb
########################################################
## This section use foreach function to do detail in the brace
#set.seed(123, kind = "L'Ecuyer-CMRG")
BOOTSTRAP <- foreach(b = 1:length(lb), .combine = 'cbind', .packages = c("forecast", "Metrics")) %dopar%{
  l <- lb[b]# block size at each instance 
  m <- ceiling(t / l)                                 # number of blocks
  blk <- split(N10_ar0.8_seed289805, rep(1:m, each=l, length.out = t))  # divides the series into blocks
  ######################################################
  set.seed(5)
  res<-sample(blk, replace=T, 1000)        # resamples the blocks
  res.unlist <- unlist(res, use.names = FALSE)   # unlist the bootstrap series
  mod <- auto.arima(res.unlist)
  fit <- fitted(mod)
  ACCURACY <- forecast::accuracy(fit, res.unlist)      # RETURNS ACCURACY
  BOOTSTRAP[b] <- ACCURACY
}
BOOTSTRAPS <- matrix(BOOTSTRAP, nrow = 1, ncol = length(lb))
colnames(BOOTSTRAPS) <- lb
BOOTSTRAPS

我得到了:

#                 1        2        3         4        5            6        7        8        9
# [1,] -0.0001474358 1.576266 1.298314 -1.210742 204.8705 0.0002838375 1.413514 1.129676 8.073587

我想得到这样的东西:

Statistic   1      2        3       4      5        6      7      8       9
----------------------------------------------------------------------------------
MSE       0.886   1.021    0.8866  0.886  1.021    0.886  0.886  1.021    0.886
MAE       0.762   0.835    0.762   0.762  0.835    0.762  0.762  0.835    0.762
MAPE      3.859   2.263    3.859   3.859  2.263    3.859  3.859  2.263    3.859
ME       -0.005   0.381   -0.005   -0.005 0.381    -0.005 -0.005 0.381    -0.005
MPE      -0.086   1.004   -0.086   -0.086 1.004    -0.086 -0.086 1.004   -0.086

ACCURACY 是一个 1 行矩阵:

ACCURACY
                   ME     RMSE      MAE      MPE     MAPE
Test set 0.0002838375 1.413514 1.129676 8.073587 205.3605

dim(ACCURACY)
[1] 1 5

您可以使用 rbind 而不是 cbind 并转置最终结果。
请注意,您不需要定义 BOOTSTRAP 因为它直接由 foreach 返回:

################################################################################
## Load packages and prepare multicore process
library(forecast)
library(future.apply)
plan(multisession)
library(parallel)
library(foreach)
library(doParallel)
n_cores <- detectCores()
cl <- makeCluster(n_cores)
registerDoParallel(cores = detectCores())

## simulate ARIMA(1,0, 0)
set.seed(289805)
N10_ar0.8_seed289805 <- arima.sim(n=10, model=list(ar=0.8, order=c(1, 0, 0)), sd=1)
auto.arima(N10_ar0.8_seed289805, ic="aicc")

########################################################
## create a vector of block sizes
t <- length(N10_ar0.8_seed289805)    # the length of the time series
lb <- seq(t-1) #seq(t-2)+1   # vector of block sizes to be 1 < l < n (i.e to be between 1 and n exclusively)
########################################################

########################################################
## This section use foreach function to do detail in the brace
#set.seed(123, kind = "L'Ecuyer-CMRG")
BOOTSTRAP <- foreach(b = 1:length(lb), .combine = 'rbind', .packages = c("forecast", "Metrics")) %dopar%{
  l <- lb[b]# block size at each instance 
  m <- ceiling(t / l)                                 # number of blocks
  blk <- split(N10_ar0.8_seed289805, rep(1:m, each=l, length.out = t))  # divides the series into blocks
  ######################################################
  set.seed(5)
  res<-sample(blk, replace=T, 1000)        # resamples the blocks
  res.unlist <- unlist(res, use.names = FALSE)   # unlist the bootstrap series
  mod <- auto.arima(res.unlist)
  fit <- fitted(mod)
  ACCURACY <- forecast::accuracy(fit, res.unlist)      # RETURNS ACCURACY
  ACCURACY
}
BOOTSTRAPS <- t(BOOTSTRAP)
colnames(BOOTSTRAPS) <- lb
BOOTSTRAPS


                 1            2             3             4            5             6             7             8            9
ME   -1.474358e-04 2.838375e-04 -1.109465e-04 -3.546411e-04 4.991512e-05 -5.613602e-04 -2.061036e-04 -2.793918e-04  -0.00016262
RMSE  1.576266e+00 1.413514e+00  1.407372e+00  1.226815e+00 1.142930e+00  1.089578e+00  1.025063e+00  9.066126e-01   0.86546536
MAE   1.298314e+00 1.129676e+00  1.111374e+00  8.359341e-01 7.644257e-01  8.700083e-01  8.375352e-01  7.335306e-01   0.66246307
MPE  -1.210742e+00 8.073587e+00 -6.381357e+00  2.462817e+01 8.554913e+01  1.980497e+01  2.027281e+01 -2.615038e+01 -32.72766437
MAPE  2.048705e+02 2.053605e+02  1.922015e+02  1.500389e+02 1.533140e+02  1.768961e+02  1.665399e+02  1.436919e+02 140.63999983