Muzaffer Musal
2006-Oct-04 17:36 UTC
[R] A more efficient code using whole object approach?
Dear All, I have the following code which works fine but takes 16+ hours to run and I was hoping someone could give me a hint as to how I could make it more efficient. Let me explain what the code is supposed to do. Each single row of the m.o.f matrix (n*p dim) has to go through the following operation Dm*m.o.f[i]^alpha1m where both Dm and alpha1m have n*1 dimensions. Each row of the m.o.f is to be evaluated n times for the different values of alpha1m and Dm. My code changes the Dm and alpha1m (n*1) matrices into (n*p) matrices (not shown below, done only 1 time) and also change the (1*p) m.o.f row into a m.o (n*p) matrix (shown below, done n times). For each of the n rows of the m.o.f matrix we repeat this operation which we record in the temp matrix. The third line of the loop multiplies for each row, the columns of the matrix and takes the resultant mean of the vector. I know I should use the whole object approach somehow but after many tries I gaave up for the long approach. I know the explanation above is rather long and I apologize for that, any hint/help will be greatly appreciated. for (i in 1:numrows) { mo.o<-t(array(m.o.f[i,],dim=c(numcols,numrows))) t<-Dm*m.o^alpha1m l1[i]<-mean(apply(t,1,"prod")) } Best Regards, --------------------------------- [[alternative HTML version deleted]]
A couple of thoughts (hopefully others will have even better suggestions): 1. Could you do a some of the computations on the log scale? This would turn exponents into multiplications and multiplications into additions and may turn a lot of the work into matrix multiplications. 2. It does not appear that the order matters (i=1, 2, ...), so this could be easily parallelized, the nws package allows a fairly straight forward way to do this if you have access to multiple computers (I for one don't have a problem stealling cycles from my coworkers computers when they are not doing computationally intesive things). Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Muzaffer Musal Sent: Wednesday, October 04, 2006 11:36 AM To: r-help at stat.math.ethz.ch Subject: [R] A more efficient code using whole object approach? Dear All, I have the following code which works fine but takes 16+ hours to run and I was hoping someone could give me a hint as to how I could make it more efficient. Let me explain what the code is supposed to do. Each single row of the m.o.f matrix (n*p dim) has to go through the following operation Dm*m.o.f[i]^alpha1m where both Dm and alpha1m have n*1 dimensions. Each row of the m.o.f is to be evaluated n times for the different values of alpha1m and Dm. My code changes the Dm and alpha1m (n*1) matrices into (n*p) matrices (not shown below, done only 1 time) and also change the (1*p) m.o.f row into a m.o (n*p) matrix (shown below, done n times). For each of the n rows of the m.o.f matrix we repeat this operation which we record in the temp matrix. The third line of the loop multiplies for each row, the columns of the matrix and takes the resultant mean of the vector. I know I should use the whole object approach somehow but after many tries I gaave up for the long approach. I know the explanation above is rather long and I apologize for that, any hint/help will be greatly appreciated. for (i in 1:numrows) { mo.o<-t(array(m.o.f[i,],dim=c(numcols,numrows))) t<-Dm*m.o^alpha1m l1[i]<-mean(apply(t,1,"prod")) } Best Regards, --------------------------------- [[alternative HTML version deleted]] ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.