Question

So I have to run large mcmc's which are not feasible in R. Hence I am writing out the loops in C and calling it from R using .C

However, within this loop, I occasionally have sampling steps or modeling steps for which there are standard packages in R. Coding these things in C from scratch would be overkill for me.

Which is why I would like to call R functions here. I am going to do this using "8.1 Embedding R under Unix-alikes" in "writing R extensions".

So it becomes, R being embedded in C, which is in itself a .C call from R. Is this approach OK in terms of

  • simplicity, or is there an more elegant framework ?

  • stability (R calling C, which calls R)

  • scalability (R calling C, which calls R), I will be running multiple such mcmc samplers on amazon ?

  • memory usage?

I am only interested in R embedding in C (not c++). Speed is very important to me.

Was it helpful?

Solution

We do precisely this in the mcmc package on CRAN, http://cran.us.r-project.org/web/packages/mcmc/index.html . That link includes way to download the source code.

This package implements the Metropolis-Hastings algorithm. Specifically, C code handles running the MH loop, but calls a user supplied R function to evaluate the log unnormalized target density at each iteration, it will also call a user supplied output function.

I've run this code with very large models and datasets, so it's definitely feasible to run "large" MCMC estimations in R with this approach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top