简体   繁体   English

将Rcpp与C代码链接以进行自适应大都市拒绝采样

[英]Link Rcpp with C code for Adaptive Metropolis rejection sampling

I'd like to use Adaptive Metropolis rejection sampling(AMRS) for my Gibbs sampling. 我想对我的Gibbs采样使用Adaptive Metropolis拒绝采样(AMRS)。 I could use the package HI in R directly and use the algorithm. 我可以直接在R中使用HI包并使用算法。 However, since I have too many parameters, sampling one by one in a loop in R is rather slow. 但是,由于参数太多,因此在R的循环中一个接一个地采样非常慢。 So I am considering writing the whole sampling part in Rcpp. 因此,我正在考虑在Rcpp中编写整个采样部分。 The problem is how to use the AMRS function in Rcpp. 问题是如何在Rcpp中使用AMRS功能。 One choice is find a way to use the R function in Rcpp, but it will be slow. 一种选择是找到一种在Rcpp中使用R函数的方法,但这会很慢。 Then I found the original C code in the page 然后我在页面中找到了原始的C代码

[1]: AMRS http://www1.maths.leeds.ac.uk/~wally.gilks/adaptive.rejection/web_page/Welcome.html [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. 因此,问题就变成了如何将C代码与Rcpp集成在一起。 When I'm searching around "call C in C++", those are all about compile C first, then C++, then link. 当我搜索“在C ++中调用C”时,所有这些都是关于编译C,然后是C ++,然后是链接。 However I don't know how to do that in Rcpp since the compilation happens when I was using SourceCpp. 但是我不知道如何在Rcpp中执行此操作,因为编译是在使用SourceCpp时发生的。 Anyone can give me some suggestions how to achieve that? 任何人都可以给我一些建议,以实现这一目标?

Check out how HI implemented the C code in their package . 查看HI如何在其程序包中实现C代码

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 这在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: 在这种情况下,您应该选择使用以下适当的Rcpp数据类型替换SEXP对象:

/* 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() 注意:实现实际上应该是基于包的,而不是使用sourceCpp()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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