1

The followings are functions for bootstrapping, but how can I make the result reproducible? I tried set.seed() but that does not work because the every time lapply calls function boot.lm.vector, the function just produced one simulated set and calculated coefficients once. Is there any thing in R that can function like a seed list? or any other way to make the result reproducible?

boot.lm.vector <- function(index, inputData) {
  d <- inputData[sample.int(nrow(inputData), replace = T),]
  a <- ncol(inputData)-1    
  X <- d[, 1:a]    
  y <- d[, a+1]
  solve(crossprod(X), crossprod(X,y))
}

rtest <- lapply(1:10000, fun = boot.lm.vector, inputData = boot_set) 
rtestdf <- plyr::ldply(rtest)
Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
Teng Li
  • 13
  • 4

1 Answers1

0

If you set the seed using an index inside your function, you should be able to reproduce it. Dummy boot.lm.vector function below:

## samples 1 item from inputData
boot.lm.vector <- function(index, inputData) {
                                              set.seed(index)
                                              return(sample(inputData, 1)) 
                                              }

## iterating 5 times: use lapply as per your requirement
test <- sapply(1:5, FUN = boot.lm.vector, inputData = 1:10) 
test
[1] 3 2 2 6 3                                 # reproducible result
Mankind_008
  • 2,158
  • 2
  • 9
  • 15