How Do I Write Different Matrix Produced by a `Loop` into One-Single Matrix

53 views Asked by At

I have an R function as below:

## 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)
#n=10; phi <- 0.6; order <- c(1, 0, 0)
bootstrap1 <- function(n, phi){
  ts <- arima.sim(n, model = list(ar=phi, order = c(1, 0, 0)), sd = 1)
  ########################################################
  ## create a vector of block sizes
  t <- length(ts)    # the length of the time series
  lb <- seq(n-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
  BOOTSTRAP <- foreach(b = 1:length(lb), .combine = 'cbind') %do%{
    l <- lb[b]# block size at each instance 
    m <- ceiling(t / l)                                 # number of blocks
    blk <- split(ts, rep(1:m, each=l, length.out = t))  # divides the series into blocks
    ######################################################
    res<-sample(blk, replace=T, 10)        # resamples the blocks
    res.unlist <- unlist(res, use.names = FALSE)   # unlist the bootstrap series
    train <- head(res.unlist, round(length(res.unlist) - 10)) # Train set
    test <- tail(res.unlist, length(res.unlist) - length(train)) # Test set
    nfuture <- forecast::forecast(train, model = forecast::auto.arima(train), lambda=0, biasadj=TRUE, h = length(test))$mean        # makes the `forecast of test set
    RMSE <- Metrics::rmse(test, nfuture)      # RETURN RMSE
    BOOTSTRAP[b] <- RMSE
  }
  BOOTSTRAPS <- matrix(BOOTSTRAP, nrow = 1, ncol = length(lb))
  colnames(BOOTSTRAPS) <- lb
  BOOTSTRAPS
  return(list(BOOTSTRAPS))
}

Calling the function with the below R command:

set.seed(1, kind = "L'Ecuyer-CMRG")
for (i in 1:3)  { 
  print(bootstrap1(10, 0.5))
}

I have

##$BOOTSTRAPS
##              2        3         4        5        6        7         8         9
##  [1,] 0.8920703 0.703974 0.6990448 0.714255 1.308236 0.809914 0.5315476 0.8175382
##$BOOTSTRAPS
##              2        3         4        5        6        7         8         9
##       [1,] 1.87805 1.432904 1.587332 2.233088 2.266381 1.838313 2.189185 1.154926
##$BOOTSTRAPS
##              2        3         4        5        6        7         8         9
##      [1,] 4.505243 4.417204 4.741331 4.791572 3.614339 3.946822 4.535057 3.571953

Here is What I exspect

##             2         3          4         5         6         7          8        9
##  [1,] 0.8920703 0.703974  0.6990448 0.714255  1.308236  0.809914  0.5315476 0.8175382
##  [2,] 1.87805   1.432904  1.587332  2.233088  2.266381  1.838313  2.189185  1.154926
##  [3,] 4.505243  4.417204  4.741331  4.791572  3.614339  3.946822  4.535057  3.571953

How do I make my R code to obey my mind? I want the column label to show.

0

There are 0 answers