0

This is another step of my battle with multi-dimensional arrays in R, previous question is here :)

I have a big R array with the following dimensions:

> data = array(..., dim = c(x, y, N, value))

I'd like to perform a sort of bootstrap comparing the mean (see here for a discussion about it) obtained with:

> vmean = apply(data, c(1,2,3), mean)

With the mean obtained sampling the N values randomly with replacement, to explain better if data[1,1,,1] is equals to [v1 v2 v3 ... vN] I'd like to replace it with something like [v_k1 v_k2 v_k3 ... v_kN] with k values sampled with sample(N, N, replace = T).

Of course I want to AVOID a for loop. I've read this but I don't know how to perform an efficient indexing of this array avoiding a loop through x and y.

Any ideas?

UPDATE: the important thing here is that I want a different sample for each sample in the fourth (value) dimension, otherwise it would be simple to do something like:

> dataSample = data[,,sample(N, N, replace = T), ]
Community
  • 1
  • 1
Matteo De Felice
  • 1,488
  • 9
  • 23
  • " for loops worse than *apply" is an urban legend. So you might just want to do `for(j in 1:{value of N}) mean(data[,,j,])` with of course whatever sort of replacement instructions you desire added into the loop. – Carl Witthoft Apr 17 '14 at 15:26
  • In fact I was looking for a vectorised solution, something like http://cran.r-project.org/doc/manuals/R-intro.html#Array-indexing – Matteo De Felice Apr 17 '14 at 15:37
  • 1
    OK, I'll see what I can gin up . Or see if `vectorize()` is fast enough. – Carl Witthoft Apr 17 '14 at 15:42
  • I don't know what the package `boot` uses under the hood, but you need to define the estimation as a function that takes an index vector. – ilir Apr 17 '14 at 17:24

1 Answers1

0

Also there's the compiler package which speeds up for loops by using a Just In Time compiler. Adding thes lines at the top of your code enables the compiler for all code.

require("compiler")
compilePKGS(enable=T)
enableJIT(3)
setCompilerOptions(suppressAll=T)
Ludecan
  • 331
  • 4
  • 13