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.

up vote 3 down vote accepted

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.

  • 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. – Blain Waan Nov 13 '12 at 16:43
  • 1
    Again, I do not know, but the online material suggested that - perhaps @Xi'an will be kind enough to answer this query. – phaneron Nov 13 '12 at 18:25
  • 1
    @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... – Xi'an Nov 14 '12 at 10:04
  • @Xi'an Thanks a lot. I'll definitely appreciate such a book. :) – Blain Waan Nov 14 '12 at 16:03
  • 1
    phaneron Thanks! It was truly useful. Very nice book, very well written. Thanks @Xi'an too. – Blain Waan Nov 15 '12 at 17:49

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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