[Rcpp-devel] Input on code design implementing a MCMC algorithm in R and improving performance with RcppArmadillo

Joonwook Park joonwookpark at gmail.com
Fri Apr 10 20:05:13 CEST 2020


A while ago, I did exactly what you are trying to do. I converted my MCMC
code in Matlab to R although it does not involve Kalman filter forward
filtering and backward smoothing algorithm. I converted one program that
takes the most of the time to RCpp with the help of others and used
other modules as in R. My experience was not as positive as others may have
had as it takes about 7 days to run 10,000 iterations with R for the same
data, which normally takes about 7 hours in Matlab. It was a fun project
but it might be a better idea to build a new program from scratch in R and
RCpp rather than converting an existing Matlab program to R.  I did use
list variable type for data, parameters, and storage for MCMC run. If you
are familiar with Matlab, the conversion to R and RCpp would not take much
time and obviously you have a fantastic community that lends their hands
should you have any questions (Thank you Dirk!). Obviously there is a code
optimization issue, which I am not so familiar with R and RCpp, that led to
increasing computing time in my case. My goal was to build new programs in
R for the future but I was discouraged by the speed. You can also try
Microsoft R which happens to utilize more CPU cores than plain R. Best
wishes for your attempt.

On Fri, Apr 10, 2020 at 10:25 AM Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
wrote:

> Translating reference-based OOP designs from other languages into R seems
> like a bad idea, and on the surface looks like re-inventing the wheel. Are
> you sure this couldn't be done with one of the existing packages for doing
> MCMC?
>
> On April 10, 2020 2:21:13 AM PDT, eva lehner <eva-valerie at gmx.at> wrote:
> >Hi Dirk,
> >Below is my question in text format since there were troubles with the
> >html formating.
> >
> >I want to „translate“ a MCMC algorithm written in matlab to R to
> >eventually release it in a package. In the matlab version model input,
> >state of the chain and chain output is stored in structs. Updating
> >different parameters of the underlying model is performed with
> >different functions. I already have a very raw R version where these
> >structs are implemented as S4 classes and updates are performed with
> >generic functions taking both classes as input. I would like to
> >increase performance with RcppArmadillo. However, I struggle to choose
> >a good approach since I am not very experienced in any of the above
> >subjects and have difficulties putting together the different concepts.
> >It would be very helpful if you could give me your thoughts on what I
> >think my main options are, tell me in case I misunderstood some
> >concepts or point me into a better direction.
> >
> >Option 1: mainly R
> >Keeping R S4 classes (or rewrite them as Reference Classes since I
> >think they would have been a better choice for defining a class storing
> >states of a model) and the algorithm written in R and write updating
> >functions in RcppArmadillo. These functions can be exposed to R with
> >Rcpp modules or attributes.
> >+ possibly the easiest version since I only have to rewrite the
> >functions in Rcpp
> >- Still a lot of R which could come with performance costs
> >
> >Option2: mainly Rcpp
> >Define one Cpp class which stores input and updated parameters as
> >member variables and the updating functions as member functions. Then I
> >could write the main MCMC algorithm as a separate function that creates
> >an instance of this class, calls the updating functions and returns a
> >list containing the output. I could only expose this main algorithm
> >(with Rcpp modules or attributes) to R and avoid exposing the internal
> >classes. (Currently I don’t think exposure of the classes is necessary
> >and seems more difficult than exposing functions).The part involving
> >the class is taken from the Kalman filter example (RcppArmadillo-intro
> >vignette) and I imagine the structure of the algorithm function and
> >returned list similar to the gallery entry on the EM Algorithm for
> >Probit Regressions
> >(
> https://gallery.rcpp.org/articles/EM-algorithm-example/#attempt-1[https://deref-gmx.net/mail/client/Q6JdcN6Rp84/dereferrer/?redirectUrl=https%3A%2F%2Fgallery.rcpp.org%2Farticles%2FEM-algorithm-example%2F%23attempt-1]
> ).
> >+ probably faster
> >+ possibly cleaner
> >- more difficult and I have very little Cpp experience (especially with
> >classes)
> >
> >Personally, I aim for a compromise between performance, implementation
> >difficulty and the best approach since this is for my master’s thesis
> >and I also have to consider the deadline. I think this compromise would
> >be to stay with the main R version but rewrite the S4 classes as
> >Reference Class.
> >I'm looking forward to hearing your feedback, thoughts and experiences,
> >Eva
> >
> >
> >
> >Gesendet: Donnerstag, 09. April 2020 um 14:51 Uhr
> >Von: "Dirk Eddelbuettel" <edd at debian.org>
> >An: "eva lehner" <eva-valerie at gmx.at>
> >Cc: rcpp-devel at lists.r-forge.r-project.org
> >Betreff: Re: [Rcpp-devel] Input on code design implementing a MCMC
> >algorithm in R and improving performance with RcppArmadillo
> >Hi Eva,
> >
> >My email client doesn't display you html email, possibly a fault at my
> >end.
> >But text-only email is still a default in most places. Can you maybe
> >resend
> >text-only please? Thanks!
> >
> >Cheers, Dirk
> >
> >--
> >http://dirk.eddelbuettel.com[http://dirk.eddelbuettel.com] |
> >@eddelbuettel | edd at debian.org
> >_______________________________________________
> >Rcpp-devel mailing list
> >Rcpp-devel at lists.r-forge.r-project.org
> >https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
> --
> Sent from my phone. Please excuse my brevity.
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20200410/a5a84c6e/attachment-0001.html>


More information about the Rcpp-devel mailing list