简体   繁体   中英

Link Rcpp with C code for Adaptive Metropolis rejection sampling

I'd like to use Adaptive Metropolis rejection sampling(AMRS) for my Gibbs sampling. I could use the package HI in R directly and use the algorithm. However, since I have too many parameters, sampling one by one in a loop in R is rather slow. So I am considering writing the whole sampling part in Rcpp. The problem is how to use the AMRS function in Rcpp. One choice is find a way to use the R function in Rcpp, but it will be slow. Then I found the original C code in the page

[1]: AMRS http://www1.maths.leeds.ac.uk/~wally.gilks/adaptive.rejection/web_page/Welcome.html

So the question become how to integrate the C code with Rcpp. When I'm searching around "call C in C++", those are all about compile C first, then C++, then link. However I don't know how to do that in Rcpp since the compilation happens when I was using SourceCpp. Anyone can give me some suggestions how to achieve that?

Check out how HI implemented the C code in their package .

Specifically, you should implement your own version of:

SEXP arms(SEXP bounds, SEXP myldens, SEXP yprev, SEXP size, SEXP rho);

That is given in https://github.com/cran/HI/blob/master/src/arms-Rc#L86-L172

In this case, you should opt to replace the SEXP object with the appropriate Rcpp data types for:

/* bounds      : boundaries of the support of the density */
/* myldens     : R function to evaluate log density */
/* yprev       : previous value from markov chain */
/* size        : number of sampled values to be obtained */
/* rho         : R environment in which the logdensity is evaluated */

Note: The implementation should really be package-based instead of using sourceCpp()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM