R 如何获得回路中九(9)个子系列的精度度量
我想通过以下步骤获得9个不同子系列R 如何获得回路中九(9)个子系列的精度度量,r,windows,loops,forecasting,arima,R,Windows,Loops,Forecasting,Arima,我想通过以下步骤获得9个不同子系列的精度度量值,如下面我的R循环中所述: 模拟10个AR(1)系列样本 将序列拆分为大小为1、2、3、4、5、6、7、8、9的子序列,不重叠 使用替换对每个块大小1000倍的子系列进行重采样 通过连接每个块大小的所有重采样子系列,形成一个新系列 检查每个区块大小新形成的九(9)个子系列的准确性(ME、RMSE、MAE、MPE、MAPE) 详情如下: ##########################################################
的精度度量值,如下面我的R循环中所述:
模拟10个AR(1)
系列样本
将序列拆分为大小为1、2、3、4、5、6、7、8、9的子序列,不重叠
使用替换对每个块大小1000倍的子系列进行重采样
通过连接每个块大小的所有重采样子系列,形成一个新系列
检查每个区块大小新形成的九(9)个子系列的准确性(ME、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
我想得到这样的东西:
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
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
################################################################################
##加载包并准备多核过程
图书馆(预测)
图书馆(future.apply)
计划(多段)
图书馆(平行)
图书馆(foreach)
图书馆(双平行)
n_芯
################################################################################
## 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