<div dir="ltr">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.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Apr 10, 2020 at 10:25 AM Jeff Newmiller <<a href="mailto:jdnewmil@dcn.davis.ca.us">jdnewmil@dcn.davis.ca.us</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">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?<br>
<br>
On April 10, 2020 2:21:13 AM PDT, eva lehner <<a href="mailto:eva-valerie@gmx.at" target="_blank">eva-valerie@gmx.at</a>> wrote:<br>
>Hi Dirk, <br>
>Below is my question in text format since there were troubles with the<br>
>html formating. <br>
><br>
>I want to „translate“ a MCMC algorithm written in matlab to R to<br>
>eventually release it in a package. In the matlab version model input,<br>
>state of the chain and chain output is stored in structs. Updating<br>
>different parameters of the underlying model is performed with<br>
>different functions. I already have a very raw R version where these<br>
>structs are implemented as S4 classes and updates are performed with<br>
>generic functions taking both classes as input. I would like to<br>
>increase performance with RcppArmadillo. However, I struggle to choose<br>
>a good approach since I am not very experienced in any of the above<br>
>subjects and have difficulties putting together the different concepts.<br>
>It would be very helpful if you could give me your thoughts on what I<br>
>think my main options are, tell me in case I misunderstood some<br>
>concepts or point me into a better direction.<br>
> <br>
>Option 1: mainly R<br>
>Keeping R S4 classes (or rewrite them as Reference Classes since I<br>
>think they would have been a better choice for defining a class storing<br>
>states of a model) and the algorithm written in R and write updating<br>
>functions in RcppArmadillo. These functions can be exposed to R with<br>
>Rcpp modules or attributes.<br>
>+ possibly the easiest version since I only have to rewrite the<br>
>functions in Rcpp<br>
>- Still a lot of R which could come with performance costs<br>
> <br>
>Option2: mainly Rcpp<br>
>Define one Cpp class which stores input and updated parameters as<br>
>member variables and the updating functions as member functions. Then I<br>
>could write the main MCMC algorithm as a separate function that creates<br>
>an instance of this class, calls the updating functions and returns a<br>
>list containing the output. I could only expose this main algorithm<br>
>(with Rcpp modules or attributes) to R and avoid exposing the internal<br>
>classes. (Currently I don’t think exposure of the classes is necessary<br>
>and seems more difficult than exposing functions).The part involving<br>
>the class is taken from the Kalman filter example (RcppArmadillo-intro<br>
>vignette) and I imagine the structure of the algorithm function and<br>
>returned list similar to the gallery entry on the EM Algorithm for<br>
>Probit Regressions<br>
>(<a href="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]" rel="noreferrer" target="_blank">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]</a>).<br>
>+ probably faster<br>
>+ possibly cleaner<br>
>- more difficult and I have very little Cpp experience (especially with<br>
>classes)<br>
> <br>
>Personally, I aim for a compromise between performance, implementation<br>
>difficulty and the best approach since this is for my master’s thesis<br>
>and I also have to consider the deadline. I think this compromise would<br>
>be to stay with the main R version but rewrite the S4 classes as<br>
>Reference Class.<br>
>I'm looking forward to hearing your feedback, thoughts and experiences,<br>
>Eva<br>
><br>
> <br>
><br>
>Gesendet: Donnerstag, 09. April 2020 um 14:51 Uhr<br>
>Von: "Dirk Eddelbuettel" <<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>><br>
>An: "eva lehner" <<a href="mailto:eva-valerie@gmx.at" target="_blank">eva-valerie@gmx.at</a>><br>
>Cc: <a href="mailto:rcpp-devel@lists.r-forge.r-project.org" target="_blank">rcpp-devel@lists.r-forge.r-project.org</a><br>
>Betreff: Re: [Rcpp-devel] Input on code design implementing a MCMC<br>
>algorithm in R and improving performance with RcppArmadillo<br>
>Hi Eva,<br>
><br>
>My email client doesn't display you html email, possibly a fault at my<br>
>end.<br>
>But text-only email is still a default in most places. Can you maybe<br>
>resend<br>
>text-only please? Thanks!<br>
><br>
>Cheers, Dirk<br>
><br>
>--<br>
><a href="http://dirk.eddelbuettel.com" rel="noreferrer" target="_blank">http://dirk.eddelbuettel.com</a>[<a href="http://dirk.eddelbuettel.com" rel="noreferrer" target="_blank">http://dirk.eddelbuettel.com</a>] |<br>
>@eddelbuettel | <a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a><br>
>_______________________________________________<br>
>Rcpp-devel mailing list<br>
><a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
><a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
<br>
-- <br>
Sent from my phone. Please excuse my brevity.<br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></blockquote></div>