0

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

Siguza
  • 21,155
  • 6
  • 52
  • 89
user3387899
  • 601
  • 5
  • 18
  • 1
    If I understood correctly, it seems like a simple `R` programming question. If you need to view the resampled data set at each iteration, why don't you use an appropriate function, such as `print(repl)` or similar, right after the `sample()` call? – Aleksandr Blekh Apr 08 '15 at 12:31
  • Thank your the comment. Using the print option inside the loop indeed allows to view each resampled dataset. However, it seems like this does not allow me to use each resampled dataset for analysis. Isn't it possible to output the resampled dataset as somekind of list, such that I can work with each dataset seperately as repl[1], repl[2]? I appoligize for my poor R knowledge and thank you again for your help. – user3387899 Apr 08 '15 at 12:44
  • You're very welcome and no need to apologize. In regard to working with each resampled data set, you certainly can refer to each set individually (it wasn't clear to me at first), but for that you need to define `repl` as a vector and initialize corresponding element of that vector at each iteration: before the `for` loop: `repl <- x` (for simplicity); after `seed()`: `repl[boots] <- sample(x, n, replace=TRUE)`. Then you can refer to each resampled data set as `repl[1]`, etc., after the loop. – Aleksandr Blekh Apr 08 '15 at 13:00
  • I tried our suggestion, but it does not seem to work. Unless I interpret it wrong. I wrote my results in the question (see edit2). Could I try something else? – user3387899 Apr 08 '15 at 13:26
  • Please see my answer below. Hope that helps. – Aleksandr Blekh Apr 08 '15 at 13:44

1 Answers1

0

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)
}
Aleksandr Blekh
  • 2,462
  • 4
  • 32
  • 64