简体   繁体   中英

How Do I Set Seed to Archive Reproducibility of Simulation Result with Replicate Function in R

I wrote an R function and will like to run it kth times and will want the same result anytime same is run in the same environment. I think of setting seed but can not achieve the same result as I ran the same function with the same seed two consecutive times.

## 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 = n_cores)
bootstrap1 <- function(n, phi){
  ts <- arima.sim(n, model = list(ar=phi, order = c(1, 1, 0)), sd = 1)
  #ts <- numeric(n)
  #ts[1] <- rnorm(1)
  #for(i in 2:length(ts))
  #  ts[i] <- 2 * ts[i - 1] + rnorm(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
  #BOOTSTRAP <- list(length(lb))
  ########################################################
  ## This section use foreach function to do detail in the brace
  BOOTSTRAP <- foreach(b = 1:length(lb), .combine = 'cbind') %dopar%{
    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, 1000)        # 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" = BOOTSTRAPS))
}

First Trial

set.seed(123, kind = "L'Ecuyer-CMRG")
t(replicate(3, bootstrap1(10, 0.5)$BOOTSTRAPS[1,]))
#            2        3        4        5        6        7        8        9
#[1,] 3.353364 4.097191 3.759332 3.713234 4.541143 4.151920 4.603380 5.237056
#[2,] 4.490765 5.037171 4.289265 3.964172 3.225878 5.345506 4.646740 2.593153
#[3,] 4.514881 4.838114 3.701961 5.069747 4.165742 4.130256 3.951216 4.133241

Second Trial

set.seed(123, kind = "L'Ecuyer-CMRG")
t(replicate(3, bootstrap1(10, 0.5)$BOOTSTRAPS[1,]))
#            2        3        4        5        6        7        8        9
#[1,] 3.271285 3.701031 2.725770 3.867532 3.283368 3.713057 3.274201 4.141896
#[2,] 3.987040 3.767720 5.440987 3.850190 3.306520 5.399880 5.337676 3.288834
#[3,] 5.157924 3.895024 3.996077 4.855608 4.443317 5.224098 5.335144 2.918870

How do I set seed or what will I do to get the same result?

Edit

I am operating R on Windows.

You should only be setting the seed once. It seems like you're setting the seed twice (once with set.seed(1) inside your bootstrap function, and again with set.seed(123, kind = "L'Ecuyer-CMRG") outside your bootstrap function.

Try only using only one set.seed() function (and using the same value both times), and see if that fixes it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM