# How can I optimise computational efficiency when fitting a complex model to a large data set repeatedly?

I am having performance issues using the MCMCglmm package in R to run a mixed effects model. The code looks like this:

MC1<-MCMCglmm(bull~1,random=~school,data=dt,family="categorical"
, prior=list(R=list(V=1,fix=1), G=list(G1=list(V=1, nu=0)))
, slice=T, nitt=iter, ,burnin=burn, verbose=F)


There are around 20,000 observations in the data and they are clustered in around 200 schools. I have dropped all unused variables from the dataframe and removed all other objects from memory, prior to running. The problem I have is that it takes a very long time to run, unless I reduce the iterations to an unacceptably small number. With 50,000 iterations, it takes 5 hours and I have many different models to run. So I would like to know if there are ways to speed up the code execution, or other packages I could use. I am using MCMCglmm because I want confidence intervals for the random effects.

On the other hand, I was hoping to get a new PC later this year but with a little luck I may be able to bring that forward, so I have been wondering how to best spend a limited amount of money on new hardware - more RAM, faster CPU etc. From watching the task manager I don't believe RAM is the issue (it never gets above 50% of physical used), but the CPU usage doesn't get much above 50% either, which strikes me as odd. My current setup is a intel core i5 2.66GHz, 4GB RAM, 7200rpm HDD. Is it reasonable to just get the fastest CPU as possible, at the expense of additional RAM ? I also wondered about the effect of level 3 CPU cache size on statistical computing problems like this ?

Update: Having asked on meta SO I have been advised to rephrase the question and post on Superuser. In order to do so I need to give more details about what is going on "under the hood" in MCMCglmm. Am I right in thinking that the bulk of the computations time is spent doing optimisation - I mean finding the maximum of some complicated function ? Is matrix inversion and/or other linear algebra operations also a common operation that could be causing bottlenecks ? Any other information I could give to the Superuser community would be most gratefully received.

-
I don't think it should be a surprise that MCMC takes a long time on such problems. I am sure there are probably ways to make it run faster. But to crank out a correct answer is still going to take time. –  Michael Chernick Jun 22 '12 at 14:56
@Michael Chernick, thank you - I am aware it will still take time. I would just like to minimise it as much as possible, that's all. My dad has an Oracle SPARC T4 at his work and that runs MCMC quite fast ;) –  Joe King Jun 22 '12 at 15:06
@JoeKing, I've edited your title to be more descriptive and perhaps draw in more users who can help you. I've also found that fitting lmer() models to large data sets can take quite a while, especially if you need to do it many times. An answer to your question may lie in parallel computing although other users (e.g. @DirkEddelbuettel) would be much more helpful than me with this. There's also a chance that you may get better answers on stackoverflow. –  Macro Jun 22 '12 at 16:15
Macro , thank you for the helpful edit. I have also used glmer (as you know from my other posts) and that takes about 20 seconds, but the problem is that it doesn't give confidence intervals or standard errors, and from what I read on a mailing list archive the author of the lme4 package says that the sampling distribution of the random effects can be very skewed, so those statistics are not reported. Actually I found from MCMCglmm so far that in my case they are approaching normal (not that this helps much - I'm just saying). Would it be better if I request to migrate it to SO ? –  Joe King Jun 22 '12 at 16:33
I do not know the specifics of mcmcglmm, but have used MCMC methods a lot. The nice thing about MCMC is that is is embarrassingly paralleliseable (that's a technical term!). If you have multiple cores, you run independent chains on each then pool the results. This is how I run MCMC, but I've written my own parallel C++ codes (using MPI) to do it. In terms of hardware advice then, go for something with as many cores as possible. That assumes that whatever tool you are using can take advantage of the multiple cores. In terms of info to give SU in your question, find out if you can utilise cores. –  Bogdanovist Jun 28 '12 at 5:30

## 1 Answer

Why not run it on Amazon's EC2 cloud-computing service or a similar such service? MCMCpack is, if I remember correctly, mostly implemented in C, so it isn't going to get much faster unless you decrease your model complexity, iterations, etc. With EC2, or similar cloud-computing services, you can have multiple instances at whatever specs you desire, and run all of your models at once.

-
One modification to this: running on m2.4xlarge (the 68.7GB RAM option) is one only way to guarantee you're getting the full machine, so that you don't necessarily hit RAM caching issues that may occur on VMs (virtual machines / AMIs) that run on a fraction of the machine. –  Iterator Jun 24 '12 at 2:47
+1 This seems like a really good idea. Thanks ! –  Joe King Jun 24 '12 at 11:34
Check my answer then :) I desire meaningless karma. –  Zach Jun 24 '12 at 12:27