I have been trying to learn the Bayesian simulation based methods of posterior approximation. Although the theories are now quite clear but I am seeking for some examples with R codes so that I can practice and implement to my own work.

I am particularly looking for the examples with R codes of the non-iterative and iterative Monte Carlo methods. Mainly-

  1. Importance sampling
  2. Rejection sampling
  3. Metropolis algorithm
  4. Metropolis-Hastings algorithm
  5. Gibbs sampler

So, is it possible to find examples with R codes of these methods of simulation somewhere collectively? I mean it is a kind of heuristic learning, so basically I need some tutorial or some blog or website where the examples are properly described with corresponding R codes.

Edit: I am sorry I forgot to mention, I was actually looking for creating functions in R for these sampling methods to know the basic of these algorithms. Not looking for any packaged software.


1 Answer 1


Online information would suggest

Christian Robert, George Casella (2009). Introducing Monte Carlo Methods with R. Springer-Verlag, New York.

would at least be a start and the code is available in the mcsm package here.

Perhaps someone who has actually read it might be able to provide further comments.

  • $\begingroup$ Thank you for the reference, but is it possible to learn how to program the algorithms myself? I mean I am interested to not use any package but to learn how to write the very basic functions for these algorithms. $\endgroup$
    – Blain Waan
    Nov 13, 2012 at 16:43
  • 1
    $\begingroup$ Again, I do not know, but the online material suggested that - perhaps @Xi'an will be kind enough to answer this query. $\endgroup$
    – phaneron
    Nov 13, 2012 at 18:25
  • 1
    $\begingroup$ @BlainWaan: In our book, we use the R language to teach how to program the algorithms. The package is just there to avoid the user re-typing every example. I am not trying to (over)sell the book, however I use it in class to get the students to learn Monte Carlo and MCMC. If you look at the book online, you will see we cover exactly the program you set in your question... $\endgroup$
    – Xi'an
    Nov 14, 2012 at 10:04
  • $\begingroup$ @Xi'an Thanks a lot. I'll definitely appreciate such a book. :) $\endgroup$
    – Blain Waan
    Nov 14, 2012 at 16:03
  • 1
    $\begingroup$ phaneron Thanks! It was truly useful. Very nice book, very well written. Thanks @Xi'an too. $\endgroup$
    – Blain Waan
    Nov 15, 2012 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.