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

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sat Apr 18 00:17:36 CEST 2020


This is veering off topic... but to give you something to chase elsewhere...

Bad idea: reference classes in R. R is functional... RC/R6 break that paradigm and reduce re-usability and are difficult to debug. Every time I have done that I have regretted it later. I can't say definitively that you won't come up with a case that justifies itself... but I would recommend learning a bit about functional software architectures before going too far down that road.

Existing packages: look at the Bayesian Inference Task View on CRAN.

On April 17, 2020 5:16:48 AM PDT, eva lehner <eva-valerie at gmx.at> wrote:
>
>Hi Jeff,
> 
>thank you for the answer. I did not really consider this option. 
>Why do you think that translating a reference-based OOP design to R is
>a bad idea? 
>And do you have a recommendation when it comes to existing MCMC
>packages (I would need gibbs, metropolis-hastings and reversible jump
>samplers)? 
> 
>Cheers, 
>Eva 
>
>Gesendet: Freitag, 10. April 2020 um 17:25 Uhr
>Von: "Jeff Newmiller" <jdnewmil at dcn.davis.ca.us>
>An: rcpp-devel at lists.r-forge.r-project.org, "eva lehner"
><eva-valerie at gmx.at>, "Dirk Eddelbuettel" <edd at debian.org>
>Betreff: Re: [Rcpp-devel] Input on code design implementing a MCMC
>algorithm in R and improving performance with RcppArmadillo
>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[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][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[https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel]
>
>--
>Sent from my phone. Please excuse my brevity.

-- 
Sent from my phone. Please excuse my brevity.


More information about the Rcpp-devel mailing list