[R] python

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Sat Nov 21 16:33:53 CET 2009


On Sat, Nov 21, 2009 at 2:29 PM, Jean Legeande <jean.legeande at gmail.com> wrote:
> Dear R users,
>
> I would like to make my R code for MCMC faster. It is possible to integrate
> C code into R but I think C is too complicated for me. I would need a C
> introduction only for MCMC and I do not know if such a thing exists.
>
> I was thinking of Python (and scipy). Where could I read about its
> integration into R ? How developed are the statistical packages in Python ?
> I could not find a Python package on the web with functions to simulate
> Wishart, or multivariate gamma or student distributions.
>
> Since I am a little bit lost, I write this message to the R help list. Sorry
> for these naive questions and thanks for your help.
>

 Have you done a profile of your MCMC code to see where the bottleneck
is? Without doing that first any effort could be a total waste of
time.

 R can do a lot of it's calculations at the same level as C, so if 80%
of your time is spent inverting matrices then converting to Python or
C (or even assembly language) isn't going to help much since R's
matrix inversion is done using C code (and quite possibly very
optimised C code with maybe some assembly language too).

 So do a profile (see ?Rprof) and work out the bottleneck. It might be
one of your functions, in which case just re-writing that in C and
linking to R (see programmers guide and a good C book) will do the
job.

 My hunch is that Python and R run at about the same speed, and both
use C libraries for speedups (Python primarily via the numpy package).

 You can call the GSL from Python, and there are probably tricks for
getting the distributions you want:

http://www.mailinglistarchive.com/help-gsl@gnu.org/msg00096.html

 describes how to get samples from a Wishart.

 However using the GSL from Python probably wont be much faster than
using R because again it's all at the C level already. Did I suggest
you profile your code?

Barry




More information about the R-help mailing list