I would like to speed up the below monte carlo simulation of a DEA estimate
A<-nrow(banks)
effm<-matrix(nrow=A, ncol=2)
m<-20
B<-100
pb <- txtProgressBar(min = 0,
max = A, style=3)
for(a in 1:A) {
x1<-x[-a,]
y1<-y[-a,]
theta=matrix(nrow=B,ncol=1)
for(i in 1:B){
xrefm<-x1[sample(1:nrow(x1),m,replace=TRUE),]
yrefm<-y1[sample(1:nrow(y1),m,replace=TRUE),]
theta[i,]<-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
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.
Any help/direction would be much appreciated
Barry
apply
function may or may not be faster than a for loop; it depends on what you're doing. You need to profile your code for speed to see what portions are slowest (see?Rprof
), then you will know what needs to be faster. People could help profile your code if you provide a reproducible example. – Joshua Ulrichbanks
? – Roman Luštrik