Monday, January 16, 2012

Using multiple processor cores in R-Projet

My laptop is equipped with a Core i7 processor with 4 cores that can execute in parallel 8 processes. My R-Project computations use only one single core, if I use the default instructions. I have ended up by thinking that it is a pity that other cores are just sitting idle (sort of), instead of contributing to the speed of my computations, even if I do not run yet really heavy ones in my research. As a consequence, I have started to look for an easy way to use all cores in R-project. And, indeed, there is an easy solution to this problem. It uses the doMC library, and the instructions foreach and %dopar%.
For example, for computing linear models with different dependent variables and a given set of exogenous ones, one can do the following computations:
library(doMC) # There are other parallel computing libraries
registerDoMC() # You mud register one of them for foreach
getDoParWorkers() # Indicates you how many cores have been detected by registerDoMC()

Suppose that you have a dataset called mydata, containing the dependent variables y1, y2, y3, and the independent variables, x1,x2,x3.
We can execute in parallel the estimation of linear models of each y on the set of independent variables, by executing the following code:
myVariableList <- c("y1", "y2", "y3")
results <- foreach(i = 1:length(myVariableList),.errorhandling="stop",.inorder=TRUE)
%dopar% {
model <- lm(as.formula(paste(myVariableList[i],"~x1+x2+x3")),data=mydata)
return(model)
}
%dopar% executes these estimations on different cores, in parallel and a list of the estimated models is saved in the variable results.
We can now look at the characteristics of the estimated models, by printing them successively on the output of R:

for (i in 1:length(results)) { print(summary(results[[i]])) }
Voilà!

Of course, this possibility is especially useful for more complex computations, like stepwise regressions with many independent variables, that can take some time, or regression trees with big datasets, etc.

Saturday, January 07, 2012

Broken XQuartz applications under OSX Lion

So, my new laptop runs OSX Lion. I have been able to migrate my data from the old laptop (but the automatic process provided by the Migration assistant was a failure, Apple support had to help me to do it manually).


Many of the migrated applications have worked without a hitch as soon as the migration ended, but some other applications, like gretl -the GNU econometric software- and some wine-based windows tools, have refused to start. I was quite annoyed by this, but have not had any time to take care of it until today. I have installed wine using MacPorts (instead of the WineBottler that I normally use), the problem persisted. I have discovered PlayItOnMac (PION) implementation of Wine and installed it, but the installation stalled during the creation of the virtual disk needed by this soft.

Now, the support page of PION tells that you should start XQuartz to complete the installation process if you get stalled the first time. And I have tried to do launch it (it lives in Applications/Utilities), but it crashed by telling that the version I am trying to launch is maybe not compatible with my OS. Actually, that was an important clue. I have directly jumped on the XQuartz website and seen that there was a new version waiting for me. I have installed it and BINGO! All my Wine and X11 softs are working again.


So, if you have difficulties to launch Wine-based or XWindows softs after having upgraded to Lion (or on your new Mac under Lion), you should download and install the most recent version of XQuartz, from their web site.