重命名矩阵列而不是R

问题描述

我有一个R函数,我对该函数进行了重写,以为foreach函数parallel process留出空间,但是矩阵结果具有不同的列名。

以下原始功能

# simulate arima(1,0)
library(forecast)
n=10
phi <- 0.6
set.seed(106100125)
ar1 <- arima.sim(n,model = list(ar=phi,order = c(1,0)),sd = 1)
auto.arima(ar1)
ts <- ar1

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)

# vector to store block means
RMSEblk <- matrix(nrow = 1,ncol = length(li))
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

  # initialize vector to receive result from for loop
  singleblock <- vector()                     
  for(i in 1:10){
    res<-sample(blk,replace=T,10)        # resamples the blocks
    res.unlist<-unlist(res,use.names = F)    # unlist the bootstrap series
    # Split the series into train and test set
    train <- head(res.unlist,round(length(res.unlist) * 0.6))
    h <- length(res.unlist) - length(train)
    test <- tail(res.unlist,h)

    # Forecast for train set
    model <- auto.arima(train)
    future <- forecast(test,model=model,h=h)
    nfuture <- as.numeric(future$mean)        # makes the `future` object a vector            
    RMSE <- rmse(test,nfuture)               # use the `rmse` function from `Metrics` package

    singleblock[i] <- RMSE # Assign RMSE value to final result vector element I
  }

  RMSEblk[b] <- mean(singleblock) # store into matrix
}

RMSEblk

所需的结果安排

##            2        3       4        5        6        7        8        9
   ##[1,] 1.022961 1.440676 1.54268 1.074976 1.205165 1.186345 1.436563 1.501218

修改后的功能

## 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)
n=10
phi <- 0.6
set.seed(106100125)
ar1 <- arima.sim(n,sd = 1)
auto.arima(ar1)
ts <- ar1
########################################################
## greate a vector of block sizes
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)
########################################################
## This section create matrix to store block means
RMSEblk <- matrix(nrow = 1,ncol = length(li))
colnames(RMSEblk) <-li
########################################################
## This section use foreach function to do detail in the brace
RMSEblk <- foreach(b = 1:length(li),.combine = 'cbind') %do%{
#for (b in 1:length(li)){
  l <- li[b]# block size
  m <- ceiling(t / l)                                 # number of blocks
  blk <- split(ts,length.out = t))  # divides the series into blocks
  ######################################################
  ## Thissubsection initialize vector to receive result from for loop
  singleblock <- vector()
  singleblock <- foreach(i = 1:10,.packages = c("forecast"),.combine = 'c') %dopar% { ### Replacement parallel foreach loop
  #for(i in 1:10){
    res<-sample(blk,10)        # resamples the blocks
    res.unlist <- unlist(res,use.names = FALSE)   # unlist the bootstrap series

    train <- head(res.unlist,round(length(res.unlist) * 0.8)) # Train set

    test <- tail(res.unlist,length(res.unlist) - length(train)) # Test set

    nfuture <- forecast(train,model = auto.arima(train),lambda=0,biasadj=TRUE,h = length(test))$mean        # makes the `forecast of test set
    RMSE <- accuracy(nfuture,test)      # RETURN RMSE
    singleblock[i] <- RMSE # Assign RMSE value to final result vector element I
    }

  RMSEblk[b] <- mean(singleblock) # store into matrix
}
stopCluster(cl)
RMSEblk[] <- future_vapply(b=1:length(li),RMSEblk[b],numeric(1))

RMSEblk

**结果的不必要安排**

## Error: ‘is.function(FUN)’ is not TRUE
##> 
##> RMSEblk
 ##result.1 result.2 result.3 result.4 result.5 result.6 result.7 result.8
 ##[1,]  34.4202 20.54789 24.77103 25.49809 15.42677 30.80389 18.28471 15.92572

我想要的

我希望将second function的结果作为first function的结果进行展示和安排。这意味着我想用result.1作为列名,而不是2,或者我想用result. 8作为列名9的{​​{1}}。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)