Fast function for generating bootstrap samples in matrix forms in R

I have a matrix A , and I would like to draw patterns from each column A and build new matrices. For example:

A = matrix(seq(1,9),3,3)

therefore, to get the first bootstrap matrix, I would fetch with the replacement (3 times) from the first column of A , i.e. 1,2,3, sample with replacement (3 times) from the second column A , i.e. 4,5,6, and a sample with substitution (3 times) from the third column A , i.e. 7.8.9. After that, I rebuild the 1st bootstrap matrix B1 by combining the three bootstrap vectors. I will repeat this procedure for B = 199 times, so that the original matrices B1, ..., B199 will be available.

My question is: how can I make this program run faster? What function should I use? I know that apply includes essentially for loops, so speed is not guaranteed. What about do.call ? Thanks!

+2
r statistics-bootstrap
source share
1 answer

You can use replicate and apply :

 A <- matrix(seq(1,9),3,3) B <- 199 replicate(B, apply(A, 2, sample, replace = TRUE)) 
+5
source share

All Articles