Search code examples
rreplicationresamplingstatistics-bootstrap

View selected sample for each replication in bootstrap loop


Assume following simple bootstrap procedure:

x <-     c(20,54,18,65,87,49,45,94,22,15,16,15,84,55,44,13,16,65,48,98,74,56,97,11,25,43,32,74,45,19,56,874,3,56,89,12,28,71,93)
n <- length(x)

nBoot <- 5; mn <- numeric(nBoot)
for(boots in 1:nBoot){
set.seed(830219+boots)
repl <- sample(x,n,replace=TRUE)
mn[boots] <- mean(repl)
}

Is there a way that I can view the resampled dataset 'repl' for each of the 5 replications?

I would appreciate an answer very much. Many thanks in advance

EDIT

I tried the following:

x <-      c(20,54,18,65,87,49,45,94,22,15,16,15,84,55,44,13,16,65,48,98,74,56,97,11,25,43,32,74,45, 19,56,874,3,56,89,12,28,71,93)
 n <- length(x)

nBoot <- 5; mn <- numeric(nBoot)
for(boots in 1:nBoot){
set.seed(830219+boots)
repl <- sample(x,n,replace=TRUE)
print(repl)
mn[boots] <- mean(repl)
}

This allows me to view each of the 5 resampled datasets, but does not allow me to work with each dataset seperately as repl[1], repl[2],...

EDIT2

I tried following:

x <-      c(20,54,18,65,87,49,45,94,22,15,16,15,84,55,44,13,16,65,48,98,74,56,97,11,25,43,32,74,45,19,56,874,3,56,89,12,28,71,93)
n <- length(x)

nBoot <-3; mn <-  numeric(nBoot); repl <- x
for(boots in 1:nBoot){
set.seed(830219+boots)
repl[boots] <- sample(x, n, replace=TRUE)
pr <- print(repl)
mn[boots] <- mean(repl)
}

I then however get 5 warning messages: 'In repl[boots] <- sample(x, n, replace = TRUE) : number of items to replace is not a multiple of replacement length'

and calling repl[1] only gives me one number


Solution

  • Based on your comments, I've fixed the code. Here's the version that I tested and it seems to work:

    x <- c(20,54,18,65,87,49,45,94,22,15,16,15,84,55,44,13,16,65,48,98,74,56,97,11,25,43,32,74,45,19,56,874,3,56,89,12,28,71,93)
    n <- length(x)
    
    nBoot <-3; mn <- numeric(nBoot)
    repl <- matrix(x, nrow=nBoot, ncol=length(x))
    
    for (boots in 1:nBoot) {
      set.seed(830219+boots)
      repl[boots, ] <- sample(x, n, replace=TRUE)
      pr <- print(repl)
      mn[boots] <- mean(repl)
    }