title: speed up a monte carlo simulation with nested loop in R
body: <p>I would like to speed up the below monte carlo simulation of a DEA estimate</p>
  
  <pre><code>A&lt;-nrow(banks)
  effm&lt;-matrix(nrow=A, ncol=2)
  m&lt;-20
  B&lt;-100
  
  pb &lt;- txtProgressBar(min = 0,
                       max = A, style=3)
  for(a in 1:A) {
    x1&lt;-x[-a,]
    y1&lt;-y[-a,]
    theta=matrix(nrow=B,ncol=1) 
  
    for(i in 1:B){
  
      xrefm&lt;-x1[sample(1:nrow(x1),m,replace=TRUE),]
      yrefm&lt;-y1[sample(1:nrow(y1),m,replace=TRUE),]
      theta[i,]&lt;-dea(matrix(x[a,],ncol=3),
                     matrix(y[a,],ncol=3),
                     RTS='vrs',ORIENTATION='graph',
                     xrefm,yrefm,FAST=TRUE)
    }
  
    effm[a,1]=mean(theta)
    effm[a,2]=apply(theta,2,sd)/sqrt(B)
    setTxtProgressBar(pb, a) 
  }
  close(pb)
  effm 
  </code></pre>
  
  <p>Once A becomes large the simulation freezes.  i am aware from online research that the apply function rapidly speeds up such code but am not sure how to use it in the above procedure.</p>
  
  <p>Any help/direction would be much appreciated</p>
  
  <p>Barry </p>