Gibbs Sampling in R and C++

喜你入骨 提交于 2019-12-02 13:19:18

What's your question?

R::rgamma() is from Rcpp too. It conveniently wraps the C-level, non-namespaced ::Rf_rnorm().

Note that you also have the vectorised Rcpp::rnorm() -- and that there are plenty of Gibbs Sampler examples out there following the initial post by Darren Wilkinson. Our best example may be this page at the Rcpp Gallery.

Edit: And as you are evidently confused over the shape = 1/rate parameterization, here is a complete and worked example for you:

We compile a convenience R function calling C++ via Rcpp first:

R> cppFunction("NumericVector callrgamma(int n, double shape, double scale) { 
+                  return(rgamma(n, shape, scale)); }")
R>

We then call R, making sure we fix the seed:

R> set.seed(42); rgamma(3, 2.0, 2.0)   # calling R
[1] 1.824478 0.444055 0.779610
R>

Now, using the same seed, we call the C++ function and we make sure we respect the "1/over" reparameterization as well:

R> set.seed(42); callrgamma(3, 2.0, 1/2.0)   # calling Rcpp
[1] 1.824478 0.444055 0.779610
R> 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!